# ListBox Control's Intrinsic Conversion from Double to Object Problem

Ok crowd, here is the issue. Idle_Mind and I have been dealing with another authors problem, and we have come across an interesting issue with the ListBox control. If you use the Add() method of the list box control on a Double, the lisbox's conversion from Double to Object widens the Double as expected, but unexpectedly returns junk data. To see this for yourself, add a listbox to a forn and try:

For LoopCount As Double = 1 To 2 Step 0.1
Next

NOTE: I'm not looking for a workaround, there are any number of ways around it. This is just an academic discussion about the object itself and what the problem is or may be. If you think you know, there are points in it for you.
LVL 11
###### Who is Participating?

Author Commented:
Not sure what issue you are having...
Notice anything different about those two results? We are trying to figure out the cause.
0

RetiredCommented:

Not sure what issue you are having. I entered the code from your post and the results are shown below. When I selected an item I displayed its value as also shown below. Here is the code I used.

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

For LoopCount As Double = 1 To 2 Step 0.1
Next

End Sub

Private Sub ListBox1_SelectedValueChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles ListBox1.SelectedValueChanged

Console.WriteLine(ListBox1.SelectedItem)

End Sub

Fernando

SP32-102.jpg
SP32-103.jpg
0

Middle School Assistant TeacherCommented:
=)

To state the problem differently, why does the Listbox display the values differently using these two snippets?...
...since the ListBox calls ToString() on each Object in its Item collection, I would expect the two to produce the same result:

Snippet A:

For LoopCount As Double = 1 To 2 Step 0.1
ListBox1.Items.Add(LoopCount) ' produces looooong precise values (x.000000000y) in ListBox
Next

Snippet B:

For LoopCount As Double = 1 To 2 Step 0.1
ListBox1.Items.Add(LoopCount.ToString) ' produces short (x.y) values in ListBox
Next

0

Author Commented:
It seems that every floating point type *except* decimal behaves this way, even 'Object' when inferred to be floating point (which is probably inferred to be a 'Single' in this context, though I am not sure). Very very odd.
For those wondering why I am bothering with this, take a read to what Idle showed me yesterday:
The screenshot below shows the different types I played with....

sshot.png
0

Author Commented:
What I meant to say:
(which is probably inferred to be a 'Double' in this context, though I am not sure)
It is apparently treated as a double judging from the output, but I am willing to believe anything at this point :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.