.NET - How do I initialize a dictionary class?

I am creating my first .NET application in Visual Basic using Visual Studio 2010 RC.  I need a place to store SQL query strings that I will use to populate controls on my forms.  I started to store these strings in the project resources or settings, but then thought that that might not be very safe.  So I thought I would create a class with a dictionary object in it and store my query strings there.

In VBA, a class has an initialize event that fires when the class is instanced.  I cannot find such an event for .NET classes.  My question is, do I just need to create a Sub called "New" and will it automatically be fired when a create a new instance of this class?  If so, the code below should populate my dictionary with all the SQL strings I need for my application when the class is created (right?).

If you see any problems with this, please let me know.  Or if you can suggest a better way to do what I'm trying to do, I'll also award points.
Class clsDict
    Dim dictLookup As New Dictionary(Of String, String)
    Sub New()
        dictLookup.Add("query1", "select * from customers")
        dictLookup.Add("query2", "select * from employees")
        dictLookup.Add("query3", "select * from cities")
    End Sub
End Class

Open in new window

LVL 5
KTN-ITAsked:
Who is Participating?
 
KTN-ITAuthor Commented:
Nope.  Didn't work.  That's how constructors work in C#, but is Visual Basic.NET, constructors are, in fact, created using Sub New().

The hard part is finding any technical information or language reference for VB.NET!  Microsoft has plenty of good documentation on C#, and I usually end up reading the C# reference and using the VB code example there.  But I guess they thought constructors would be too much for VBers to handle.

Here's the page I finally found on VB constructors:
http://msdn.microsoft.com/en-us/library/ms973814.aspx#creatingclasses_topic7
0
 
dericstoneCommented:
In VB, when a class is created, its constructor is called. So instead of creating a method called New, create a method with the name of the class -- that is the constructor. See the code below.
Public Class Form1
    Dim myCls As clsDict
    Private Sub Form1()
        myCls = New clsDict()
        myCls.dictLookup.Add("query4", "select * from countries")
    End Sub
End Class

Class clsDict
    Public dictLookup As New Dictionary(Of String, String)
    Public Sub clsDict()
        dictLookup.Add("query1", "select * from customers")
        dictLookup.Add("query2", "select * from employees")
        dictLookup.Add("query3", "select * from cities")
    End Sub
End Class

Open in new window

0
 
KTN-ITAuthor Commented:
Thanks for the quick response.

This page says that constructors don't apply to VB.NET, just C#:
http://msdn.microsoft.com/en-us/library/ms173115.aspx

I'll try it out and see if it works.  It looks like Visual Basic documentation is getting sparse, maybe I should be learning C#...
0
 
dericstoneCommented:
You're right and I'm wrong. I shouldn't be anwering questions outside my area of expertise!
At least you could answer your own question! Kudos.
0
 
KTN-ITAuthor Commented:
While your comment might not have been entirely correct, you helped steer me in the right direction by mentioning "constructors."  So thanks for your help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.