Sign in to follow this  

Best practice for instantiation in event? C#

This topic is 3486 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The Full Question: Hello! I am learning C#, and am trying to create a small exercise program that I thought up. It's a small app that lets me select from 5 different People objects I have created to "send a chat to" one of the other 5 People objects (the "chat" itself is a messagebox that pops up, saying the message and who sent it to who). Chat is a class I created that has a few methods of its own. My problem is that when the user clicks the "Send Chat" button on the form, I need to create a new Chat object in the SendChat() method of my People object, but I don't know how to name it. If I give it a name in the SendChat() method, I believe I will get an error once I try it again, because I will be trying to create an object with the same reference name as one that already exists, correct? The only work-arounds I have come up with are all "clever code", that seem to be extremely sloppy and prone to errors. What is the way that you actually do this? Oddly enough, the book I am working from ("Head First C#") has not gone over that yet, nor has it shown how to check to see if an object exists already. Any help would be very appreciated! I tried doing a couple of searches, but could not come up with anything with the ways I thought to word it. Thank you! The Abridged Question: How do you have a button that creates instances of a class multiple times without having a conflict in the name? I have a feeling this has to do with checking if it exists, but the book I am reading has not gone over how to check if it exists. Thanks! [Edited by - Ciosolla on May 27, 2008 8:49:55 PM]

Share this post


Link to post
Share on other sites
Quote:

My problem is that when the user clicks the "Send Chat" button on the form, I need to create a new Chat object in the SendChat() method of my People object, but I don't know how to name it. If I give it a name in the SendChat() method, I believe I will get an error once I try it again, because I will be trying to create an object with the same reference name as one that already exists, correct? The only work-arounds I have come up with are all "clever code", that seem to be extremely sloppy and prone to errors. What is the way that you actually do this? Oddly enough, the book I am working from ("Head First C#") has not gone over that yet, nor has it shown how to check to see if an object exists already.

You are confused, I believe.

The name you use for a variable is a code concept, not a runtime concept. That is, it only matters in code -- and in particular, in only a small subset of the code for which the variable is considered to be 'in scope' (generally, that scope is the method containing the variable, or the class containing the variable). Names do not correlate to individual instances of objects at runtime; there are other, more powerful mechanisms that the runtime will use internally to track references to objects.

When you use 'new' to create a new object, you are creating a new object. It doesn't matter what variable you assign the resulting object to:

Chat chat = new Chat();
chat = new Chat();
chat = new Chat();
chat = new Chat();
chat = new Chat();

The above is perfectly legal, if rather useless, C#.

So, if you need to create a new Chat object in the SendChat method:

public void SendChat() {
Chat chat = new Chat();

MessageBox.Show(chat.Message,"Chat");
}

(or whatever is appropriate.

Now, a potentially related issue exists if you want to be able to refer to previously-created Chat objects outside of that function. If that is the case, you should store a list of Chat objects in the class containing the SendChat method, and SendChat should create a new Chat object and add it to that list. It might look like this:

class SomeClass {
List<Chat> chats = new List<Chat>();

public void SendChat() {
Chat chat = new Chat();
chats.Add(chat);

MessageBox.Show(chat.Message,"Chat");
}
}


That way, if you do need to find a previously created Chat object, you can ask the list for it.

Share this post


Link to post
Share on other sites
Ahh yes, I was confused. For some reason, I thought that I would get an error if I created a new object with an existing reference that was already pointing to an object. Now that I stop and think about it, it makes sense that I would not get an error. It would just be garbage collected (the old one, that is). I guess I was thinking of something else I have learned along my long journey to try to learn a C-type language. Thank you!

Share this post


Link to post
Share on other sites

This topic is 3486 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this