0

a simple question :

public class class1
{
     public string string1;
     public class class2
     {
         public string string2
         {
             get{ string tmp = class1.string1;  } 
         }
     }

}

I want to be able to reach class1.string1 from class2.string2.get, but I cant. What would you recommend me to change, so that I can do that?

Thanx

EngelbertCoder
  • 777
  • 2
  • 9
  • 29
  • 1
    If you wish to access it as class1.string1 you will need to have a static value. Otherwise you need to have a reference in class2 to the class1 object. When initialising class2, maybe add a reference as one of the constructor parameters to the class1? – Sander Jun 20 '13 at 09:26
  • @Sander since that is the correct answer you should post it as an answer :) add some elaboration on that there's a possible one-to-many relationship between superclass and subclass, which is why there can't be a straight mapping by definition. – Niels Keurentjes Jun 20 '13 at 09:31
  • Please can you better explain in your question how you are using the inner class inside your main class? is string1 `static`? – davioooh Jun 20 '13 at 09:45
  • class2 is not initialized. It is used in a function as type inside class1 like : GetQueryable() .Where(..) – EngelbertCoder Jun 20 '13 at 09:51
  • @NielsKeurentjes I guess I am too late now :) – Sander Jun 20 '13 at 12:24

4 Answers4

1

Passing class1 reference to class2 in constructor:

public class class1 {
  public string string1;
  public class class2 {
    private class1 _Reference;
    public class2(class1 reference) {
      if (reference == null) {
        throw new ArgumentNullException("reference");
      }
      _Reference = reference;
    }
    public string string2 {
      get { return _Reference.string1; }
    }
  }

}

Passing class1 reference to class2 after both classes have been created:

public class class1 {
  public string string1;
  public class class2 {
    private class1 _Reference;

    public class1 Reference {
      set { _Reference = value; }
    }

    public string string2 {
      get { return _Reference.string1; }
    }
  }

}

static void usage() {
  var foo = new class1();
  var bar = new class1.class2();
  bar.Reference = foo;
  string value = bar.string2;
}
Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89
1

There is no means of accessing a class from within a nested class that I know of. Class nesting doesn't lead to automatic instantiation of the surrounding class, it's just a (usually rather smelly) means of structuring your code.

You would either need a reference to an actual instance of Class1 inside Class2 or you'd need a static method on Class1.

Another way to accomplish this would be to use inheritance, but that's a whole different beast to tame:

public class Class1 {
    protected String String1 { get; set; }
}

public class Class2 : Class1 {
    public String String2 {
        get {
            String PropertyFromClass1 = base.String1;
            // ...
        }
    }
}

That said: Your code wouldn't compile, string2's getter doesn't return anything. And please make yourself familiar with C#'s naming conventions.

Community
  • 1
  • 1
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
0

Thanx for the suggestions. Due to the specific nature of the code, I had to solve this situation with a global public static class in another namespace.

EngelbertCoder
  • 777
  • 2
  • 9
  • 29
0

Coming from Java I faced this "problem" when I started developing in C#.

As clearly explained by Dennis Traub and in this article in C# you can't access outer class members or methods. So you have to implement what in Java happens automatically:

class OuterClass {
    string s;
    // ...
    class InnerClass {
        OuterClass o_;
        public InnerClass(OuterClass o) { o_ = o; }

        public string GetOuterString() { return o_.s; }
        }

    void SomeFunction() {
        InnerClass i = new InnerClass(this);
        i.GetOuterString();
    }
}
davioooh
  • 23,742
  • 39
  • 159
  • 250