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