473,625 Members | 3,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1170
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.supe rnews.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.MyConnec tion

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.getda ta(strSQL)

' code in class
Public Class DataClass
Shared Function getdata(ByVal tSQLString As String) As DataSet
' create DSN connection
Dim _strConn As String = "DSN=mysqlLockB ox"
Dim _connODBC As OdbcConnection = New OdbcConnection
Dim _ds1 As New DataSet
Dim _da1 As OdbcDataAdapter
_connODBC.Conne ctionString = _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******** *****@TK2MSFTNG P15.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.MyConnec tion

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.getda ta(dsFinders, strSQL,TableNam e)

' 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=mysqlLockB ox"
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.Dispo se()
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********@dis cussions.micros oft.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
NotInheritab le 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
1579
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, I avoid using them. However, many modules in the standard library are implemented as classes: sgmllib, HTMLParser, etc. Is it possible to use these modules without getting into OO programming and inheritance and all the other lofty, theoretical...
8
1638
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. Ultimately, I would need to loop through all of these, instantiating each of the classes in turn and calling a pre-known method of each. Finding the name of each module is not a problem, but loading the classes out the module once I know the name...
7
2065
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 old lines at the end: if __name__ == "__main__": unittest.main() This is great, because each of the modules are runnable, and I can select classes or tests to run on the commandline if I want to. However, running all the tests from e.g. a...
31
2516
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 class, file, and package? To my mind, although one CAN put many classes in a file, it is better to put one class per file, for readability and maintainability. One can then create packages and libraries, using groups of files, one
9
1995
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 N advance.
5
11587
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 explicitly create a shared class, how does vb.net creates it? TIA, Erik Cruz
5
1651
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, keeps stuff apart. If I do that, I usually name the .py file to the same as the class in it. File: Foo.py *********************** class Foo:
173
5658
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 modules left-over from VB6, and not much used anymore? It seems that it is better to require Imports or use fully qualified names for functions in other classes/modules, but a Module doesn't require this, cluttering the global namespace. It seems...
3
1107
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 are currently 297 existing modules in the application I'm working on. The majority are an interface to middleware services, passing data back and forth from the front end. Any design suggestions for porting to VB.NET is much appreciated.
0
8256
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8189
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8635
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8356
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8497
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7184
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4193
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1500
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.