Display ListBox object as string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have started an earlier thread about this topic. I got some good info but
I'm still confused. I'm trying to add a complex number object that contains a
real and a imaninary double to a listbox. But I want the listbox to show a
string that describes the number. e. g. 4 +j5

this.memoryListBox.Items.Add(result.ToString());
this line adds the string just like the example but not the actual refrence
to the complex class object, which is what I need to add so I can use it
again to do math with it.

This line adds the complex number object but it doesn't show the number in
the list for the user.
this.memoryListBox.Items.Add(complexNumber);

How do I show a different obect than is stored?
 
I have started an earlier thread about this topic. I got some good info but
I'm still confused. I'm trying to add a complex number object that
contains a
real and a imaninary double to a listbox. But I want the listbox to show a
string that describes the number. e. g. 4 +j5

this.memoryListBox.Items.Add(result.ToString());
this line adds the string just like the example but not the actual
refrence
to the complex class object, which is what I need to add so I can use it
again to do math with it.

This line adds the complex number object but it doesn't show the number in
the list for the user.
this.memoryListBox.Items.Add(complexNumber);

How do I show a different obect than is stored?

From your description it rather sounds that you've forgotten the "override"
keyword for the ToString method in your ComplexNumber class?

// Bjorn A
 
no, I actually do the override and it prints out e.g 4 +j5 just the way I
formatted the ToString function. But it's a string now not a complex object
which is what I would need to do calculations when I retrieve it from the
list.
 
This line adds the complex number object but it doesn't show the number in
the list for the user.
this.memoryListBox.Items.Add(complexNumber);
That line should work it you override the ToString method correctly.
What did you mean by "it doesn't show the number in the list for the
user."? It show something else, or it ignore the Add method completely?
Could you post the code including signature of ToString method? In your
post you use both "result" and "complexNumber". Both of them are of the
same complex number type?
 
...
If you *have* overridden the ToString method correctly, then it's *that*
method that will be invoked to get what is to be displayed in the ListBox,
when you put an instance into the listbox.
no, I actually do the override and it prints out e.g 4 +j5 just
the way I formatted the ToString function. But it's a string
now not a complex object which is what I would need to do
calculations when I retrieve it from the list.

Have you tried to put your complexNumber into the ListBox, or do you only
"assume" that it won't work?

If you *have* overridden the ToString method correctly in your complexNumber
class, and you *have* put the complexNumber into the Listbox, and it *still*
won't work, there must be something else going on.

Either you really aren't putting the actual instance into the listbox, or
your framework is seriously broken.



// Bjorn A
 
I finally got it to work. Thanks for all the hints. At first I was putting
the complex object in there and it showed windowsapplicaion1.complex in the
listbox and not the string. Then I initialized the property of the listbox
where you can add items from the beginning to "Memory". And from then on it
worked and showed my modified ToString of the complex number.
 
I finally got it to work. Thanks for all the hints. At first I
was putting the complex object in there and it showed
windowsapplicaion1.complex in the listbox and not the string.

....which only can be the result of *not* overriding the ToString method
correctly.

class Complex
{
// ...

// note the keyword "override"
public override string ToString()
{
// ...
return "something";
}
}

It *could* be possible that you used an "old" version of Complex, where you
not yet had implemented ToString.

// Bjorn A
 
Back
Top