简体   繁体   中英

What is the best way to create “dynamic objects” in vb.net webforms

I'm not sure i'm asking this question correctly as i'm still kinda new to this...

But what i'm trying to learn how to do (if possible) is to create a serializable class object with user data (name, address, phone, etc). For my project, i'm referring to this user data as user parameters. As this list will continue to grow. For example, fav song, fav color, etc.

So i want to create the object dynamically based on the available 'parameters' by user. Some users will have just one, others will have more. The user data/parameters will be stored in a separate table with the user GUID and a FK identifying what the parameter is. So ideally I would just have to add a new record to this table and the code behind would just work.

Otherwise... i have to update the serialized class, the updates, the inserts anytime i updated or changed a parm?

Ideas?

Thanks

How about a dictionary?

<Serializable()>
Public Class UserData

  Public Property UserGUID As GUID
  Public Property Parameters As Dictionary(Of String, String)

End Class

Then you could have the dictionary filled with things like:

{{"Name", "Sir Launcelot of Camelot"},
 {"Quest","To seek the Holy Grail"},
 {"Favorite Color","Blue"},
 {"Favorite Place","Castle Anthrax"},
     ...
}

And you could have a table that looks like this:

UserGUID    | Parameter      | Value
-----------------------------------------------------------
AZ-12-43-EF | Name           | Sir Launcelot of Camelot
AZ-12-43-EF | Quest          | To seek the Holy Grail
AZ-12-43-EF | Favorite Color | Blue
34-DF-E4-22 | Name           | Sir Galahad of Camelot
34-DF-E4-22 | Quest          | I seek the Holy Grail
34-DF-E4-22 | Favorite Color | Blue.  No yel--  Auuuuuuuugh!

If you need more complex values for the parameters, maybe you could store a JSON string that goes with each parameter? ... or if you've gone that far, you might make all the user data a JSON string ;)

Edit:

To display the data, you have several options, if you know what entry you want, you can simply grab it out of the dictionary:

If userData.Parameters.ContainsKey("Name") Then
    textBoxName = userData.Parameters("Name")
Else
    'Name value not found
    textBoxName = "Enter name here"
End If

If you want to display all the parameters in a table though, you'll have to convert the dictionary into something else first. For example, to bind to a DataGridView , you could do the following:

Dim parameterList = From entry In userData.Parameters
                    Select New With { .Parameter = entry.Key, entry.Value }
dataGridView1.DataSource = parameterList.ToArray()

In the example above parameterList is actually an IEnumerable of an anonymous type that has two properties, Parameter As String and Value As String .


Resources for learning more about Dictionary :

... and some StackOverflow questions:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM