473,657 Members | 2,576 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static Class Constants (VB 2005)

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.Constan tString

dim myObj = new MyClass
ClassConst = myObj.ConstantS tring

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.

Dec 8 '05 #1
29 1957
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******** *****@TK2MSFTNG P14.phx.gbl...
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.Constan tString

dim myObj = new MyClass
ClassConst = myObj.ConstantS tring

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.

Dec 8 '05 #2
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?


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/>

Dec 8 '05 #3
Michael
i use the release version of Visual studio 2005 ( professional edition )

Public Class Form1
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

Dim ClassConst As String = XMyClass.Consta ntString

MsgBox(ClassCon st)

Dim myObj = New XMyClass

ClassConst = myObj.ConstantS tring

MsgBox(ClassCon st)

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******** *****@TK2MSFTNG P14.phx.gbl...
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.Constan tString

dim myObj = new MyClass
ClassConst = myObj.ConstantS tring

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.

Dec 8 '05 #4
Code readability.

Mike.

"Marina" <so*****@nospam .com> wrote in message
news:OE******** *****@tk2msftng p13.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******** *****@TK2MSFTNG P14.phx.gbl...
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.Constan tString

dim myObj = new MyClass
ClassConst = myObj.ConstantS tring

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.



Dec 8 '05 #5
There are times when accessing through an instance makes more sense - it
ensures I'm working with the correct object/class type. Other times,
accessing through the class type make more sense - when trying to verify
class type of an unknown string representation of the object. In the latter
case, class type verification can then be done without the overhead of
creating an object that then gets immediately thrown away.

Mike Ober.

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:OA******** ******@TK2MSFTN GP15.phx.gbl...
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?


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/>


Dec 8 '05 #6
Apparently you lose the ability to compile without getting a warner. You
also lose code clarity, as now it is not obvious this is a constant. It
could just be an instance property of the class, and it's not at all obvious
that it is not. Where as seeing the type name in front, you see that this
something that exists only once for the class.

The point is, if you don't gain anything by doing it, and you lose
readability, then why do it?

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:OA******** ******@TK2MSFTN GP15.phx.gbl...
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?


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/>

Dec 8 '05 #7
Regardless of Option Explicit and Option Strict settings on a brand new form
with a single button, I get the error "Access of shared member, constant
member, enum member or nested type through an instance; qualifying
expression will not be evaluated." on your code sample. VS 2005 Standard
RTM.

Mike.

"m.posseth" <mi*****@nohaus ystems.nl> wrote in message
news:ev******** ******@TK2MSFTN GP14.phx.gbl...
Michael
i use the release version of Visual studio 2005 ( professional edition )

Public Class Form1
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

Dim ClassConst As String = XMyClass.Consta ntString

MsgBox(ClassCon st)

Dim myObj = New XMyClass

''''''''''''' Error is on this line ClassConst = myObj.ConstantS tring

MsgBox(ClassCon st)

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******** *****@TK2MSFTNG P14.phx.gbl...
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.Constan tString

dim myObj = new MyClass
ClassConst = myObj.ConstantS tring

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.



Dec 8 '05 #8
If this is about a constant, how does that help ensure you are working with
the right class? There is no inheritance issue here.

"Michael D. Ober" <obermd.@.alum. mit.edu.nospam> wrote in message
news:OI******** ******@TK2MSFTN GP10.phx.gbl...
There are times when accessing through an instance makes more sense - it
ensures I'm working with the correct object/class type. Other times,
accessing through the class type make more sense - when trying to verify
class type of an unknown string representation of the object. In the
latter
case, class type verification can then be done without the overhead of
creating an object that then gets immediately thrown away.

Mike Ober.

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:OA******** ******@TK2MSFTN GP15.phx.gbl...
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?


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/>


Dec 8 '05 #9
"Marina" <so*****@nospam .com> schrieb:
If this is about a constant, how does that help ensure you are working
with the right class? There is no inheritance issue here.


That's true, but especially when dealing with public constants it seems
completely irrelevant to me whether or not the member belongs to an instance
or the class itself. I agree that it doesn't make much sense to obtain a
reference to an object using complicated code only to access one of the
public constants of the class.

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

Dec 8 '05 #10

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

Similar topics

12
6113
by: Gordon | last post by:
I want to provide a set of static functions in a superclass that work with class constants defined in a decendant of that class. Unfortunately I've run into a snag with this idea. Example: class SuperClass { const CNST = 'Super class'; public static function getCnst () {
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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
7324
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...
0
4151
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
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.