|
-
Mar 26th, 2010, 11:51 PM
#1
Thread Starter
Hyperactive Member
[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???
In an effort to abolish the writing of "Spaghetti Code",
some fools got together and invented Spaghetti Syntax.
-
Mar 27th, 2010, 12:53 AM
#2
Hyperactive Member
-
Mar 27th, 2010, 01:29 AM
#3
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.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Mar 27th, 2010, 02:28 AM
#4
Thread Starter
Hyperactive Member
Re: Can't convert integer to string in constant expression
 Originally Posted by The Fire Snake
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.
In an effort to abolish the writing of "Spaghetti Code",
some fools got together and invented Spaghetti Syntax.
-
Mar 27th, 2010, 05:29 AM
#5
Re: Can't convert integer to string in constant expression
Could you show us the code for the constant
-
Mar 27th, 2010, 05:50 AM
#6
Thread Starter
Hyperactive Member
Re: Can't convert integer to string in constant expression
 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.
In an effort to abolish the writing of "Spaghetti Code",
some fools got together and invented Spaghetti Syntax.
-
Mar 27th, 2010, 07:34 AM
#7
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)
-
Mar 27th, 2010, 07:47 AM
#8
Thread Starter
Hyperactive Member
Re: Can't convert integer to string in constant expression
ReadOnly is a good idea. Basically makes it something like a constant. Thanks.
In an effort to abolish the writing of "Spaghetti Code",
some fools got together and invented Spaghetti Syntax.
-
Mar 27th, 2010, 12:38 PM
#9
Hyperactive Member
Re: Can't convert integer to string in constant expression
 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
-
Mar 27th, 2010, 12:48 PM
#10
Re: Can't convert integer to string in constant expression
Which is why I suggested
Option Strict On
-
Mar 27th, 2010, 04:18 PM
#11
Thread Starter
Hyperactive Member
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.
In an effort to abolish the writing of "Spaghetti Code",
some fools got together and invented Spaghetti Syntax.
-
Mar 27th, 2010, 06:43 PM
#12
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.
-
Mar 27th, 2010, 06:51 PM
#13
Thread Starter
Hyperactive Member
Re: [RESOLVED] Can't convert integer to string in constant expression
 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?
In an effort to abolish the writing of "Spaghetti Code",
some fools got together and invented Spaghetti Syntax.
-
Mar 27th, 2010, 07:42 PM
#14
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|