Public Variables in Module1

G

Guest

Very simple question.

I have a project with multiple forms and a Module1. In Module1 I have
written the connectionstring:

Module Module1
Public connectionstring = "provider=Microsoft.Jet.OLEDB.4.0;data source=" +
Application.StartupPath + "\mydatabase.mdb;"

End Module

In Form I am using this "connectionstring" form.

Ok..now here is the question. How come this variable is available
(automatically) in other forms even though I haven't imported Module1 in
those forms?

Thank you in advance.
 
A

Adam Goossens

Hi patang,

Have a read of this blog entry I wrote that goes over this:

http://agoossens.blogspot.com/2005_04_01_agoossens_archive.html

Check the paragraph titled "Members of a module are scoped to the
surrounding namespace of their module."

I've been having issues with blogger lately so I've given up any form of
blogging :p

Members of modules are scoped to their surrounding namespace. So, as
long as you import the namespace of the module you can access it's
members automatically.

"Under-the-hood" the VB.NET compiler applies the StandardModule
attribute to your Module. When you compile code that calls the members
of this module, the VB.NET compiler automatically replaces them with
their properly qualified versions.

IE, this:
 
C

Cor Ligthert

Patang,

A form is just a class in your project what is instanced as one or more
objects. You can use one form class to create more form objects.

A module is an forever shared class in your project. The name for shared in
C derived languages is static.

The shared declared members are every where usable in a project and will not
been destroyed.

I hope this helps,

Cor
 
H

Herfried K. Wagner [MVP]

patang said:
Module Module1
Public connectionstring = "provider=Microsoft.Jet.OLEDB.4.0;data source="
+
Application.StartupPath + "\mydatabase.mdb;"

In addition to the other replies: It's recommended to use the '&' operator
for string concatenations in VB.NET. 'System.IO.Path.Combine' can be used
to combine path and file name.
 

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