VB.NET, is there a special Const?

SoftwareEng

Senior member
Apr 24, 2005
553
4
81
Is there a special type of Const that can be initialized in code separately from declaration? It's like a write-once variable, that once you set its value, it becomes a constant.

It's like

Public Const sSERVER_NAME As String

'-- And then, later:
sSERVER_NAME = "windows"

?

 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
Never heard of that, but I am not a VB expert, and wouldn't be that surprised to see a hack like that in VB ;). It's not const if it can be changed after initialization.
 

Apathetic

Platinum Member
Dec 23, 2002
2,587
6
81
No there isn't. Probably the closest thing you could do would be something like this:

Public Class TestClass
Private m_ServerName As String

Public Sub New()
m_ServerName = "SERVER NAME"
End Sub

Public Property ServerName() As String
Get
Return m_ServerName
End Get

Set(ByVal value As String)
Throw New ApplicationException("Variable is constant")
End Set
End Property
End Class


This way you can set the value in the constructor and as long as all the rest of the code uses the ServerName property, you'll get what you're looking for.

Hope this helps.

Dave
 

Apathetic

Platinum Member
Dec 23, 2002
2,587
6
81
Umm... is is just me (probably) or is the whole "attach code" thing all fsck'ed up?

I tried attaching code 5 or 6 times, and all of them would come out messed up.

Dave
 

SoftwareEng

Senior member
Apr 24, 2005
553
4
81
Thanks for your help guys. I saw something like this in C#, don't remember what it's called... "Initialize-once" variables or something like that. Ehh.

I've never used the Attach Code feature here, is it broxen?
 

SoftwareEng

Senior member
Apr 24, 2005
553
4
81
That's it, static readonly in C#. There doesn't seem to be an equivalent in VB.NET 2005, but at least we know what they are.

thank you guys