静态类常量(VB 2005) [英] Static Class Constants (VB 2005)

查看:68
本文介绍了静态类常量(VB 2005)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在一个类中创建一个常量,可以和实例化的对象一起使用,也可以不使用

。例如:


dim ClassConst as string = myClass.ConstantString


dim myObj = new MyClass

ClassConst = myObj.ConstantString


在课堂内MyClass


Class MyClass


public const ConstantString =" ;一些常量字符串


结束类


在Beta 2中,ConstantString可用于上述任何一种情况而无需

投诉。在RTM版本中,我不能同时做到这两点。我不想关闭

警告,因为这样做可能会在我的代码中实际引入另一个错误

,因为我没有看到警告。


谢谢,

迈克。

Is there any way to create a constant in a class that can be used both with
an instantiated object and without. For example:

dim ClassConst as string = myClass.ConstantString

dim myObj = new MyClass
ClassConst = myObj.ConstantString

Inside the class MyClass

Class MyClass

public const ConstantString = "Some Constant String"

End Class

In Beta 2, ConstantString was available in either of the above cases without
complaint. In the RTM version, I can''t do both. I don''t want to turn off
the warning as doing so may actually introduce another bug in my code later
because I didn''t see the warning.

Thanks,
Mike.

推荐答案

如果是''常量,为什么不总是通过类型名称引用它?什么

你通过实例访问它获得了什么?


Michael D. Ober < obermd @ alum.mit.edu.nospam>在消息中写道

新闻:uj ************* @ TK2MSFTNGP14.phx.gbl ...
If it''s a constant, why not always reference it through the type name? What
do you gain by accessing it through an instance?

"Michael D. Ober" <obermd.@.alum.mit.edu.nospam> wrote in message
news:uj*************@TK2MSFTNGP14.phx.gbl...
有什么方法可以创建类中的常量,既可以用于实例化对象,也可以不用。例如:

dim ClassConst as string = myClass.ConstantString
dim myObj = new MyClass
ClassConst = myObj.ConstantString

里面MyClass类

类MyClass

public const ConstantString =" Some Constant String"

End Class

Beta 2,ConstantString可用于以上任何一种情况
没有
投诉。在RTM版本中,我不能同时做到这两点。我不想关闭
警告,因为这样做可能会在我的代码中实际引入另一个错误
,因为我没有看到警告。
<谢谢,
迈克。
Is there any way to create a constant in a class that can be used both
with
an instantiated object and without. For example:

dim ClassConst as string = myClass.ConstantString

dim myObj = new MyClass
ClassConst = myObj.ConstantString

Inside the class MyClass

Class MyClass

public const ConstantString = "Some Constant String"

End Class

In Beta 2, ConstantString was available in either of the above cases
without
complaint. In the RTM version, I can''t do both. I don''t want to turn off
the warning as doing so may actually introduce another bug in my code
later
because I didn''t see the warning.

Thanks,
Mike.



Marina,


"码头" <所以***** @ nospam.com> schrieb:
Marina,

"Marina" <so*****@nospam.com> schrieb:
如果它是常数,为什么不总是通过类型名称引用它?
通过实例访问它会得到什么?
If it''s a constant, why not always reference it through the type name?
What do you gain by accessing it through an instance?




通过实例访问它会让你失去什么?


-

MS Herfried K. Wagner

MVP< URL:http://dotnet.mvps.org/>

VB< URL:http://classicvb.org/petition/>



What do you loose by accessing it through an instance?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


Michael

i使用Visual Studio 2005的发布版(专业版)


公共类Form1

Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理Button1.Click


Dim ClassConst As String = XMyClass.ConstantString


MsgBox(ClassConst)


Dim myObj = New XMyClass


ClassConst = myObj.ConstantString


MsgBox(ClassConst)


End Sub

结束班级


类XMyClass


Public Const ConstantString =" Some Constant String"


结束课程


我的开发计算机上没有任何问题(没有警告)没有问题


问候


Michel Posseth [MCP]

" Michael D. Ober" < obermd @ alum.mit.edu.nospam>在消息中写道

新闻:uj ************* @ TK2MSFTNGP14.phx.gbl ...
Michael
i use the release version of Visual studio 2005 ( professional edition )

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim ClassConst As String = XMyClass.ConstantString

MsgBox(ClassConst)

Dim myObj = New XMyClass

ClassConst = myObj.ConstantString

MsgBox(ClassConst)

End Sub

End Class

Class XMyClass

Public Const ConstantString = "Some Constant String"

End Class

this works without anny problems on my dev computer ( no warnings )

regards

Michel Posseth [MCP]
"Michael D. Ober" <obermd.@.alum.mit.edu.nospam> wrote in message
news:uj*************@TK2MSFTNGP14.phx.gbl...
有什么方法可以创建类中的常量,既可以用于实例化对象,也可以不用。例如:

dim ClassConst as string = myClass.ConstantString
dim myObj = new MyClass
ClassConst = myObj.ConstantString

里面MyClass类

类MyClass

public const ConstantString =" Some Constant String"

End Class

Beta 2,ConstantString可用于以上任何一种情况
没有
投诉。在RTM版本中,我不能同时做到这两点。我不想关闭
警告,因为这样做可能会在我的代码中实际引入另一个错误
,因为我没有看到警告。
<谢谢,
迈克。
Is there any way to create a constant in a class that can be used both
with
an instantiated object and without. For example:

dim ClassConst as string = myClass.ConstantString

dim myObj = new MyClass
ClassConst = myObj.ConstantString

Inside the class MyClass

Class MyClass

public const ConstantString = "Some Constant String"

End Class

In Beta 2, ConstantString was available in either of the above cases
without
complaint. In the RTM version, I can''t do both. I don''t want to turn off
the warning as doing so may actually introduce another bug in my code
later
because I didn''t see the warning.

Thanks,
Mike.



这篇关于静态类常量(VB 2005)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆