.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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.