[RESOLVED] Help required for using DICTIONARY Class.
Dear All,
In my project I have a module and inside module I declared an object of type Dictionary class. The declaration is done as,
Code:
Public MyDictionary as Dictionary(of String, String) = New Dictionary(of String, String)
But when i try to add items to my dictionary in the same module say
Code:
MyDictionary.Add("Text", "Hello")
I am getting error saying "Declaration Expected" for "MyDictionary" object even though I have declared it.
Can anybody explain why is it giving this error even after declaration or if the syntax is wrong can anybody post the proper syntax?
Thanks in advance.
Regards,
Susheelss
Re: Help required for using DICTIONARY Class.
First up, rather than this:
Code:
Public MyDictionary As Dictionary(Of String, String) = New Dictionary(Of String, String)
you would normally do this:
Code:
Public MyDictionary As New Dictionary(Of String, String)
Also, you do not declare objects. You declare variables and create objects, then assign the objects to the variables.
As for the question, that does sound odd. Any public member of a module should be accessible anywhere in the project. Are you sure that it's in an actual module and not in a class?
Re: Help required for using DICTIONARY Class.
First of all Thanks Mr.jmcilhinney for your reply.
Yes the declaration is in module. I also tried to declare the Dictionary inside the class and tried to add items. But even then it gave the same error.
Is it necessary that the items be added inside a function or a procedure itself??
Basically what I'm trying to do is to convert the C# statement given below in VB.NET.
public Dictionary<string, string> dict = new Dictionary<string,string>()
{
{"Text", "Hello"},
}
Re: Help required for using DICTIONARY Class.
Ah, sorry. I misinterpreted the error message. Yes, that is saying that you are trying to execute some code in an area where only declarations are allowed. Within the body of a class, all you are allowed to do is declare members, i.e. fields, properties, methods, etc. You can execute other code as long as it can be incorporated into the same line as a field declaration, e.g. you can create an object and assign it to the field by invoking a constructor and you can use object or collection initialisers to set properties or add items. Anything that isn't, or can't be incorporated into, a declaration must be done, as you suggest, within the body of a method or property.
Re: Help required for using DICTIONARY Class.
You can however do this:
Code:
Public MyDictionary As Dictionary(Of String, String) = InitialValues()
Private Function InitialValues() As Dictionary(Of String, String)
Dim objReturn As New Dictionary(Of String, String)
objReturn.Add("Text", "Hello")
objReturn.Add("Text2", "Hello2")
Return objReturn
End Function
Re: Help required for using DICTIONARY Class.
Thanks Mr.Grimfort and Mr.jmcilhinne. The method provided by Grimfort work.
My problem is resolved.
Re: [RESOLVED] Help required for using DICTIONARY Class.
Just noticed this thread is near exact same question, the code given which is similar to your C# copy would actually work in .Net 4.0 (VS2010). Something to keep in mind, I have not seen it myself before.