473,795 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_strMyPublicVa r As String

Then in the form (frmMain), the g_strMyPublicVa r 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 3675
On Nov 19, 4:28*pm, "Fred Block" <fblock_no_spam m...@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_strMyPublicVa r As String

Then in the form (frmMain), the g_strMyPublicVa r 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_strMyPublicV ar", 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_strMyPublicV ar"
Public Module modMain
' eg: Assign value "foo"
Public g_strMyPublicVa r As String = "foo"
End Module

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

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

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

....And as you pointed, you are declaring it in your module so,
assigning a value to "g_strMyPublicV ar" 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_spam m...@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_strMyPublicVa r As String

Then in the form (frmMain), the g_strMyPublicVa r 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.space s.live.com/Blog/cns!A68482B9628 A842A!234.entry


"Fred Block" <fb************ ****@w-systems.comwrot e in message
news:ug******** ******@TK2MSFTN GP06.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_strMyPublicVa r As String

Then in the form (frmMain), the g_strMyPublicVa r 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 "accidental ly" 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.Dire ctoryNotFoundEx ception)...

I was using: sw = New StreamWriter(st rFile, 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

---------------------------------------
"kimiraikko nen" <ki************ *@gmail.comwrot e in message
news:26******** *************** ***********@v13 g2000pro.google groups.com...
On Nov 19, 4:28 pm, "Fred Block" <fblock_no_spam m...@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_strMyPublicVa r As String

Then in the form (frmMain), the g_strMyPublicVa r 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_strMyPublicV ar", 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_strMyPublicV ar"
Public Module modMain
' eg: Assign value "foo"
Public g_strMyPublicVa r As String = "foo"
End Module

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

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

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

....And as you pointed, you are declaring it in your module so,
assigning a value to "g_strMyPublicV ar" 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.comwrot e:
Hi Onur and thanks!

I found the issue to be that I "accidental ly" 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.modul ename.variablen ame. 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.ne tschreef in bericht
news:uU******** ******@TK2MSFTN GP02.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.space s.live.com/Blog/cns!A68482B9628 A842A!234.entry


"Fred Block" <fb************ ****@w-systems.comwrot e in message
news:ug******** ******@TK2MSFTN GP06.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_strMyPublicVa r As String

Then in the form (frmMain), the g_strMyPublicVa r 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_spam m...@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.Dire ctoryNotFoundEx ception)...

I was using: sw = New StreamWriter(st rFile, 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 DirectoryNotFou nd 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.Fi leSystem.Direct oryExists" 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.c omwrote:
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.ne tschreef in berichtnews:uU* *************@T K2MSFTNGP02.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.space s.live.com/Blog/cns!A68482B9628 A842A!234.entry
"Fred Block" <fblock_no_spam m...@w-systems.comwrot e in message
news:ug******** ******@TK2MSFTN GP06.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_strMyPublicVa r As String
Then in the form (frmMain), the g_strMyPublicVa r 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

------------------------------------
"kimiraikko nen" <ki************ *@gmail.comwrot e in message
news:16******** *************** ***********@c22 g2000prc.google groups.com...
On Nov 19, 7:11 pm, "Fred Block" <fblock_no_spam m...@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.Dire ctoryNotFoundEx ception)...

I was using: sw = New StreamWriter(st rFile, 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 DirectoryNotFou nd 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.Fi leSystem.Direct oryExists" 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

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

Similar topics

4
24187
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(); function loadUrlVariables() { varString = location.search;
3
515
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 application is a virtual directory. Any ideas? ------------
6
3951
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 after clicking on a link which activate a function that changes the global variable. this variable i use as the value of a hidden field in a form. my problem is that the global variable dosen't change. this is the code i used to do this: ...
17
5634
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 constantly running tests on imperfect code. On of the cumbersome jobs encountered is reassigning global vars their values after a close encounter with an untrapped runtime error. Rather than writing a procedure to simply reassign them all with a...
33
3052
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 been for years. If the machine has memory to spare and windows can use it - I'm thinking "Why not?" I was wondering what some of you have to say about that, particularly any severe "gotchas" you've had the unfortunate experience to contend with.
1
3695
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 the functions from Page Load. Would like to send the ID variable as a SQL parameter to stored procedures for Select, Update, and Insert. What I have done so far to get around it is declare it in the SQL Select statements of the Web form code,...
41
10679
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 = String.Format("SELECT * FROM dbo.documents WHERE ") & query End Sub
19
10215
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 app using web application projects and it's firing fine but it was upgraded from the 1.1 framework. Why doesn't my global.asax application_error routine fire?
20
12763
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 AutoExec macro calls a function "InitApplication" which, in turn, calls a function to set the value of a global string variable
0
9673
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
9522
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
10002
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
9044
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...
1
7543
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
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
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.