Concept ? about Modules vs Classes

B

Bruce D

I'm developing my first VB.NET application and I want to use good coding
technique, but I'm a little confused about 'modules and 'classes'. I have
several windows forms that have to connect to my data (MySQL). During my
testing, I created a ODBCconnection object via code (I didn't want to use
the windows form object because I'm going to want to use this from other
forms)...and all was great. So, now I need to put that code in a place
where I can call it from other areas of my program...and that's where I
become confused on where I should store it and how I should store it.
First I added a new 'module' to my project and created a public sub...but I
can't seem to reference that from my windows form.
Second, I added a new 'class' to my project. This worked, but I thought it
was weird that I had to instantiate the object before calling the code.
Long story short...my question is how should I be storing this small bit of
code: module / class / something else??

TIA
-bruce duncan
 
C

Carlos J. Quintero [.NET MVP]

IMO, it is better to avoid using global objects. I prefer to put it in the
main class of my app (say, the main form, for example), make it accessible
through a Get property and pass it to whoever method needs it.

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com
 
C

Cor Ligthert

Bruce,

The use of modules is almost a compatible VB6 and older situation (not
forever however where you do it, it is)

You can make shared or non shared classes.

In what you are doing it can be a shared class, which clearly depends how
you use that dataclass.

Public Class MyDataHanding
Public Shared Sub MyConnection
bla bla
End sub
End Class

Now you can do everywhere in your program
MyDataHandling.MyConnection.

When that "shared" is not in the syntax, than it becomes
dim myTemp = New MyDataHandling
myTemp.MyConnection

Therefore this is everytime a new one, which will (when you do it well) be
automaticly everytime destroyed by the Garbage Collector

By the way, why you use ODBC, there is in dotNet not any advantage for that
while there are a lot of disadvanteges. As well be aware that you can have
open only one datareader (which is used by the dataadapter) at a time in a
program.

It is also advices to dispose everytime your connection when you have done
your reading or writing.

I hope this helps?

Cor
 
X

xavier

Hello;

Just to comment, did you try the MySqlConnector, it's very good for
..net. It's like SQLConnector ! ! !

TIA
 
B

Bruce D

Cor,
Thanks for the insight! You mentioned that ODBC might have
disadvantages...hmm...I going to start reading more. But for now, here is
my code. Is this bad practice in .NET?

' code if form
Dim dsFinders As New DataSet
dsFinders = DataClass.getdata(strSQL)

' code in class
Public Class DataClass
Shared Function getdata(ByVal tSQLString As String) As DataSet
' create DSN connection
Dim _strConn As String = "DSN=mysqlLockBox"
Dim _connODBC As OdbcConnection = New OdbcConnection
Dim _ds1 As New DataSet
Dim _da1 As OdbcDataAdapter
_connODBC.ConnectionString = _strConn
' create a data adapter
_da1 = New OdbcDataAdapter(tSQLString, _connODBC)
' fill dataset
_da1.Fill(_ds1, "finders")
' clean up
_da1 = Nothing
_connODBC.Close()
Return _ds1
End Function
End Class

Thanks again!
-bruce
 
G

Guest

A module is actually equivalent to a Friend NotInheritable class with only
shared members (and can't be instantiated). It is actually appropriate to
use anywhere you want to group a certain type of functionality not based on
object instances. It is not very OO, but then neither are Friend
NotInheritable classes with only shared members.

You'll need to qualify the call to the sub with the module name. You would
call in exactly the same way for a shared method in a regular class (i.e.,
qualify the call with the class name).

David Anton
Tangible Software Solutions
www.tangiblesoftwaresolutions.com
Home of the Instant VB C# to VB.NET converter and the Instant C# VB.NET to
C# converter.
 
C

Cor Ligthert

Bruce,

I saw to late that it was *mySQL* (and not your SQL), for that I am not sure
if OleDB is the best choise. Sorry about that sentence.

Your code is almost the same (not exact) as I use it in a lot of programs.
I changed some things what is in *my* opinion better.

The same as more more active persons in this newsgroup do I hate that
underscore convention by the way. I do not see any reason for that, do you?

The most suprised you wil probably be that I changed the function for a Sub.
Keep in mind that in dotNet for every reference type (every not value type)
only the reference will be passed to a method.

\\\
Dim dsFinders As New DataSet
DataClass.getdata(dsFinders, strSQL,TableName)

' code in class
Public Class DataClass
Public Shared Sub getdata(ByVal ds as Dataset, ByVal SQLString As
String, ByVal TableName as String)
' create DSN connection
Dim _strConn As String = "DSN=mysqlLockBox"
Dim _connODBC As OdbcConnection = New OdbcConnection(_strConn)
' create a data adapter
Dim _da1 As New OdbcDataAdapter(SQLString, _connODBC)
' fill dataset
Try
_da1.Fill(ds, TableName)
Catch ex as Exception
'Error handling now just a messagebox
Messagebox.Show(ex.ToString)
Finally
_connODBC.Dispose()
End Try
End Sub
End Class
///

This above is not tested just typed in this message

I hope this helps?

Cor
 
C

Cor Ligthert

Bruce,

You can see I typed it only in this message, this is surely a typing
mistake.
Dim _connODBC As OdbcConnection = New OdbcConnection(_strConn)
Dim _connODBC As New OdbcConnection(_strConn)

I am glad I see it before Herfried, I did this one some weeks ago as well,
changing text and than not completing.

Cor
 
J

J L

David,
If there is no naming conflict with the sub, I believe you do not have
to qualify with the module name. Is that correct?

e.g. Module1 has sub MyMessage

I can call MyMessage from any form in my project without referencing
Module1.

TIA,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top