0

I want to assign an object of one class to another class by constructor, and retrieve the values of the class assigned by constructor from the class which has the constructor. Sounds confusing, it is too :). Can't handle it. Do you have any ideas? Here is an example code.

public class Class1 { public class1(int value1) { Value1 = value1; } public int Value1 { get;} } public class Class2 { public object Object1 {get; set;} } public class main { Class1 TestClass = new Class1(15); Class2 TestClass1 = new Class2(); public void main() { TestClass1.Object1 = TestClass(); //Now I want to this Console.WriteLine(TestClass1.Object1.Value1); TestClass1.Object1.Value1; } } 
3
  • 2
    TestClass1.Object1 = TestClass(); What do you expect to happen here? Commented Nov 5, 2019 at 12:20
  • 1
    ... and this TestClass1.Object1.Value1; would be seen as a code smell by a good part of developers. Commented Nov 5, 2019 at 12:21
  • Ok, now, while the answers give you explanations about what you would need to change to make this "work" and why your version doesn't, I'd like to throw in that I actually wouldn't recommend this altogether. You should hide Value1 from Class2 in most cases. This is a pretty abstract example, but for example if you need to compute something from Value1, why not make Class1 do the math? Commented Nov 5, 2019 at 12:31

3 Answers 3

1

For that you need to creare property of that particular type instead of using base object type like:

public class Class2 { public Class1 Object1 {get; set;} } 

then you can write :

Class1 TestClass = new Class1(15); Class2 TestClass1 = new Class2(); public void main() { TestClass1.Object1 = TestClass; //Now you can do what you wanted Console.WriteLine(TestClass1.Object1.Value1); } 

if you really want to used object type then you will need to cast the object to it's actual type and then you can have the properties available at compile time :

public class Class2 { public object Object1 {get; set;} } 

and now:

public void main() { TestClass1.Object1 = TestClass; Class1 temp = TestClass1.Object1 as Class1; // safe guard in case cast fails if(temp !=null) { //Now you can do what you wanted Console.WriteLine(temp.Value1); } } 
Sign up to request clarification or add additional context in comments.

Comments

0

Class stores it's child as object and object doesn't have a property Value1.

You need to cast the Object1 property or make Class2 generic.

Casting

Class1 o1 = new Class1(15); Class2 o2 = new Class2(); o2.Object1 = o1; var x = o2.Object1 as Class1; //If o2.Object1 is not Class1 x will be null. Console.WriteLine(x.Value1); 

Making Class2 generic

public class Class1 { public Class1(int value1) { Value1 = value1; } public int Value1 { get; set; } } public class Class2<T> { public T Object1 { get; set; } } public static async Task Main() { var o1 = new Class1(15); var o2 = new Class2<Class1>(); o2.Object1 = o1; Console.WriteLine(o2.Object1.Value1); } 

Comments

0

Following approach take nested instance as a constructor parameter whilst constructing outer / container instance.

 public class Inner { public Inner(int value) { Value = value; } public int Value { get; } } public class Outer { public Outer(Inner inner) { InnerObject = inner; } public Inner InnerObject { get;} } public class main { static void Main() { var outer = new Outer(new Inner(10)); Console.WriteLine(outer.InnerObject.Value); var value = outer.InnerObject.Value; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.