且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

全局变量在形式上没有任何结果

更新时间:2022-11-06 15:10:50

On 11月19日,4:28 * pm,Fred Block < fblock_no_spamm ... @ w-systems.com>

写道:

大家好,


我是一位经验丰富的VB6开发人员,现在用VB 2008开始(newbee)和

我很兴奋。


这里这是一个问题我正在经历起跑线并且不能让它感觉到它的价值:


我有一个模块(公共)和在其中包含一些全局变量(String

数据类型)。当我在表单中使用这些时,值为Nothing。为什么

这个?

..

公共模块modMain


Public g_strMyPublicVar As String


然后在表格(frmMain)中,g_strMyPublicVar不包含数据

,因为它是在modMain中设置的。


非常感谢任何见解,我提前感谢你!


亲切的问候 - Fred



在你的帖子中,你没有为g_strMyPublicVar指定任何值,因此

你可能会得到一个空的refence例外。


如果你做这样的事情,你不会得到例外。


''在你的模块中,为g_strMyPublicVar指定一个值

公共模块modMain

''例如:分配值foo

Public g_strMyPublicVar As String =" foo"

结束模块


或为g_strMyPublicVar赋值在你的形式(frmMain)里面的

方法:


''在你的表格中,那就行了

公开Class Form1

Sub mymethod()

g_strMyPublicVar =" foo"

End Sub

End Class


''如果你在课堂上分配价值,那就不行了。

''在你的表格中

公共课Form1

g_strMyPublicVar =" foo"

结束班级


....正如你指出的那样,你宣布它在你的模块中,

为g_strMyPublicVar赋值在你的模块中应该工作

当你在表单中调用它。


希望这有帮助,


Onur Güzel


11月19日,9:28 * am,Fred Block < fblock_no_spamm ... @ w-systems.com>

写道:

大家好,


我是一位经验丰富的VB6开发人员,现在用VB 2008开始(newbee)和

我很兴奋。


这里这是一个问题我正在经历起跑线并且不能让它感觉到它的价值:


我有一个模块(公共)和在其中包含一些全局变量(String

数据类型)。当我在表单中使用这些时,值为Nothing。为什么

这个?

..

公共模块modMain


Public g_strMyPublicVar As String


然后在表格(frmMain)中,g_strMyPublicVar不包含数据

,因为它是在modMain中设置的。


非常感谢任何见解,我提前谢谢你!


亲切的问候 - Fred



什么是期望值?从你发布的价值应该是

没有任何东西,因为没有分配。


如果你想立即设置一个值,你可以做

以下:


//////////

Public MyPublicVar As String =" please drop匈牙利语

符号: - )"

/////////


我也讨厌模块(只是我个人的偏好)并建议您转换为静态类或实现单例类来实现

公开属性。


谢谢,


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




以下是您提供的一些提示新的:


停止使用匈牙利表示法。


静态(在VB.NET***享)变量可能比Global更好一点

变量。 (Aka,停止使用VB6全局变量概念)。


............

因为你是新的,这是一个额外的对你而言,与你的问题无关:


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



" Fred Block" < fb **************** @ w-systems.comwrote in message

news:ug ************ ** @ TK2MSFTNGP06.phx.gbl ...

大家好,


我是一位经验丰富的VB6开发人员现在用VB 2008开始(newbee)

我很兴奋。


这是一个问题我正在经历起跑线并不能理解它:


我有一个模块(公共),其中包含一些全局变量(String

数据类型)。当我在表单中使用这些时,值为Nothing。为什么

是这个?

..

公共模块modMain


Public g_strMyPublicVar As String


然后在表格(frmMain)中,g_strMyPublicVar不包含数据

,因为它是在modMain中设置的。


非常感谢任何见解,我提前感谢您!


亲切的问候 - 弗雷德



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

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


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/



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