Constructors

JasonD

Member
Joined
Aug 13, 2006
Messages
17
Location
Papillion, NE
Programming Experience
3-5
Application Overview
My application is an account and character manager for a game, it uses MySQL's ODBC driver to communicate with the server.

Now, I didn't use the best of my knowledge when doing the queries, as every query requires a reconnection to the server.

The way I designed it was, when the user "connects", it just checks if the connection to the server is possible at the time, then saves the connection details in variables so other forms can access them.

What I should have done
I should have created an actual connection and used constructors to access it as a connection instead of reconnecting, but I do not know how to use constructors in Visual Basic .NET.
My Question
How do you work constructors in Visual Basic .NET? And how would you access them from other forms.

Going by C# to access it would be something (interpreted) like
VB.NET:
Dim Connection As New Form1.SavedConnection
Correct?

The connection
VB.NET:
Dim SQL As New Odbc.OdbcConnection(Connection_String)

Thanks in advance for any help :)
 
The code you've posted is exactly correct. However, it is not the .NET convention to create a single connection and leave it open continually. ADO.NET is designed to be disconnected, so your supposed to open and close connection when and as needed. Also, ADO.NET uses connection pooling by default so there is no advantage gained by using a single connection object throughout your app. It is completely appropriate to save a connection string to a global variable and then have distinct connection objects on each form that use that connection string. There are various ways that the Framework and the IDE can make this easier for you. You could create an application setting for the connection string and bind all your connections to it. That would mean that changing it for one connection would change it for all. That would also allow you to persist the string between sessions. Alternatively you could declare a public property in the ApplicationEvents.vb file and then access that as My.Application.SomeProperty throughout the project.

Finally, if you are building this connection string from data input by the user you should look into the OdbcConnectionStringBuilder class.
 
Thanks for your quick reply, I'am glad to see that I've done it correctly :)
 
Back
Top