Storing a value for each ComboBox item - vb.net

In lack of a Value property I planned to use Classes for storing Text and Value properties for my ComboBox items. So far I've succeeded.
Here is my class:
Public Class clCombobox
Public cname As String
Public cvalue As Integer
Public Property Display() As String
Get
Return Me.cname
End Get
Set(ByVal value As String)
Me.cname = value
End Set
End Property
Public Property Value() As String
Get
Return Me.cvalue
End Get
Set(ByVal value As String)
Me.cvalue = value
End Set
End Property
Public Sub New(ByVal name As String, ByVal value As String)
cname = name
cvalue = value
End Sub
Public Overrides Function ToString() As String
Return cname
End Function
End Class
Data is being added to the ComboBox like this:
cmbComboxBox.Items.Add(New clCombobox("Text", 1))
It seems like this works so far. But how do I get data back. Like if I want the value of the selected CheckBox item?
I tried using:
CType(cmbCombobox.SelectedItem, clCombobox).Value()
Did not work.

As per the documentation, use the SelectedItem property to retrieve the object that you stored in it.
Code to retrieve value you want:
Dim selectedItem as clCombobox = CType(cmbComboBox.SelectedItem, clCombobox)
Dim value As Integer = selectedItem.cvalue

Related

Sort an arraylist of objects by one of the properties?

Hi I have an object called item here is the code for it:
Public Class item
Private itemValue As String
Private urlfoundValue As String
Private typeValue As String
Private scorevalue As String
Public Sub New()
' Leave fields empty.
End Sub
Public Sub New(ByVal item As String, ByVal urlfound As String, ByVal type As String, ByVal score As String)
itemValue = item
urlfoundValue = urlfound
typeValue = type
scorevalue = score
End Sub
Public Property item() As String
Get
Return itemValue
End Get
Set(ByVal value As String)
itemValue = value
End Set
End Property
Public Property urlfound() As String
Get
Return urlfoundValue
End Get
Set(ByVal value As String)
urlfoundValue = value
End Set
End Property
Public Property type() As String
Get
Return typeValue
End Get
Set(ByVal value As String)
typeValue = value
End Set
End Property
Public Property score() As String
Get
Return scorevalue
End Get
Set(ByVal value As String)
scorevalue = value
End Set
End Property
I have an ArrayList of these items that I would like to sort in highest to lowest order by the Score property (is string but can be converted to int32).
The items are all store in this arraylist:
Public fullitem As New System.Collections.ArrayList()
Any suggestions on how to get the items in order by the score? Can be done in c# too.
A direct answer to your question is to use LINQ like this:
Dim orderdList = fullitem.Cast(Of item)() _
.OrderBy(Function(i As item) Convert.ToInt32(i.score))
fullitem = New System.Collections.ArrayList()
For Each item In orderdList
fullitem.Add(item)
Next
This uses Cast to obtain a generic IEnumerable<item> from the ArrayList which then enables you to use LINQ functions like OrderBy.
However, please note that ArrayList is now obsolete, and you should consider using List<Item> instead. If you do that then you can do something as simple as this:
Assuming you have the list defined like this:
Dim list = new List(Of item)()
You can sort it like this:
list = list.Cast(Of item)() _
.OrderBy(Function(i As item) Convert.ToInt32(i.score)) _
.ToList()

Assigning Private Property values

I'm testing out PetaPoco to determine if it meets our needs. I was under the impression that it would delve into my class and see the private properties that I've marked up as valid columns. Unfortunately, it's not behaving as expected. Can you please take a look and let me know if I'm doing something wrong.
My test class:
Imports PetaPoco
<PetaPoco.TableName("PaulTest")>
<PetaPoco.PrimaryKey("ID")>
Public Class PaulTest
<PetaPoco.Column("ID")>
Private Property pvtID As Integer
Private pvtName As String
Private Sub New()
End Sub
Public Sub New(name As String)
If String.IsNullOrEmpty(name) Then
Throw New ArgumentException("Passed Name is empty")
End If
Me.pvtName = name
End Sub
<PetaPoco.Ignore>
Public ReadOnly Property ID As Integer
Get
Return pvtID
End Get
End Property
<PetaPoco.Column("Name")>
Public Property Name As String
Get
Return pvtName
End Get
Set(value As String)
If String.IsNullOrEmpty(value) Then
Throw New ArgumentException("Passed Name is empty")
End If
Me.pvtName = value
End Set
End Property
End Class
The call to the DB (_db is a PetaPoco database object)
Return (_db.Fetch(Of Peta.Domain.PaulTest)(";EXEC selAllPaulTest")).OrderBy(Function(PaulTest) (PaulTest.ID)).ToList()
What's in the database
ID Name
107 Paul
What's being returned:
PaulTest.ID = 0
PaulTest.pvtID = 0
PaulTest.Name = "Paul"
PaulTest.pvtName = "Paul"
<PetaPoco.Ignore> attribute instructs PetaPoco to ignore the column during mapping. As such, the property Id will always be returning the default value of 0.
If you want to prevent the Id property from being modified, comment our or delete the attribute and add a private setter to the Id property as in the following code snippet.
' <PetaPoco.Ignore>
Public Property Id As Integer
Get
Return pvtID
End Get
Private Set(value As Integer)
pvtID = value
End Set
End Property

UserControl + ArrayList + vb.net

When i get the text of a listbox i write
ListBox1.Items(2).ToString()
What is the code that allows me to do this?
UserControl.positioniwant(2).title
UserControl.positioniwant(1).msg
It's called an "Indexed Property":
Public Class Foo
Public Property Position(ByVal index As Integer) As String
Get
Return _position(index)
End Get
Set(ByVal Value As String)
_position(index) = Value
End Set
End Property
Private _position(9) As String
End Class

Deserializing nested json in vb.net

I'm new to json and am working on deserializing some nested json into an object. The outer object works fine, but I'm not getting any values for the inner object. I've tried several solutions including using list object, collections, the datacontractserializer, but nothing seems to work. I think I'm probably missing something obvious. Here is what I have now:
The json string looks like this:
{"type":"lookup","message":"Success","version":0.1,"user":{"loginName":"username","vendor":null}}
My code is as follows:
<Serializable()> Public Class LookupReturn
Private _Type As String = ""
Private _Message As String = ""
Private _Version As String = ""
Private _user As New jsonUser
Public Property Type() As String
Get
Return _Type
End Get
Set(ByVal value As String)
_Type = value
End Set
End Property
Public Property Message() As String
Get
Return _Message
End Get
Set(ByVal value As String)
_Message = value
End Set
End Property
Public Property Version() As String
Get
Return _Version
End Get
Set(ByVal value As String)
_Version = value
End Set
End Property
Public Property Userobj() As jsonUser
Get
Return _user
End Get
Set(ByVal value As jsonUser)
_user = value
End Set
End Property
End Class
<Serializable()> Public Class jsonUser
Private _loginName As String = ""
Private _vendor As String = ""
Public Property loginName() As String
Get
Return _loginName
End Get
Set(ByVal value As String)
_loginName = value
End Set
End Property
Public Property vendor() As String
Get
Return _vendor
End Get
Set(ByVal value As String)
_vendor = value
End Set
End Property
End Class
Dim _Json As New JavaScriptSerializer()
Dim _Message as string = "{"type":"lookup","message":"Success","version":0.1,"user"{"loginName":"username","vendor":null}}"
Dim returnData As LookupReturn = _Json.Deserialize(Of LookupReturn)(_Message)
I'm geting data in the type, message, version values for the LookupReturn object, and it's returning an object for the user item, but the value for loginName is an empty string.
Any help would be appreciated!
Thanks!
After working with this some more, I realized that the problem was that my nested object was not named as it should be in the Get/Set section of my code. Because I was calling it "Userobj" instead of "user" as it was called in the jSon, the deserializer wasn't able to work properly. I changed the name to "user" and all worked well!

OData made from objects

Lets say I have a class called "Item" and "SubItems"
Public Class SubItem
Private _SubItemName As String
Public Property SubItemName() As String
Get
Return _SubItemName
End Get
Set(ByVal value As String)
_SubItemName = value
End Set
End Property
End Class
<DataServiceKey("Name")> _
Public Class Item
Private _Name As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Private _SubItems As List(Of SubItem)
Public Property SubItems() As List(Of SubItem)
Get
Return _SubItems
End Get
Set(ByVal value As List(Of SubItem))
_SubItems = value
End Set
End Property
End Class
How would I create a service that would return a list of Items and upon looking up an individual item I would be able to see the sub items.
You can check out WCF Data Servies reflection provider, e.g.,
http://blogs.msdn.com/b/alexj/archive/2010/06/11/tip-56-writing-an-odata-service-using-the-reflection-provider.aspx
http://msdn.microsoft.com/en-us/library/dd728281.aspx
Hope this helps.