473,395 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Concept ? about Modules vs Classes

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

Nov 21 '05 #1
8 1147
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

"Bruce D" <br*************@hotmail.com> escribió en el mensaje
news:11*************@corp.supernews.com...
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

Nov 21 '05 #2
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
Nov 21 '05 #3
Hello;

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

TIA
"Bruce D" <br*************@hotmail.com> a écrit dans le message de news:
11*************@corp.supernews.com...
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

Nov 21 '05 #4
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:Oq*************@TK2MSFTNGP15.phx.gbl...
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

Nov 21 '05 #5
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.

"Bruce D" wrote:
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

Nov 21 '05 #6
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
Nov 21 '05 #7
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
Nov 21 '05 #8
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

On Thu, 24 Feb 2005 09:55:01 -0800, "David Anton"
<Da********@discussions.microsoft.com> wrote:
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.

"Bruce D" wrote:
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


Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Brad Tilley | last post by:
I don't understand classes very well... maybe some day. Is it just me or are they supposed to be difficult to understand? They make my head hurt. Anyway, because I don't understand classes well,...
8
by: Rob Snyder | last post by:
Greetings - I have a situation where I need to be able to have a Python function that will take all the modules in a given directory and find all the classes defined in these modules by name....
7
by: Jorgen Grahn | last post by:
I have a set of tests in different modules: test_foo.py, test_bar.py and so on. All of these use the simplest possible internal layout: a number of classes containing test*() methods, and the good...
31
by: N.Davis | last post by:
I am very new to Python, but have done plenty of development in C++ and Java. One thing I find weird about python is the idea of a module. Why is this needed when there are already the ideas of...
9
by: MLH | last post by:
I need a fundamental explanation of Class Modules - something suitable for newbies. Access 2.0 didn't seem to focus on them very much. Now that I'm using Access 97, it seems they're everywhere. thx...
5
by: Erik Cruz | last post by:
Hello! I have read some threads discussing the fact that a module is in reality a shared class. If I try to create a Public Shared Class in vb.net I receive a compile error. Why? If I can't...
5
by: Alex | last post by:
Hi, this is my first mail to the list so please correct me if Ive done anything wrong. What Im trying to figure out is a good way to organise my code. One class per .py file is a system I like,...
173
by: Zytan | last post by:
I've read the docs on this, but one thing was left unclear. It seems as though a Module does not have to be fully qualified. Is this the case? I have source that apparently shows this. Are...
3
by: MK | last post by:
Hi, I'm currently looking into redesigning existing VB6 modules before I port them to VB.NET and would like oppinions of whether to go with classes or seperate class libraries (dlls). There...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.