הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Wednesday, April 4, 2018 10:29 AM
Dear All,
How to add multiple Data to string dictionary?
I have definition stringParam on my Module.
Public stringParam As New Dictionary(Of String, String)
I want add that stringParam with data in one stringParam.add
stringParam.Add("param1", value1)
stringParam.Add("param2", value2)
stringParam.Add("param3", value3)
best regards,
Surbakti
All replies (6)
Thursday, April 5, 2018 9:03 AM ✅Answered | 1 vote
Add a new code module, call it whatever you like. Replace the contents with.
Namespace My
<ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Never)> _
Partial Friend Class _Data
Public StringParam As New Dictionary(Of String, String)
Public sub Add(pKey As string,pValue As string)
If Not StringParam.ContainsKey(pKey)
StringParam.Add(pKey, pValue)
End If
End sub
End Class
<HideModuleName()> _
Friend Module CustomUserObject
Private ReadOnly Instance As New ThreadSafeObjectProvider(Of _Data)
ReadOnly Property Data() As _Data
Get
Return Instance.GetInstance()
End Get
End Property
End Module
End Namespace
Let's say you do the following in one form
My.Data.Add("param1","Some value")
Then in another form.
My.Data.Add("param1","Another value")
My.Data.Add("param2","Last")
Then in another form
for each kvp As KeyValuePair(Of string,string) In My.Data.StringParam
Console.WriteLine(kvp)
Next
We get (of course you would access the values via the key rather than just iterate as done above.
[param1, Some value]
[param2, Last]
Or this version can update an existing key
Namespace My
<ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Never)> _
Partial Friend Class _Data
Public StringParam As New Dictionary(Of String, String)
Public sub Add(pKey As string,pValue As string)
If Not StringParam.ContainsKey(pKey)
StringParam.Add(pKey, pValue)
Else
StringParam(pKey) = pValue
End If
End sub
End Class
<HideModuleName()> _
Friend Module CustomUserObject
Private ReadOnly Instance As New ThreadSafeObjectProvider(Of _Data)
ReadOnly Property Data() As _Data
Get
Return Instance.GetInstance()
End Get
End Property
End Module
End Namespace
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Wednesday, April 4, 2018 11:29 AM
You can initialize with a collection initializer
dim dict as New Dictionary(Of String,string) From
{
{"param1", value1},
{"param2", value2},
{"param2", value3}
}
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Wednesday, April 4, 2018 3:10 PM
You'll need to create a Dictionary(Of String, List(Of String)) or similar if you want to have multiple values stored by the same unique key. Note that this requires that you add a new List(Of String) before adding values e.g.
Dim stringParam As New Dictionary(Of String, List(Of String))
stringParam.Add("param1", New List(Of String))
stringParam("param1").Add(value1)
stringParam("param1").Add(value2)
stringParam("param1").Add(value2)
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
Thursday, April 5, 2018 8:39 AM
I want change item from lot of form.
for example:
In FormA :
stringParam.Add("param1", value1)
stringParam.Add("param2", value2)
stringParam.Add("param3", value3)
But in FormB :
stringParam.Add("param1", trial1)
stringParam.Add("param2", trial2)
stringParam.Add("param3", trial3)
stringParam.Add("param4", trial4)
I want access that stringParam from lot of form, that's why I create in Module.
Best regards,
Surbakti
Thursday, April 5, 2018 10:26 AM
Karen,
Are you able in words to tell what the code which you show does?
At least I'm not able to do that. My perception was that the OP wanted to add a string array like this {{"A","1"},{"B","2"}
But that is in my perception not what it does.
I hope it is not a method likewise
Private Sub A(Y As KeyValuePair(Of String, String))
x.Append(Y)
End Sub
Success
Cor
Thursday, April 5, 2018 11:03 AM
Karen,
Are you able in words to tell what the code which you show does?
At least I'm not able to do that. My perception was that the OP wanted to add a string array like this {{"A","1"},{"B","2"}
But that is in my perception not what it does.
I hope it is not a method likewise
Private Sub A(Y As KeyValuePair(Of String, String)) x.Append(Y) End Sub
Success
Cor
Hi Cor, rather then me describe all of the code, perhaps those interested would read the following and HideModule. Lastly my MSDN code sample.
In plain English, the custom My namespace I provided allows a developer to access the Dictionary anyplace in the current project but not in another project.
Also I could had made StringParam a property
Public Property StringParam As New Dictionary(Of String, String)
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator