3

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

RandomlyOnside
  • 436
  • 1
  • 9
  • 22
  • one solution i have thought of is to just create a "list of" so each item in the list will contain the information about 1 parm. – RandomlyOnside Apr 04 '13 at 06:05

1 Answers1

0

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:

Community
  • 1
  • 1
Jeff B
  • 8,572
  • 17
  • 61
  • 140
  • it seems like this could work.... i need to research more about Dictionary ... do you have a good link that explains more about how they work? – RandomlyOnside Jun 10 '13 at 15:42
  • Added some links at the end of my answer, not sure if they're the best, but they should work. – Jeff B Jun 10 '13 at 16:15
  • thanks... i've been researching this. I really think this might work! I'll check out your links, and hopefully i'll be able to try it later this week! – RandomlyOnside Jun 10 '13 at 16:18
  • If this ended up working for you, you should accept this answer. You'll get some reputation points for it ;) If not, you can always add and accept your own answer as well. – Jeff B Jul 10 '13 at 14:27
  • Sorry, i haven't been able to try this yet. I think it will work. I'm just confused as to how to display the dictionary data. Once I get the data into the dictionary (and object). The issue now is how to display that information in the correct labels. And figure out the blank/not-answered parameters so the user can answer them if they desire. Let me know if you have any suggestions on this... But at this point i feel confident enough that this should be the way to go! Thanks you! – RandomlyOnside Jul 10 '13 at 17:32
  • Are you showing just one user at a time? Are the names of the labels fixed and you just want to pull/display data for those defined fields and put them in a textbox? – Jeff B Jul 10 '13 at 21:32
  • Yes, the labels will be fixed, or can be. I am willing to change then around if need be. And to answer the first question... yes & no. Lets say user A is on user B's profile page. That profile page will show user A all of the parameters that user B filled out. But, there is a "team view" so on the team view, a select # of parameters will be displayed (name, pic, etc). Hopefully this helps! THaNKS! – RandomlyOnside Jul 11 '13 at 04:10
  • 1
    I've edited my answer to show how you might get the data out of the dictionary for display purposes. – Jeff B Jul 16 '13 at 13:15