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

Global Variable Ends Up as Nothing in Form

Hi All,

I'm an experienced VB6 developer and now starting (newbee) with VB 2008 and
I'm very excited.

Here's an issue I'm experiencing right off the starting line and cannot make
sense of it:

I have a module (Public) and in it contains a few global variables (String
data type). When I go to use these in a Form the value is "Nothing." Why is
this?
...
Public Module modMain

Public g_strMyPublicVar As String

Then in the form (frmMain), the g_strMyPublicVar does not contain the data
as it was set in modMain.
Any insight is greatly appreciated and I thank you in advance!

Kind regards - Fred
Nov 19 '08 #1
14 3635
On Nov 19, 4:28*pm, "Fred Block" <fblock_no_spamm...@w-systems.com>
wrote:
Hi All,

I'm an experienced VB6 developer and now starting (newbee) with VB 2008 and
I'm very excited.

Here's an issue I'm experiencing right off the starting line and cannot make
sense of it:

I have a module (Public) and in it contains a few global variables (String
data type). When I go to use these in a Form the value is "Nothing." Why is
this?
..
Public Module modMain

Public g_strMyPublicVar As String

Then in the form (frmMain), the g_strMyPublicVar does not contain the data
as it was set in modMain.

Any insight is greatly appreciated and I thank you in advance!

Kind regards - Fred
In your post, you didn't assign any value to "g_strMyPublicVar", thus
you may get a null refence exception.

If you do something like this, you won't get an exception.

' In your module, assign a value to "g_strMyPublicVar"
Public Module modMain
' eg: Assign value "foo"
Public g_strMyPublicVar As String = "foo"
End Module

or assign a value to "g_strMyPublicVar" in your form(frmMain) inside a
method:

' In your form, that'll work
Public Class Form1
Sub mymethod()
g_strMyPublicVar = "foo"
End Sub
End Class

' That won't work, if you assign value at class-level
' in your form
Public Class Form1
g_strMyPublicVar = "foo"
End Class

....And as you pointed, you are declaring it in your module so,
assigning a value to "g_strMyPublicVar" in your module should work
when you call it within your form.

Hope this helps,

Onur Güzel
Nov 19 '08 #2
On Nov 19, 9:28*am, "Fred Block" <fblock_no_spamm...@w-systems.com>
wrote:
Hi All,

I'm an experienced VB6 developer and now starting (newbee) with VB 2008 and
I'm very excited.

Here's an issue I'm experiencing right off the starting line and cannot make
sense of it:

I have a module (Public) and in it contains a few global variables (String
data type). When I go to use these in a Form the value is "Nothing." Why is
this?
..
Public Module modMain

Public g_strMyPublicVar As String

Then in the form (frmMain), the g_strMyPublicVar does not contain the data
as it was set in modMain.

Any insight is greatly appreciated and I thank you in advance!

Kind regards - Fred
What is the expected value? From what you posted the value should be
Nothing since nothing was assigned.

If you would like to set a value right off the bat, you could do the
following:

//////////
Public MyPublicVar As String = "please drop the hungarian
notation :-)"
/////////

I also hate modules (just my personal preference) and would recommend
you either switch to a static class or implement a singleton class to
expose the properties.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
Nov 19 '08 #3

Here are a couple of hints since you're new:

Stop using the Hungarian Notation.

Static (shared in VB.NET) variables might be a little better than Global
Variables. (Aka, stop with the VB6 global variable concept).

............
Since you're new, here is a "extra" for you, not related to your question:

http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!234.entry


"Fred Block" <fb****************@w-systems.comwrote in message
news:ug**************@TK2MSFTNGP06.phx.gbl...
Hi All,

I'm an experienced VB6 developer and now starting (newbee) with VB 2008
and I'm very excited.

Here's an issue I'm experiencing right off the starting line and cannot
make sense of it:

I have a module (Public) and in it contains a few global variables (String
data type). When I go to use these in a Form the value is "Nothing." Why
is this?
..
Public Module modMain

Public g_strMyPublicVar As String

Then in the form (frmMain), the g_strMyPublicVar does not contain the data
as it was set in modMain.
Any insight is greatly appreciated and I thank you in advance!

Kind regards - Fred


Nov 19 '08 #4
Hi Onur and thanks!

I found the issue to be that I "accidentally" had the same global variable
in two different modules. VB did not complain however which now puzzles me.

When the code ran I was trying to create a file in a path that did not exist
(System.IO.DirectoryNotFoundException)...

I was using: sw = New StreamWriter(strFile, True)

Is there a parameter I can use or some other way to have the path ctraed if
it does not exist?

Thanks again! - Fred

---------------------------------------
"kimiraikkonen" <ki*************@gmail.comwrote in message
news:26**********************************@v13g2000 pro.googlegroups.com...
On Nov 19, 4:28 pm, "Fred Block" <fblock_no_spamm...@w-systems.com>
wrote:
Hi All,

I'm an experienced VB6 developer and now starting (newbee) with VB 2008
and
I'm very excited.

Here's an issue I'm experiencing right off the starting line and cannot
make
sense of it:

I have a module (Public) and in it contains a few global variables (String
data type). When I go to use these in a Form the value is "Nothing." Why
is
this?
..
Public Module modMain

Public g_strMyPublicVar As String

Then in the form (frmMain), the g_strMyPublicVar does not contain the data
as it was set in modMain.

Any insight is greatly appreciated and I thank you in advance!

Kind regards - Fred
In your post, you didn't assign any value to "g_strMyPublicVar", thus
you may get a null refence exception.

If you do something like this, you won't get an exception.

' In your module, assign a value to "g_strMyPublicVar"
Public Module modMain
' eg: Assign value "foo"
Public g_strMyPublicVar As String = "foo"
End Module

or assign a value to "g_strMyPublicVar" in your form(frmMain) inside a
method:

' In your form, that'll work
Public Class Form1
Sub mymethod()
g_strMyPublicVar = "foo"
End Sub
End Class

' That won't work, if you assign value at class-level
' in your form
Public Class Form1
g_strMyPublicVar = "foo"
End Class

....And as you pointed, you are declaring it in your module so,
assigning a value to "g_strMyPublicVar" in your module should work
when you call it within your form.

Hope this helps,

Onur Güzel
Nov 19 '08 #5
On 2008-11-19, Fred Block <fb****************@w-systems.comwrote:
Hi Onur and thanks!

I found the issue to be that I "accidentally" had the same global variable
in two different modules. VB did not complain however which now puzzles me.
Well, actually, it's legal. The full name of a variable in a module is
essentilally, namespace.modulename.variablename. So, a variable in a
different module has a different name...

--
Tom Shelton
Nov 19 '08 #6
Variables. (Aka, stop with the VB6 global variable concept).

I dare to even go a step further,, in my code you wil not find anny shared
keywords
it is verry easy to just wrap all needed parameters in a parameter class and
pass this back and forward to constructors of classes that need these
parameters

for small projects it is enough to create a base class with the required
properties

regards

Michel

"sloan" <sl***@ipass.netschreef in bericht
news:uU**************@TK2MSFTNGP02.phx.gbl...
>
Here are a couple of hints since you're new:

Stop using the Hungarian Notation.

Static (shared in VB.NET) variables might be a little better than Global
Variables. (Aka, stop with the VB6 global variable concept).

...........
Since you're new, here is a "extra" for you, not related to your question:

http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!234.entry


"Fred Block" <fb****************@w-systems.comwrote in message
news:ug**************@TK2MSFTNGP06.phx.gbl...
>Hi All,

I'm an experienced VB6 developer and now starting (newbee) with VB 2008
and I'm very excited.

Here's an issue I'm experiencing right off the starting line and cannot
make sense of it:

I have a module (Public) and in it contains a few global variables
(String data type). When I go to use these in a Form the value is
"Nothing." Why is this?
..
Public Module modMain

Public g_strMyPublicVar As String

Then in the form (frmMain), the g_strMyPublicVar does not contain the
data as it was set in modMain.
Any insight is greatly appreciated and I thank you in advance!

Kind regards - Fred



Nov 19 '08 #7
On Nov 19, 7:11*pm, "Fred Block" <fblock_no_spamm...@w-systems.com>
wrote:
Hi Onur and thanks!
When the code ran I was trying to create a file in a path that did not exist
(System.IO.DirectoryNotFoundException)...

I was using: sw = New StreamWriter(strFile, True)

Is there a parameter I can use or some other way to have the path ctraed if
it does not exist?

Thanks again! - Fred
Hi Fred,
For the question about checking path, when you use StreamWriter's
constructor specifying a path, new file is created even the file does
not exist in the path that you passed. If it exists, it's
overwritten.

See here:
http://msdn.microsoft.com/en-us/library/36b035cb.aspx

But as you get DirectoryNotFound Exception, it means that file's
"directory" specified in StreamWriter's constructor, is invalid. You
need to enter correct directory, at least.

Additionaly, if you still need to check if a directory is available,
you can use:
"My.Computer.FileSystem.DirectoryExists" in a if-then conditional to
determine.

See it here:
http://msdn.microsoft.com/en-us/library/ax2x538z.aspx

Hope this helps,

Onur Güzel


Nov 19 '08 #8
On Nov 19, 12:34*pm, "Michel Posseth [MCP]" <M...@posseth.comwrote:
Variables. *(Aka, stop with the VB6 global variable concept).

I dare to even go a step further,, *in my code you wil not find anny shared
keywords
it is verry easy to just wrap all needed parameters in a parameter class and
pass this back and forward to constructors of classes that need these
parameters

for small projects it is enough to create a base class with the required
properties

regards

Michel

"sloan" <sl...@ipass.netschreef in berichtnews:uU**************@TK2MSFTNGP02.phx.gbl. ..
Here are a couple of hints since you're new:
Stop using the Hungarian Notation.
Static (shared in VB.NET) variables might be a little better than Global
Variables. *(Aka, stop with the VB6 global variable concept).
...........
Since you're new, here is a "extra" for you, not related to your question:
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!234.entry
"Fred Block" <fblock_no_spamm...@w-systems.comwrote in message
news:ug**************@TK2MSFTNGP06.phx.gbl...
Hi All,
I'm an experienced VB6 developer and now starting (newbee) with VB 2008
and I'm very excited.
Here's an issue I'm experiencing right off the starting line and cannot
make sense of it:
I have a module (Public) and in it contains a few global variables
(String data type). When I go to use these in a Form the value is
"Nothing." Why is this?
..
Public Module modMain
Public g_strMyPublicVar As String
Then in the form (frmMain), the g_strMyPublicVar does not contain the
data as it was set in modMain.
Any insight is greatly appreciated and I thank you in advance!
Kind regards - Fred
I agree, whenever plausible I try to put the properties into an
instanced property.

In many of the large applications I work on, I prefer to use a
constructor injection pattern and then have an IoC (inversion of
control) container shove a settings class into any class that requires
it. Takes a bit of time to get used to, but it is extremely powerful
once it's implemented.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
Nov 19 '08 #9
Thanks again Onur - this is exactly what I did to get it to work.

Regards - Fred

------------------------------------
"kimiraikkonen" <ki*************@gmail.comwrote in message
news:16**********************************@c22g2000 prc.googlegroups.com...
On Nov 19, 7:11 pm, "Fred Block" <fblock_no_spamm...@w-systems.com>
wrote:
Hi Onur and thanks!
When the code ran I was trying to create a file in a path that did not
exist
(System.IO.DirectoryNotFoundException)...

I was using: sw = New StreamWriter(strFile, True)

Is there a parameter I can use or some other way to have the path ctraed
if
it does not exist?

Thanks again! - Fred
Hi Fred,
For the question about checking path, when you use StreamWriter's
constructor specifying a path, new file is created even the file does
not exist in the path that you passed. If it exists, it's
overwritten.

See here:
http://msdn.microsoft.com/en-us/library/36b035cb.aspx

But as you get DirectoryNotFound Exception, it means that file's
"directory" specified in StreamWriter's constructor, is invalid. You
need to enter correct directory, at least.

Additionaly, if you still need to check if a directory is available,
you can use:
"My.Computer.FileSystem.DirectoryExists" in a if-then conditional to
determine.

See it here:
http://msdn.microsoft.com/en-us/library/ax2x538z.aspx

Hope this helps,

Onur Güzel

Nov 19 '08 #10
Hi Seth,

Why is hungarian notation not a suggested habit in .NET? Doesn;t it make
coding easier to read? If there is a newer scheme, please share this with
me. I'm very teachable and want to do this right the first time.

VB 2008 book suggestions?

Thanks in advance to you Seth (and all others answering my thread).

Regards - Fred

------------------------------------------------

//////////
Public MyPublicVar As String = "please drop the hungarian
notation :-)"
/////////

I also hate modules (just my personal preference) and would recommend
you either switch to a static class or implement a singleton class to
expose the properties.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
Nov 19 '08 #11


I agree. Using a small parameter-wrapper class is the way to go. It takes
disjoint properties and adds some cohesion to them.
One idea that a former colleague showed me...was to take this small wrapper
class...and put it into a "DataStore" (for a winforms app, it was basically
a singleton where you could keep/add/remove some objects).

The main form would create and put the small parameter-wrapper class into
the DataStore (using a unique key).

The DataStore would pull the small-wrapper-parameter class (actually remove
it at the same time)....using the same unique key.

This way.....the form could be instantiated from anywhere. Just "put into
the DataStore", then call the form.

Either way is good, just throwing a different nugget out there.

"Michel Posseth [MCP]" <MS**@posseth.comwrote in message
news:e4**************@TK2MSFTNGP02.phx.gbl...
>Variables. (Aka, stop with the VB6 global variable concept).

I dare to even go a step further,, in my code you wil not find anny
shared keywords
it is verry easy to just wrap all needed parameters in a parameter class
and pass this back and forward to constructors of classes that need these
parameters

for small projects it is enough to create a base class with the required
properties

regards

Michel

"sloan" <sl***@ipass.netschreef in bericht
news:uU**************@TK2MSFTNGP02.phx.gbl...
>>
Here are a couple of hints since you're new:

Stop using the Hungarian Notation.

Static (shared in VB.NET) variables might be a little better than Global
Variables. (Aka, stop with the VB6 global variable concept).

...........
Since you're new, here is a "extra" for you, not related to your
question:

http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!234.entry


"Fred Block" <fb****************@w-systems.comwrote in message
news:ug**************@TK2MSFTNGP06.phx.gbl...
>>Hi All,

I'm an experienced VB6 developer and now starting (newbee) with VB 2008
and I'm very excited.

Here's an issue I'm experiencing right off the starting line and cannot
make sense of it:

I have a module (Public) and in it contains a few global variables
(String data type). When I go to use these in a Form the value is
"Nothing." Why is this?
..
Public Module modMain

Public g_strMyPublicVar As String

Then in the form (frmMain), the g_strMyPublicVar does not contain the
data as it was set in modMain.
Any insight is greatly appreciated and I thank you in advance!

Kind regards - Fred




Nov 19 '08 #12
Why is hungarian notation not a suggested habit in .NET? Doesn;t it make
coding easier to read? If there is a newer scheme, please share this with
me. I'm very teachable and want to do this right the first time.
http://en.wikipedia.org/wiki/Hungari...tems_Hungarian

I prefer to just use names that make sense, no special notation at
all. If your methods are short and concise and have a single
responsibility, most likely there will be few variables that are
easily understood by assignments, and special notations aren't
required. I am also an advocate against using abbreviations in
variable names, as they hurt readability (unless they are truly
accepted acronyms like "guid" for globally unique identifier).

Most times I end up using the type name with a lowercase first letter,
unless a more specific name is required.

Dim notificationService As New NotificationService()
Dim dataStore As IDataStore = GetCustomerDataStore()
Dim message As String = "The name 'string' wouldn't make sense here"

Later versions of .NET added in implicit typing which can shorten
these declarations. However due to the confusion between implicitly
typed variables and the rubbish done in VBScript, I dislike it. I much
prefer the C# approach of using the 'var' keyword. Below are the same
samples (if you desire to use the feature)

Dim notificationService = New NotificationService()
Dim dataStore = GetCustomerDataStore()
Dim message = "I love putting messages inside strings"
VB 2008 book suggestions?
Book recommendations have never been my strong suit, mainly because I
don't read many (shame, shame I know). My recommendations would be to
hit the library and see if they have any, or even better try to find a
local .NET university or other training event. Being involved in the
community is the best way to learn.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
Nov 19 '08 #13
My recommendations would be to
hit the library and see if they have any, or even better try to find a
local .NET university or other training event.
I should clarify, I didn't mean going to a University and taking a
college class, .NET universities are sessions about new Microsoft
features etc:

http://www.dotnet-u.com/

Not sure if they have any near you, but it's worth checking out. You
might also look up your local Microsoft Developer Evangelists and
contact them for upcoming events (USA thing only I believe, so it
might not be applicable to you):

http://msdn.microsoft.com/en-us/bb905078.aspx

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/

Nov 19 '08 #14
Hi Seth and thanks again!

Point taken and I found just about all I needed in the MSDN I installed here
on the topic. This all makes sense and before I get ANY further with this
first .NET project I've taken on, I'll undo the old VB6 notations I was
using. A lot has changed!

Regards - Fred
-----------------------
"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:ac**********************************@s9g2000v bp.googlegroups.com...
>Why is hungarian notation not a suggested habit in .NET? Doesn;t it make
coding easier to read? If there is a newer scheme, please share this with
me. I'm very teachable and want to do this right the first time.

http://en.wikipedia.org/wiki/Hungari...tems_Hungarian

I prefer to just use names that make sense, no special notation at
all. If your methods are short and concise and have a single
responsibility, most likely there will be few variables that are
easily understood by assignments, and special notations aren't
required. I am also an advocate against using abbreviations in
variable names, as they hurt readability (unless they are truly
accepted acronyms like "guid" for globally unique identifier).

Most times I end up using the type name with a lowercase first letter,
unless a more specific name is required.

Dim notificationService As New NotificationService()
Dim dataStore As IDataStore = GetCustomerDataStore()
Dim message As String = "The name 'string' wouldn't make sense here"

Later versions of .NET added in implicit typing which can shorten
these declarations. However due to the confusion between implicitly
typed variables and the rubbish done in VBScript, I dislike it. I much
prefer the C# approach of using the 'var' keyword. Below are the same
samples (if you desire to use the feature)

Dim notificationService = New NotificationService()
Dim dataStore = GetCustomerDataStore()
Dim message = "I love putting messages inside strings"
>VB 2008 book suggestions?

Book recommendations have never been my strong suit, mainly because I
don't read many (shame, shame I know). My recommendations would be to
hit the library and see if they have any, or even better try to find a
local .NET university or other training event. Being involved in the
community is the best way to learn.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/


Nov 19 '08 #15

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

Similar topics

4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
3
by: Phil Lamey | last post by:
Hi All, I have the following code but for some reason I cannot get the Session_OnEnd event to fire. I am trying to limit the amount of connections a browser session can have. Where the...
6
by: Soha | last post by:
I'm a beginner in using javascript and i need a reply so urgent; i have a problem in using javascript with asp the problem is that i want to define a global variable and then change the value of it...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
1
by: Rob Wire | last post by:
Please let me know the preferred way to do the following. Accept a Variable in the calling page's URL of an ID. Set that variable to be global scope of the class so that it can be used throughout...
41
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query =...
19
by: furiousmojo | last post by:
This is a strange problem. I have a project where the contents of global.asax application_error are not firing. It is an asp.net 2.0 application using web application projects. I have another...
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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
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.