Results 1 to 14 of 14

Thread: [RESOLVED] Can't convert integer to string in constant expression

  1. #1

    Thread Starter
    Hyperactive Member wornways's Avatar
    Join Date
    Mar 2010
    Posts
    261

    Resolved [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.

  2. #2
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    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

  3. #3
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    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

  4. #4

    Thread Starter
    Hyperactive Member wornways's Avatar
    Join Date
    Mar 2010
    Posts
    261

    Re: Can't convert integer to string in constant expression

    Quote Originally Posted by The Fire Snake View Post
    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
    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.

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Can't convert integer to string in constant expression

    Could you show us the code for the constant
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    Hyperactive Member wornways's Avatar
    Join Date
    Mar 2010
    Posts
    261

    Re: Can't convert integer to string in constant expression

    Quote Originally Posted by dbasnett View Post
    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.

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8

    Thread Starter
    Hyperactive Member wornways's Avatar
    Join Date
    Mar 2010
    Posts
    261

    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.

  9. #9
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    Re: Can't convert integer to string in constant expression

    Quote Originally Posted by wornways View Post
    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:
    1. Const VERSION_NO As String = "4"
    2. Const RELEASE_NO As String = "1"
    3. Const RELEASE_NAME As String = VERSION_NO & "." & RELEASE_NO

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Can't convert integer to string in constant expression

    Which is why I suggested

    Option Strict On
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  11. #11

    Thread Starter
    Hyperactive Member wornways's Avatar
    Join Date
    Mar 2010
    Posts
    261

    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.

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  13. #13

    Thread Starter
    Hyperactive Member wornways's Avatar
    Join Date
    Mar 2010
    Posts
    261

    Re: [RESOLVED] Can't convert integer to string in constant expression

    Quote Originally Posted by dbasnett View Post
    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.

  14. #14
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width