Solved

ListBox Control's Intrinsic Conversion from Double to Object Problem

Posted on 2008-10-02
5
229 Views
Last Modified: 2013-12-17
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
            ListBox1.Items.Add(LoopCount)
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.
0
Comment
Question by:ladarling
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 100 total points
ID: 22624856
Hi ladarling;

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
            ListBox1.Items.Add(LoopCount)
        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
 
LVL 11

Accepted Solution

by:
ladarling earned 0 total points
ID: 22625275
Not sure what issue you are having...
Notice anything different about those two results? We are trying to figure out the cause.
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 100 total points
ID: 22626320
=)

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
 
LVL 11

Author Comment

by:ladarling
ID: 22626943
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:
http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.objectcollection.add.aspx
The screenshot below shows the different types I played with....

sshot.png
0
 
LVL 11

Author Comment

by:ladarling
ID: 22627009
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question