[RESOLVED] Can't convert integer to string in constant expression
Why? Can someone explain the reason for this legislature? I'd like to understand why one type can't be converted to another type inside a constant expression.
What on earth???
Re: Can't convert integer to string in constant expression
I guess the thinking is that the value is a constant and that you should know it before hand. Also if the constant expression is made up of variables, then it really isn't a constant at all. Since the values of the variables can change so can the constant, therefore it isn't really a constant but a variable;):p:wave::sick::eek2:
Re: Can't convert integer to string in constant expression
I think TFS is correct! You wouldn't need change a constant because as soon as you do it will equal a different value. However, you could create another variable which uses the constant and change its type.
Re: Can't convert integer to string in constant expression
Quote:
Originally Posted by
The Fire Snake
I guess the thinking is that the value is a constant and that you should know it before hand. Also if the constant expression is made up of variables, then it really isn't a constant at all. Since the values of the variables can change so can the constant, therefore it isn't really a constant but a variable;):p:wave::sick::eek2:
Well, in this case, the "variables" are two constants. You have the Version Number as one constant. And the Release Number as another. These two are concatenated together to create a constant for the Release Name. When MS decided not allow variables to be used in defining constants, you'd think that they, in all their holy wisdom, would realize that sometimes constants are defined by other constants, which means it's constant.
Is there some place I can turn this particular "check" off? I really don't need to be protected from myself in every single possible tiny infinitesimal little case.
Re: Can't convert integer to string in constant expression
Could you show us the code for the constant
Re: Can't convert integer to string in constant expression
Quote:
Originally Posted by
dbasnett
Could you show us the code for the constant
Sure, it's pretty straight forward:
Code:
Const VERSION_NO = 4
Const RELEASE_NO = 1
Const RELEASE_NAME As String = VERSION_NO & "." & RELEASE_NO
I'd like to use RELEASE_NAME as a nice fat uppercase constant, but I'm settling for:
Code:
Public ReleaseName as String = VERSION_NO & "." & RELEASE_NO
What can I do... I have to program in VB.NET, and VB.NET is determined to have complete control over my every twitch of breath. I've never imagined anything like this. It's the perfect language for a police state. I'll bet it's big in government.
Re: Can't convert integer to string in constant expression
You said, "...and VB.NET is determined to have complete control over my every twitch of breath...." What I think you meant is "and programming is determined to have complete control over my every twitch of breath...."
This is a detailed craft, with many rules. You want to see how many mistakes you have? Make this the first line of your code:
Option Strict On
Code:
Const VERSION_NO As Integer = 4
Const RELEASE_NO As Integer = 1
Private ReadOnly RELEASE_NAME As String = String.Format(" Foo v{0}.{1}", VERSION_NO, RELEASE_NO)
Re: Can't convert integer to string in constant expression
ReadOnly is a good idea. Basically makes it something like a constant. Thanks.
Re: Can't convert integer to string in constant expression
Quote:
Originally Posted by
wornways
Sure, it's pretty straight forward:
Code:
Const VERSION_NO = 4
Const RELEASE_NO = 1
Const RELEASE_NAME As String = VERSION_NO & "." & RELEASE_NO
I'd like to use RELEASE_NAME as a nice fat uppercase constant, but I'm settling for:
Code:
Public ReleaseName as String = VERSION_NO & "." & RELEASE_NO
What can I do... I have to program in VB.NET, and VB.NET is determined to have complete control over my every twitch of breath. I've never imagined anything like this. It's the perfect language for a police state. I'll bet it's big in government.
Ok, thats fair. You want a constant defined with other constants. I was able to do it fine. I think that the problem you are getting is because you are not defining a type for RELEASE_NO or RELEASE_NO. So I did the following with no problems. Try this:
vb Code:
Const VERSION_NO As String = "4"
Const RELEASE_NO As String = "1"
Const RELEASE_NAME As String = VERSION_NO & "." & RELEASE_NO
Re: Can't convert integer to string in constant expression
Which is why I suggested
Option Strict On
Re: Can't convert integer to string in constant expression
I thought of that, but I wasn't sure if the Build or Release numbers were used strictly as integers anywhere. But now that I think of it, I doubt they are. Defining these constants as strings really does make more sense, especially since the only way I KNOW they're used is as strings.
Alrighty folks. I think I've learned my embarrassing lesson for the day. I'll even mark this as resolved.
Re: [RESOLVED] Can't convert integer to string in constant expression
Did you turn Option Strict On? If not then you have one big lesson yet to learn.
Re: [RESOLVED] Can't convert integer to string in constant expression
Quote:
Originally Posted by
dbasnett
Did you turn Option Strict On? If not then you have one big lesson yet to learn.
Uh Oh... Where's it at?
Re: [RESOLVED] Can't convert integer to string in constant expression
Code:
'first line of your code
Option Strict On 'there is also an option to turn it on in the project settings
Public Class Form1
'rest of your code