Click here to Skip to main content
15,907,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is one of those issues where you have to see it yourself to understand - Im freaked out by it as I cannot fathom at all why.... Please save my sanity !

I am using VS2017, VB.NET, Winforms and Framework 4.8

I have been using this class successfully for a number of years, it has been used as a .itemdata for .net for combobox/listboxes etc..

So create a new winforms project, add a new class and paste below, overwriting the pre-generated class code.


Public Class Mylist
    Private sName As String
    ' You can also declare this as String,bitmap or almost anything. 
    ' If you change this delcaration you will also need to change the Sub New 
    ' to reflect any change. Also the ItemData Property will need to be updated. 
    Private iID As String

    ' Default empty constructor. 
    Friend Sub New()
        sName = ""
        ' This would need to be changed if you modified the declaration above. 
        iID = ""
    End Sub

    Friend Sub New(ByVal Name As String,
                   ByVal ID As String)

        sName = Name
        iID = ID
    End Sub

    Public Property Name() As String
        Get
            Return sName
        End Get
        Set(ByVal sValue As String)
            sName = sValue
        End Set
    End Property

    ' This is the property that holds the extra data. 
    Public Property ItemData() As String
        Get
            Return iID
        End Get
        Set(ByVal iValue As String)
            iID = iValue
        End Set
    End Property

    ' This is neccessary because the ListBox and ComboBox rely 
    ' on this method when determining the text to display. 
    Public Overrides Function ToString() As String
        Return sName
    End Function

End Class



Drop a Combobox onto Form1, set it to dropdownlist

Now, in the Form1_Load sub - paste this:
(its an item with a value of how many minutes against that item, i.e. 1 day is 1440 minutes) and so on.

With ComboBox1
    .Items.Add(New Mylist("1 Day only", "1440"))
    .Items.Add(New Mylist("2 Days only", "2880"))
    .Items.Add(New Mylist("4 Days only", "5760"))
    .Items.Add(New Mylist("1 Week only", "10080"))
    .Items.Add(New Mylist("2 Weeks only", "20160"))
    .Items.Add(New Mylist("4 Weeks only", "40320"))
    .Items.Add(New Mylist("2 Months only", "86400"))
    .Items.Add(New Mylist("3 Months only", "129600"))
    .Items.Add(New Mylist("6 Months only", "262800"))
    .Items.Add(New Mylist("1 Year only", "525600"))
    .Items.Add(New Mylist("2 Years only", "1051200"))
    .Items.Add(New Mylist("3 Years only", "1576800‬"))
    .Items.Add(New Mylist("5 Years only", "2628000"))
    .Items.Add(New Mylist("7 Years only", "3679200‬"))
    .Items.Add(New Mylist("10 Years only", "5256000"))
    .Items.Add(New Mylist("Accumulative Indefinitely/Forever", "0"))
End With


and finally, paste this into the ComboBox1_SelectedIndexChanged sub


Dim Mlist As Mylist
       Dim RecID As Int64 = 0

       Mlist = CType(ComboBox1.SelectedItem, Mylist)
       'RecID = Convert.ToInt64(Mlist.ItemData)
       MsgBox(Convert.ToInt64(Mlist.ItemData))
       'MsgBox(Convert.ToInt64(Mlist.ItemData))




The commented out code above, was me trying to find out whether having that bit of code commented/uncommented made any difference and it does not.



So now run the project, you have a pile of items underneath the combobox, select one or two of the first items and the appropriate minutes pops up as per the new mylist class.

Now select 1 Year... all ok yep?
Select 2 years... all ok yep?
Now skip and select 5 years - all ok -yep
Go back and select 3 years .... boom !

Now select 10 years ... all ok
Now select 7 years ... boom !

Why the heck is this happening?


Now you may say - why not just change the mylist class and make the IDs int64's after all, those itemdata's are only numbers.

Well - go on then :)

changing then throws up errors in Visual Studio, where you cannot run the app:

"Comma, ')' or a valid expression continuation expected." errors plus "Character is not valid" and guess where ??? on the 3year and 5year marks, not the 6 year or 10 years which have even higher number values

Now I know I've probably had about 18 hours sleep in the last week but I dont think Im losing my mind - am I ? :P

I have used the mylist class for years, for all sorts of values - Ive never used it in this particular fashion so I may be missing the blindingly obvious...

What I have tried:

Changing the class from string to int64
Posted
Updated 3-Feb-20 2:15am
Comments
phil.o 3-Feb-20 7:32am    
Please define 'boom'.
Member 12561559 3-Feb-20 7:34am    
Conks Out, explodes, does not work, error about not being able to convert string.
phil.o 3-Feb-20 7:48am    
Please, when you detail an issue, use the exact error message in the first place.
Richard MacCutchan 3-Feb-20 8:43am    
Is it really too difficult to provide a proper technical explanation that includes the exact text of an error or exception?

So, apparently you have a parsing issue.
The first thing to do would be to properly parse the string:
VB
Dim Mlist As Mylist = CType(ComboBox1.SelectedItem, Mylist)
Dim RecID As Int64
If (Int64.TryParse(Mlist.ItemData, RecID))
   MsgBox(RecID)
Else
   MsgBox(Mlist.ItemData + " could not be properly parsed.")
End If
Convert class is just a dinosaur which managed to survive so far but which should be avoided as much as possible. For built-in number types, at least, Parse and TryParse methods should be preferred (especially TryParse when you want to validate original string).

As a side note, why not using the proper type (Int64) for ItemData property? Not only will it allow you to get rid of the parsing code, but it will also be much lighter on memory usage if you were to have a lot of them in memory.
 
Share this answer
 
Comments
Member 12561559 3-Feb-20 8:45am    
Will start using the Parse/Tryparse more than the convert - thanks for the headsup - re. the Int64 in the class, I did do that but it still didnt work - some dodgy character in there that came from "somewhere". :S But parse/tryparse - yep - thats a thumbsup from me Phil
phil.o 4-Feb-20 4:48am    
I can only thumb up back to that :)
It's your fault.
There is a "hidden character" in the "3 Year" string (and probably some of the others) between the last character and the closing double quote.
Check for yourself: go to your source code, place the text cursor at the end of the line:
VB
.Items.Add(New Mylist("3 Months only", "129600"))

Cursor left one char. Under the ), yes?
Cursor left one char. Under the ), yes?
Cursor left one char. Under the ", yes?
Cursor left one char. Under the 0, yes? What do you mean no? Do it again! Ah, it is under the zero this time!
Try moving the cursor and your see there is a zero-width character at the end of the string.
Delete that, and the one in the "7 year" line, and it should work OK.


Now all you gotta do is work out how you got 'em in there ... :laugh:
 
Share this answer
 
Comments
Member 12561559 3-Feb-20 8:44am    
Gawdang it ! I can only think they came from me typing them into windows 10 Calc and copy paste and then remove the "," between the millions and hundreds of thousands. Good spotting fella - I thought I was losing my mind !!!! hahahah. Thanks ! I can go back to bed soon. lol
OriginalGriff 3-Feb-20 8:49am    
You're welcome!
Maciej Los 3-Feb-20 11:18am    
Awesome inspection.
New Sherlock Holmes was born.
OriginalGriff 3-Feb-20 11:27am    
I see dead characters ...
Maciej Los 3-Feb-20 11:42am    
:laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900