none
Visual Basic to C# defining string of characters. RRS feed

  • Question

  • I am trying to convert a VB program to C# and I am new to C#. 

    VB Code that works.

      Const embHeartbeat = Chr(17) & Chr(65)
        Const embWrongFormat = Chr(66)
        Const embFormatOkay = Chr(67)

    C# code that doesn't

      const var embHeartbeat = Strings.Chr(17) + Strings.Chr(65);
        const var embWrongFormat = Strings.Chr(66);
        const var embFormatOkay = Strings.Chr(67);

    in Visual Studio 19 I just get red squiggly lines underneath.  I tried every combination that I can think of and have went to multiple sites but cannot find a solution.  Does C# not allow you to make a string of characters?


    FordIT


    • Edited by FordIT Friday, September 20, 2019 9:26 AM
    Friday, September 20, 2019 9:22 AM

Answers

  • Here is a version that can compile. The first variable can not be a constant as the compile can not figure out the type for the first variable so it must be a string not const.

    var embHeartbeat = $"{(char)17}{(char)65}";
    const char embWrongFormat = (char)66;
    const char embFormatOkay = (char)67;

    This will build too with red error lines

    const string embHeartbeat = ((char)17).ToString() + ((char)65).ToString();
    const char embWrongFormat = (char)66;
    const char embFormatOkay = (char)67;
    My thought is the compiler will treat the first const as a variable even thou it's mark as const.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by FordIT Friday, September 20, 2019 10:05 AM
    Friday, September 20, 2019 9:56 AM
    Moderator

All replies

  • You can add reference to Microsoft.VisualBasic, then  :

    string embHeartbeat =  Microsoft.VisualBasic.Strings.Chr(17).ToString() + Microsoft.VisualBasic.Strings.Chr(65).ToString();
    string embWrongFormat =  Microsoft.VisualBasic.Strings.Chr(66).ToString();
    string embFormatOkay = Microsoft.VisualBasic.Strings.Chr(67).ToString();

    Friday, September 20, 2019 9:48 AM
  • Here is a version that can compile. The first variable can not be a constant as the compile can not figure out the type for the first variable so it must be a string not const.

    var embHeartbeat = $"{(char)17}{(char)65}";
    const char embWrongFormat = (char)66;
    const char embFormatOkay = (char)67;

    This will build too with red error lines

    const string embHeartbeat = ((char)17).ToString() + ((char)65).ToString();
    const char embWrongFormat = (char)66;
    const char embFormatOkay = (char)67;
    My thought is the compiler will treat the first const as a variable even thou it's mark as const.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by FordIT Friday, September 20, 2019 10:05 AM
    Friday, September 20, 2019 9:56 AM
    Moderator
  • Thank you!  

    FordIT

    Friday, September 20, 2019 10:05 AM