Simple Question regarding Objects  
Author Message
naiad





PostPosted: Visual C# Language, Simple Question regarding Objects Top

I'm a bit reluctant to post this question in the forums because I know that the answer is going to be something fundamental, but I've come to a point where I simply can't continue programming without having it answered and after hours of searching I'm at an end of what to do. So here goes:

How can I access the properties and methods of an object from a class other than the class that the object was created in. For example, I create a new object (called object1) in a class (lets say Entity.cs) and this new object, object1, contains a method called update. Now here's the problem, I want to call the object1.update method from a class other than the Entity class (for example Other.cs) how can I do this without creating a new object in the Other class.

^Hope that makes sense.^ Thanks in advance for your help.



Visual C#17  
 
 
ahmedilyas





PostPosted: Visual C# Language, Simple Question regarding Objects Top

you simply need to pass a reference of that object to the other class! Thats all you need to do my friend. Either pass a reference to it (via constructor of another class) OR via a property which the other class can access. Example:

 

//main form

Entity theEntity = new Entity();

OtherClass otherClass = new OtherClass();

otherClass.TheEntity = theEntity;

 

//otherclass:

 

private Entity theEntity = null;

 

public Entity TheEntity

   get { return this.theEntity; }

   set { this.theEntity = value; }

}

...

...

if (this.theEntity != null)

   this.theEntity.Update();

}

 

 

does this make sense In order for you not to say, create a new object but you wish to use the one that you created from another class, you need to pass a reference to it. You could also, for example, in .NET 2.0 create a strong typed collection of Entity (a generic collection) and add your entity objects in here, then pass this collection to the other classes so they can do whatever they want with the collection and its objects in here.



 
 
naiad





PostPosted: Visual C# Language, Simple Question regarding Objects Top

I'm a bit confused by the class names you used ahmedilyas, could you or someone else that understands the code rephrase it using these specific circumstances: I create an object of the class Entity called Entity1 in Form1 and I want to access this same object from a different form called NewEntitySelect.  It might make more sense to me if you use my own names instead. Thanks in advance!
 
 
ahmedilyas





PostPosted: Visual C# Language, Simple Question regarding Objects Top

it'll be my pleasure

  • Create an object of a class entity in Form1:

    Entity entity1 = new Entity();

  • get NewEntitySelect form to have this entity object created in Form1:

    NewEntitySelect es = new NewEntitySelect();

    es.TheEntity = entity1;

    in NewEntitySelect, we need to create:

  • private Entity variable

  • public Entity type property to accept and store this entity variable:

    //NewEntitySelect

    //global variable decleration:

    private Entity theEntity = null;

    public Entity TheEntity

    {

    get { return this.theEntity; }

    set { this.theEntity = value; }

    }

    //still in NewEntitySelect form, an example of accessing the entity object:

    this.theEntity.DoSomething(); //for example

    so:

  • you modify the NewEntitySelect form to have a private global variable of type Entity (so you can access the variable within the scope of the class) and have a public property which can allow access to getting or setting the private global variable.

  • in main form (form1) we create a new entity object, and create a new instance of the NewEntitySelect form and set the "TheEntity" property to the instance of the entity object, entity1, to it. This will then in a sense pass a reference of that entity1 object over to the newEntitySelect form, which then it can access.

    does this make sense



  •  
     
    naiad





    PostPosted: Visual C# Language, Simple Question regarding Objects Top

    Yes. Just to clarify, you're essentially storing a "copy" (I know it's not technically a copy, it's a reference) of entity1 in a property of the NewEntitySelect form called TheEntity.

    Now, a question: Is it preferable to use the private variable or the public property within newEntitySelect


     
     
    ahmedilyas





    PostPosted: Visual C# Language, Simple Question regarding Objects Top

    thats correct.

    well both really since you can control if you just want to allow access to a private variable or to set the variable. Properties are good practice since you can control the accessibility of a private object in that class.

    you can either:

  • get only

  • set only

  • get and set

     

    the private global variable in question. I did this as an example but you can just ignore get and use set only.

    you shouldnt have to expose a variable publically otherwise other classes may mess up your variables at runtime for example so properties are a good way of controlling how you can access an object and use it



  •  
     
    naiad





    PostPosted: Visual C# Language, Simple Question regarding Objects Top

    Thanks a lot mate, first rate help!
     
     
    ahmedilyas





    PostPosted: Visual C# Language, Simple Question regarding Objects Top

    Much appreciated for your feedback and thanks!