Valuk
Valuk

Reputation: 1636

Convert string to list of custom objects

Say I have an object Fish with properties like

Public Property ID As Integer
Public Property Name As String
Public Property Type As Integer
Public Property Age As Integer

And I have a string that looks like this:

"Fishes[0].ID=1&Fishes[0].Name=Fred&Fishes[0].Type=1&Fishes[0].Age=3&Fishes[1].ID=2&Fishes[1].Name=George&Fishes[1].Type=2&Fishes[1].Age=5&..."

Is there any way of converting/casting/whatever my string into a list of Fish objects? I can't seem to find anything to help. I do have some control over the format of the string if that would make things easier.

Many thanks

Upvotes: 0

Views: 295

Answers (4)

matt-dot-net
matt-dot-net

Reputation: 4244

In the comments you mentioned that this is via ASP.Net MVC. The ModelBinder will do everything for you.

public ActionResult UpdateFish(IList<Fish> listOfFish)
    {
        //the ModelBinder will figure out that the user has posted a list of Fish instances.
        //do something with listOfFish
        return View(listOfFish);
    }

Upvotes: 0

ajakblackgoat
ajakblackgoat

Reputation: 2139

What you need to do is to parse the input string to look for Fishes[X].PROPNAME=VALUE pattern. Loop through all matches found in the string and add into or set the existing object in Dictionary. Use X as each object key in the Dictionary.

Create Fish structure:

Structure Fish
    Public ID As String
    Public Name As String
    Public Type As Integer
    Public Age As Integer
End Structure

Codes to process the input string:

Dim Fishes As New Dictionary(Of String, Fish)
Dim m As Match = Regex.Match(str, "Fishes\[(?<key>\d+)]\.(?<prop>.+?)=(?<value>[^&]+)", RegexOptions.IgnoreCase)

Do While m.Success
    Dim key As String = m.Groups("key").Value.Trim.ToUpper
    Dim prop As String = m.Groups("prop").Value.Trim.ToUpper
    Dim value As String = m.Groups("value").Value

    ' if the key not yet exist in the Dictionary, create and add into it.
    If Not Fishes.ContainsKey(key) Then
        Fishes.Add(key, New Fish)
    End If

    Dim thisFish As Fish = Fishes(key) ' get the Fish object for this key

    ' determine the object property to set
    Select Case prop
        Case "ID" : thisFish.ID = value
        Case "NAME" : thisFish.Name = value
        Case "TYPE" : thisFish.Type = CInt(value)
        Case "AGE" : thisFish.Age = CInt(value)
    End Select

    Fishes(key) = thisFish ' since the Fish object is declared as Structure,
                           ' update the Dictionary item of key with the modified object.
                           ' If Fish is declared as Class, then this line is useless

    m = m.NextMatch()
Loop

Upvotes: 1

skhurams
skhurams

Reputation: 2145

this is vb.net converted

Public Partial Class test
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs)


        If Not IsPostBack Then
        End If

    End Sub
    Public Sub MYtest()
        Dim ls As New List(Of Fish)()
        Dim f As New Fish()
        f.ID = 1
        f.Name = "My name"
        f.Type = "My type"
        f.Age = 20
        ls.Add(f)
    End Sub
End Class
Public Class Fish
    Public Property ID() As Integer
        Get
            Return m_ID
        End Get
        Set
            m_ID = Value
        End Set
    End Property
    Private m_ID As Integer
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set
            m_Name = Value
        End Set
    End Property
    Private m_Name As String
    Public Property Type() As String
        Get
            Return m_Type
        End Get
        Set
            m_Type = Value
        End Set
    End Property
    Private m_Type As String
    Public Property Age() As Integer
        Get
            Return m_Age
        End Get
        Set
            m_Age = Value
        End Set
    End Property
    Private m_Age As Integer
End Class

Upvotes: 0

matzone
matzone

Reputation: 5719

Try this ..

Structure Test
    Dim ID As String
    Dim Name As String
    Dim Type As Integer
    Dim Age As Integer
End Structure

In your Button click event ..

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Fishes As New List(Of Test)

    Dim Fish As New Test
    Fish.ID = "GF"
    Fish.Name = "Gold Fish"
    Fish.Age = 1
    Fish.Type = 1
    Fishes.Add(Fish)

    MsgBox(Fishes(0).Name)

End Sub

Upvotes: 0

Related Questions