Click here to Skip to main content
15,843,031 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am migrating from vb.net to c#.net. In my projects in vb.net I code the following to store and retrieve values from combobox / listbox and they are working fine.
VB
Public Class CItemList
    Private sName As String
    Private iID As Long

    ' Default empty constructor.
    Public Sub New()
        sName = ""
        iID = 0
    End Sub

    Public Sub New(ByVal Name As String, ByVal ID As Integer)
        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 Long
        Get
            Return iID
        End Get
        Set(ByVal iValue As Long)
            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

' adding name and id to listbox
lst.Items.Add(New CItemList("xyz", 1))

'to retrieve value from the combobox / listbox
Dim ItemList As CItemList
ItemList = lst.Items(lst.SelectedIndex)
Return Convert.ToString(ItemList.ItemData)

Can anyone please give me the equivalent of this in c#.net ?

By using CItemList I can store and retrieve values easily.

Thank you in advance.
Posted
Updated 10-Jan-15 20:23pm
v2
Comments
Kornfeld Eliyahu Peter 11-Jan-15 2:57am    
What is the problem? All you need is some syntax change according to the differences between the languages!

If you want to translate VB to C#, there are several online converters which will do a pretty good job:
http://www.developerfusion.com/tools/convert/vb-to-csharp/[^] and http://converter.telerik.com/[^] both work fine...
 
Share this answer
 
Try to use the System.Windows.Forms.ListBox.ObjectCollection class.

Refer to http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.items(v=vs.110).aspx[^] for details. It should certainly help you.

Thanks
 
Share this answer
 
Comments
tiagu 11-Jan-15 6:43am    
Hi guys, thanks for your time and help. I have got it. Thank you all once again.
tiagu 1-Apr-16 12:57pm    
Thank you friends for your time. I got it in c#.net and using for my projects.

Thanks once again.

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