listbox object different from listbox item name

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

Guest

I added an object to a listbox. This object is a complex number and not a
string so it shows "Consolapplication.Complex" as a list item. If I convert
the complex number to a string then it shows a string which is correct but
also store's it in the listbox as a string. I wan't to store it as a complex
number but view it as a string. How do I go about doing that?
 
Hi,

orahm said:
I added an object to a listbox. This object is a complex number and not a
string so it shows "Consolapplication.Complex" as a list item. If I
convert
the complex number to a string then it shows a string which is correct but
also store's it in the listbox as a string. I wan't to store it as a
complex
number but view it as a string. How do I go about doing that?

Instead of converting to a string, override the ToString method in your
class:

public class Complex
{
// ...
public override string ToString()
{
return "string representation";
}
}


HTH,
Greetings
 
orahm,

You can override your ToString method on your Complex class type to
return the string representation that you wish to see. The ListBox calls
ToString on the items that are added to it for the display value of the
item.

Hope this helps.
 
Thanks for the reply. That worked fine. I did exactly what you both said:
Is the list item really a complexNumber object or a string object now?

If its a string object I need to parse it back to a complex number if I want
to reuse the number. If its a complexnumber object I should be able to just
use select the item and use it again to do calculations. e. g. like this

complexNumber = this.memoryListBox.SelectedItem;

this line gives me an error cannot implicitly convert type object. How would
I get that item back to a variable of my complex type?

public override string ToString()
{
if(Imag > 0)
return string.Format( "{0:F2} + j{1:F2}", Real, Imag);
else
{
double Imag2;
Imag2 = Math.Abs(Imag);
return string.Format("{0:F2} - j{1:F2}", Real, Imag2);
}
}

Is the list item really a complex number object or a string object now?

Nicholas Paldino said:
orahm,

You can override your ToString method on your Complex class type to
return the string representation that you wish to see. The ListBox calls
ToString on the items that are added to it for the display value of the
item.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

orahm said:
I added an object to a listbox. This object is a complex number and not a
string so it shows "Consolapplication.Complex" as a list item. If I
convert
the complex number to a string then it shows a string which is correct but
also store's it in the listbox as a string. I wan't to store it as a
complex
number but view it as a string. How do I go about doing that?
 
orahm,

The item is a complex number, it just uses the ToString method to get
the display string.

You can cast the result from the SelectedItem property back to a
ComplexNumber instance, and then use that.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

orahm said:
Thanks for the reply. That worked fine. I did exactly what you both said:
Is the list item really a complexNumber object or a string object now?

If its a string object I need to parse it back to a complex number if I
want
to reuse the number. If its a complexnumber object I should be able to
just
use select the item and use it again to do calculations. e. g. like this

complexNumber = this.memoryListBox.SelectedItem;

this line gives me an error cannot implicitly convert type object. How
would
I get that item back to a variable of my complex type?

public override string ToString()
{
if(Imag > 0)
return string.Format( "{0:F2} + j{1:F2}", Real, Imag);
else
{
double Imag2;
Imag2 = Math.Abs(Imag);
return string.Format("{0:F2} - j{1:F2}", Real, Imag2);
}
}

Is the list item really a complex number object or a string object now?

Nicholas Paldino said:
orahm,

You can override your ToString method on your Complex class type to
return the string representation that you wish to see. The ListBox calls
ToString on the items that are added to it for the display value of the
item.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

orahm said:
I added an object to a listbox. This object is a complex number and not
a
string so it shows "Consolapplication.Complex" as a list item. If I
convert
the complex number to a string then it shows a string which is correct
but
also store's it in the listbox as a string. I wan't to store it as a
complex
number but view it as a string. How do I go about doing that?
 
Nicholas,
Thanks a lot for your info. Now when I cast to a string it works:
string temp = (string)this.ListBox.SelectedItem;

but when I cast to a complexNumber it doesn't work.
result = (Complex)this.memoryListBox.SelectedItem; //result is a complexNumber
the message is: specified cast is not valid

what am I doing wrong?



Nicholas Paldino said:
orahm,

The item is a complex number, it just uses the ToString method to get
the display string.

You can cast the result from the SelectedItem property back to a
ComplexNumber instance, and then use that.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

orahm said:
Thanks for the reply. That worked fine. I did exactly what you both said:
Is the list item really a complexNumber object or a string object now?

If its a string object I need to parse it back to a complex number if I
want
to reuse the number. If its a complexnumber object I should be able to
just
use select the item and use it again to do calculations. e. g. like this

complexNumber = this.memoryListBox.SelectedItem;

this line gives me an error cannot implicitly convert type object. How
would
I get that item back to a variable of my complex type?

public override string ToString()
{
if(Imag > 0)
return string.Format( "{0:F2} + j{1:F2}", Real, Imag);
else
{
double Imag2;
Imag2 = Math.Abs(Imag);
return string.Format("{0:F2} - j{1:F2}", Real, Imag2);
}
}

Is the list item really a complex number object or a string object now?

Nicholas Paldino said:
orahm,

You can override your ToString method on your Complex class type to
return the string representation that you wish to see. The ListBox calls
ToString on the items that are added to it for the display value of the
item.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I added an object to a listbox. This object is a complex number and not
a
string so it shows "Consolapplication.Complex" as a list item. If I
convert
the complex number to a string then it shows a string which is correct
but
also store's it in the listbox as a string. I wan't to store it as a
complex
number but view it as a string. How do I go about doing that?
 
Hi Orahm,

Perhaps check that you are adding the complex number to the list, not
adding complex.ToString(), eg,

this would work, and would be able to be cast back to Complex
memoryListBox.Add(myCalcObject);

this wouldn't, because the listbox now contains a string, not a Complex
memoryListBox.Add(myCalcObject.ToString());

If you are still having problems, posting the code where you populate
the memoryListBox will help make it clearer where the problem lies.

Cheers -Mike
 
Back
Top