【问题标题】:How do I get values from a class that depend on the one I need the value in? [closed]如何从依赖于我需要该值的类中获取值? [关闭]
【发布时间】:2017-09-17 10:48:14
【问题描述】:

我有相互依赖的 class1 和 class2。我需要一个来自 class1 的值,如果它是静态的,它会与每件事都发生冲突。

简而言之就是这种关系

class one
{
    static var a;

    var b //the one that can't be static

    static var c;

    class two
    {
        public two()
        {
            do
            {
                c = f(a);
            }
            while( c == b )
    }

    public one(var A)
    {
        a = A;

        b = 0;

        for( somthing ) 
        {
            b = c;
            two();
        }
    }
}

【问题讨论】:

  • 请发布显示问题的代码;这甚至不会编译。
  • 您认为为什么需要这样做?请看What is the XY problem?
  • 您确实意识到这不是甚至远程有效的C#,对吧?
  • 耶哈。但是代码乱七八糟,所以代码更能说明问题。

标签: c# class static


【解决方案1】:

如果你认为你需要这样的循环依赖,那几乎可以肯定是存在设计缺陷。请看:What is the XY problem?

如果你坚持的话,这样的事情实际上会起作用:

public class A
{
    public B SomeB { get; set; }

    public int SomeInt { get; set; }
}

public class B
{
    public int SomeVar { get; set; }

    public A SomeA { get; set; }
}

static void Main(string[] args)
{
        A a = new A();
        a.SomeB = new B();
        a.SomeB.SomeA = a;
}

【讨论】:

  • 我可以看到你确实理解了那个 sudo 代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多