Custom Object Question
-
I just started taking a look at C# yesterday, it's pretty interesting. It's like what Java would have become if it wanted to be like C/C++. First question: I have a small class with a string and a long. I want to create an object using that class and add it to a collection and to a ListBox. That part works, but when I add the object to the list box, instead of saying what it should, instead it says "project1.Form1+MyObject". I'm pretty sure it doesn't know know how to represent that object as a string so it uses that string as a default. I can also type "MessageBox.Show ("" + obj);" and it tells me the same thing. My problem is I don't know how to add the functionality to my C# class that when asked for a String that I can give it the right information. I've looked around this message board and on all the C# articles but I can't find anything about it.
-
I just started taking a look at C# yesterday, it's pretty interesting. It's like what Java would have become if it wanted to be like C/C++. First question: I have a small class with a string and a long. I want to create an object using that class and add it to a collection and to a ListBox. That part works, but when I add the object to the list box, instead of saying what it should, instead it says "project1.Form1+MyObject". I'm pretty sure it doesn't know know how to represent that object as a string so it uses that string as a default. I can also type "MessageBox.Show ("" + obj);" and it tells me the same thing. My problem is I don't know how to add the functionality to my C# class that when asked for a String that I can give it the right information. I've looked around this message board and on all the C# articles but I can't find anything about it.
Sound like you need to override the System.Object.ToString() method. Take a look at the .Net SDK documentation for hints on handling this. Hey don't worry, I can handle it. I took something. I can see things no one else can see. Why are you dressed like that? - Jack Burton
-
Sound like you need to override the System.Object.ToString() method. Take a look at the .Net SDK documentation for hints on handling this. Hey don't worry, I can handle it. I took something. I can see things no one else can see. Why are you dressed like that? - Jack Burton
That sounds like just what I was looking for. I'll check that out, thanks a lot.