.NET Object as Property of another object?

J

Joe Barbara

I want to make an object called Object1 and and Object called Object2

Object1 has properties x and y

Object2 contains Object1 as a property



should I be able to do:

Object2.Object1.x

As I would have with COM?




or should I have to do

Object2.Object1 = new Object1(x,y)




When are references allowed in .NET? I can make both cases work and I like
the COM style, but is that improper since this is .NET with no references?


Comments thoughts?

(e-mail address removed)
 
N

Nicholas Paldino [.NET/C# MVP]

Joe,

It really doesn't matter how you do it. You can have your property
allow itself to be set by providing code for the set section of the property
declaration.

Or, you can have your class generate the instance of Object1, and then
return that through the Object1 property (not requiring a set). It's up to
you, and how you want to design your class.

Hope this helps.
 
J

james

If object1 is public you can do this as you have described it.

class Object2
{
private _object1 = null;
public Object2
{
_obj1 = new Object1();
}

public Object1 Object1 { get { return _object1;} set { _object1 =
value; } }

}

your app

obj2 = new Object2()

obj2.Object1.x = "yyyy";

JIM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top