473,403 Members | 2,071 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,403 software developers and data experts.

Module Best Practices

I will be creating multiple VB.NET applications. I want all of the
applications to have some specific global user-defined public constants
and public variables (or objects). I currently have them in a module.
It works and I can easily reuse that module in each of my applications.
But then developers are able to change the module within a specific
application without changing it in the other applications. I want them
to all be the same.

What is the best way to handle this?

Nov 21 '05 #1
7 3119
We too are creating multiple VB.NET applications. We have created a
single library of shared routines (in a dll) that includes all such
constants. The developers reference the dll which cannot be changed.
This does mean distributing a dll but it allows for creation of a nice
library of custom reusable objects.

Tom
Paul wrote:
I will be creating multiple VB.NET applications. I want all of the
applications to have some specific global user-defined public constants
and public variables (or objects). I currently have them in a module.
It works and I can easily reuse that module in each of my applications.
But then developers are able to change the module within a specific
application without changing it in the other applications. I want them
to all be the same.

What is the best way to handle this?

Nov 21 '05 #2
Paul,
I would limit the use of Modules, instead rely on Classes with Shared
members.

Classes with Shared members provide better "encapsulation", as you need to
qualify the constant/variable with the name of the class to use it, this way
you know exactly where the constant/variable is coming from...

To share Modules or Classes between projects, I would put the Module or
Class in a Class Library & reference the Class Library in each of the
projects.

http://msdn.microsoft.com/library/de...ryTemplate.asp

NOTE: With a class library each instance of each of your apps will have
their own copy of the variables in the class library. In other words the
variables will not be shared across applications.

Hope this helps
Jay

"Paul" <pw****@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
|I will be creating multiple VB.NET applications. I want all of the
| applications to have some specific global user-defined public constants
| and public variables (or objects). I currently have them in a module.
| It works and I can easily reuse that module in each of my applications.
| But then developers are able to change the module within a specific
| application without changing it in the other applications. I want them
| to all be the same.
|
| What is the best way to handle this?
|
Nov 21 '05 #3
I agree that you want to limit (or not use) modules. However, I was
trying to have user-defined constants without having to instantiate the
class that the constants reside in. Is that possible?

Also, if you want all your applications to first run a Sub Main, you
must have a module that contains the Sub Main, right? Or is there a
way to do that without a module?

Nov 21 '05 #4
I figured it out. I have a base class with the constants, etc. that I
need. Then every form I create will inherit that base class.

Nov 21 '05 #5
Paul wrote:
I figured it out. I have a base class with the constants, etc. that I
need. Then every form I create will inherit that base class.


Show me? I'm the slow kid on the block (but would like to do something
like what you're doing!).

Thanks!

--
- Mitchell Vincent
- kBilling - Invoices Made Easy!
- http://www.k-billing.com
Nov 21 '05 #6
Sure...now I have not tested it enough to know if this is going to work
as well as I want it to, but so far it is. Let me tell you what I am
doing first. That will help explain why I am doing all this.

I'm converting about 20 Microsoft Access applications to VB.NET.
Access has numerous constants (like acViewNormal), commands (like
DoCmd, SysCmd), enumerations, etc. that I will be converting to
something in VB.NET.

At first I created a public Sub in a module called DoCmdOpenForm (for
the DoCmd.OpenForm command in Access). And there were others. Also,
in this module I had created the Access constants. But I didn't want
to use a module.

So I created a class called AccessPublic. It inherits from
System.Windows.Forms.Form. Then within the class I declared all the
Public constants that I need. I also have a second class called
DoCmdObject. This second class contains the Subs that the Access DoCmd
executes like OpenForm. So in my AccessPublic class I also
instantiated a new DoCmd object with the following code:

Public DoCmd as New DoCmdObject

Now, all my application forms inherit from the AccessPublic class with
the following code:

Inherits AccessPublic

Now all my constants and the DoCmd code written in Access will work
just like they do in my .NET applications with very little conversion
needed. Here is an example:

Public Class Form1
Inherits AccessPublic

+ Windows Form Designer Generated Code

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.OpenArgs = "Add"
MessageBox.Show("acViewNormal: " & acViewNormal)
DoCmd.OpenForm("FormName")
SysCmd()
End Sub
End Class

I do not get any syntax errors. Now I just need to put code into the
DoCmd (and other) subs. I created a second form (Form2) exactly like
Form1 because I wondered if the Public definition of the constants
would conflict. But they do not. So far it's working great!

Hope that helps.

Nov 21 '05 #7
Paul,
Yes, as I mentioned in my first response, make the constants & variables
(the "members") Shared

BTW: Const are by their very nature Shared.

Public NotInheritable Class MyConstants

Public Const ProgId As String = "My Program Id"

Public Shared Readonly Name As String = "Jay"

Public Shared Sub DoWork()
End Sub

Private Sub New()
End Sub

End Class

You would then use MyConstants.Name, MyConstants.ProgId, MyConstants.DoWork
to get at the individual members. The Shared keyword allows you to use the
member without instanciating the class.

http://msdn.microsoft.com/library/de...akeyShared.asp

If I make all the members (subs, functions, fields, properties, events)
shared, then I also mark the class as NotInheritable which prevents other
from inheriting from this class, and add a Private Sub New which prevents
others from instantiating an object of the class. You can use Shared for
classes that are instantiated also, so Notinheritable or Private Sub New may
or may not be used in that case. For example a Parse or From* method, that
creates a new instance of class based on processing some data.
Note: you can use a variable to access a Shared member, however this can
lead to misleading code, such as Thread.Sleep.

Dim aThread As New Thread(...)

aThread.Sleep(100)

It appears in the above that aThread will sleep, while in actuality the
current thread will sleep, as Thread.Sleep is shared method that acts on
Thread.CurrentThread.

Hope this helps
Jay

"Paul" <pw****@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
|I agree that you want to limit (or not use) modules. However, I was
| trying to have user-defined constants without having to instantiate the
| class that the constants reside in. Is that possible?
|
| Also, if you want all your applications to first run a Sub Main, you
| must have a module that contains the Sub Main, right? Or is there a
| way to do that without a module?
|
Nov 21 '05 #8

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

Similar topics

10
by: Steven Reddie | last post by:
Hi, I want to do something like the following, which doesn't work: modulename = 'module' import modulename The error is that there is no module named 'modulename'. Is there a way to get...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
3
by: John Dalberg | last post by:
I am looking for an ASP.NET application on CodePlex which exemplifies best practices for the following: - Use of interfaces - Seperation of the UI, business and data tiers - Data Tier that uses...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.