1

How do I define a constant Char value, similar to vbCr? This does not work...

Public Const ctrM As Char = "\m"C

This says the constant must have exactly one character. Well, ok, isn't that what "\m" is?, what about the following

Public Const ctrM as Char = Convert.ToChar(9)

That's not allowed because it's a function. Huh. Luckily this does work:

Public Dim ctrM as Char = Convert.ToChar(9)

But this seems sub-optimal. Am I missing something here?

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Maury Markowitz
  • 9,082
  • 11
  • 46
  • 98
  • I know. So how do you do this in vb? – Maury Markowitz Oct 27 '14 at 21:51
  • Are you trying to define a `vbcr`? – OneFineDay Oct 27 '14 at 21:52
  • Must be yet another Xamarin problem. Never ends, does it? Why you consistently refuse to mention this and never consider using a C# helper assembly is an absolute mystery to me. You must like punishment, not so sure we do. – Hans Passant Oct 27 '14 at 21:53
  • @MauryMarkowitz: you cannot do it in C# either bercause those are two characters. It's not an [esacpe-sequence](http://msdn.microsoft.com/en-us/library/h21280bw.aspx) like `'\n'`(in C/C#). – Tim Schmelter Oct 27 '14 at 21:58
  • No, I'm trying to define "ctrM". – Maury Markowitz Oct 27 '14 at 21:59
  • So there's just no way to do this Tim? If so, you might want to make that an answer so I can accept it. Color me surprised. – Maury Markowitz Oct 27 '14 at 22:00
  • 1
    @MauryMarkowitz: what do you mean with "ctrM"? I must admit that i'm still not sure if i've understood what you're tying to do. – Tim Schmelter Oct 27 '14 at 22:03
  • Oh sorry, I want the equivalent of Convert.ToChar(25) as a Const. – Maury Markowitz Oct 27 '14 at 22:12
  • 4
    `Const ctrM As Char = Chr(25)` The compiler lets you use a few VB functions in Const declarations (Cbool, Chr etc) which can be determined at compile time. See [VB Const](http://msdn.microsoft.com/en-us/library/cyxe49xw.aspx) – Ňɏssa Pøngjǣrdenlarp Oct 27 '14 at 22:12
  • Why would you define ctrM as Chr(25)? Shouldn't it be Chr(13)? – Chris Dunaway Oct 28 '14 at 13:55
  • @Plutonix - do you know *why* Chr works in this case? It seems it is not what it *appears* to be, a function that returns a value. Is it possible to write similar code that I could use in my own Const declarations? If there's a pattern here, it would be nice to know it for future reference. – Maury Markowitz Oct 29 '14 at 12:49
  • Chr is a function (your other question in other comment below); it is just that the VB compiler gives some preferential treatment to VB functions allowing Chr, CBool etc but not Framework functions like `Convert.Toxxxx`. The result is that your code compiles to: `this.CtrM = "\u0019";` [I could not find this before](http://stackoverflow.com/a/25981310/1070452) – Ňɏssa Pøngjǣrdenlarp Oct 29 '14 at 13:07
  • Bah, humbug! Thanks @Plutonix, that's ultimately what I was looking for. – Maury Markowitz Oct 29 '14 at 13:30
  • Visual basic doesn't require string escaping, except for the quotation mark, which is escaped by putting two quotes together inside the string. – Paul Ishak Oct 29 '14 at 22:45

3 Answers3

2

Replace:

Public Const ctrM as Char = "\m"C

for this:

Public Const ctrM As Char = "m"c
Aprendiendo.NET
  • 410
  • 2
  • 6
2

Credits goes to Plutonix for giving a working/workable solution in a comment.
Used the following approach when I made large use of Modules long ago.

Add a Public Module to your Project :

    Public Module MyConsts

        ' Define your constant Char

        Public Const vbTabC As Char = Microsoft.VisualBasic.Chr(9) ' For Tabulation
        Public Const vbEMC As Char = Microsoft.VisualBasic.Chr(25) ' For EM (End of Medium)
        ' ^^ if you know the ASCII Char Code.
        ' Use Microsoft.VisualBasic.ChrW() for Unicode (unsure of that)

        Public Const vbCharQM As Char = "?"c
        Public Const vbComma As Char = ","c
        Public Const vbDot As Char = "."c
        ' or
        Public Const vbCharQM2 As Char = CChar("?")
        ' ^^ if you can actually write the char as String in the Compiler IDE

    End Module

Then use the constants identifier anywhere in your project like any VB constant string, but, they are of type Char of course (To combine them with String, you'll have to use .ToString())

    Public Sub TestConstChar()
        MessageBox.Show("[" + vbEMC.ToString() + "]")
        ' But hey ! What's the purpose of using End of Medium Char ?
    End sub

Note that you have Environment.NewLine that automatically returns the valid Line Feed, or Carriage Return/Line Feed, or only Carriage Return, or even another control Char/String/Stream that is on use on your Operating System.

Based on the Environment.NewLine example, you can also define a (wandering) Class

Public Class MyConstChars

    Public Shared ReadOnly Property Tab() As Char
        Get
            Return Microsoft.VisualBasic.ControlChars.Tab
        End Get
    End Property

    ' ...

End Class

' And use it anywhere like myString = "1" + MyConstChars.Tab.ToString() + "One"

This approach allows you to have more control over the actual value of the static/shared Property, like with Environment.NewLine, and allows your Class to propose much more options (Members) than a simple Constant. However, writing the LambdaClassName.LambdaClassProperty isn't very intuitive I reckon.


One another way to ease coding by using constant tags/identifiers in the IDE is to define Code Templates. A code template (piece of code) can be defined in the options of your IDE. You may already know what it is about : you type a keyword, then the IDE replace that keyword with one block of code (that you use often enough to require that shortcut) That's what is happening when you redefines (Overrides) a .ToString() Function in classes.

    ' I have for example one code template keyword...
    PlaceholderChecker

    ' ...that generates the following Code :

    #If IsDebugMode Then
    ''' <summary>
    ''' Placeholder Routine to check wether ALL Class Components are included in Solution.
    ''' </summary>
    Private Shared Sub PlaceholderChecker()
        p_ClassVariableName_ClassPartialSuffix = True
    End Sub
    #End If

In some cases, you don't have to define constants - or have to write more complex code - to get where you want.

Community
  • 1
  • 1
Karl Stephen
  • 1,120
  • 8
  • 22
  • Works great! Out of curiosity though, why does Chr work, but Convert.ToChar(9) not work? Both are, technically, constant expressions. I guess there is something extra-special about Chr? – Maury Markowitz Oct 28 '14 at 11:21
  • Ahhh, here's the key statement: *You cannot use variables or functions in initializer. However, you can use conversion keywords such as CByte and CShort. You can also use AscW if you call it with a constant String or Char argument, since that can be evaluated at compile time.* So I assume that Chr is not actually a function, but some sort of #define-type functionality? – Maury Markowitz Oct 28 '14 at 11:25
  • Honnestly, I don't know why XD - Haven't never tried to understand how Chr works, nor Convert.ToChar() but both seems to be Functions to me, so why Chr() works but not the later... Perhaps it's because Chr and ChrW takes _known_ values (integer) as parameter that could only throw an exception when out of range, then the compiler knows the final value before compiling. Convert.ToChar() can take a bunch of parameters including Object Type and Double, etc. (that always throws an exception) so, for const values, it would be indeed a no-go. :/ But heh ! why not have a try my dear compiler ?? XP – Karl Stephen Oct 28 '14 at 11:40
2

The answer by fsintegral is fine, but can be slightly simpler. And you can use the Framework functions if you prefer them to the VB Functions.

Class method:

Public Class AppConsts

    Public Shared ReadOnly CtrlEM As Char = Convert.ToChar(25)
    Public Shared ReadOnly CtrlT As Char = Convert.ToChar(9)
    Public Shared ReadOnly CtrlN As Char = Convert.ToChar(10)
    Public Shared ReadOnly CtrlM As Char = Convert.ToChar(13)
    Public Shared ReadOnly CrLf As String = CtrlN & CtrlM
    ...
End Class

'Usage:
Dim s as string = "..." & AppConts.CtrlEM

They will even show up in intellisense. If you dont like the Type/Class name intruding, you can import the class (I kind of like the Type portion included because it narrows the IntelliSense list rapidly to the relevant values):

Imports AppConsts
....
Dim s As String = CtrlEM

Alternatively, you can use the module method:

Module Program

    Friend ReadOnly CtrlM As Char = Convert.ToChar(25)

End Module

' usage:
Dim s2 As String = "xxxx..." & CtrlM

They are not really constants as far as how the compiler treats them at compile time because they aren't -- they are just ReadOnly fields. But as far as your code is concerned in the IDE, they will act, feel and taste like constants.

It is the use of Const statement which limits how you can define them and require you to use (some) the VB functions rather than .NET ones.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • Now THIS is precisely what I was looking for. Thanks again Plutonix! (Wow, so many down votes for a thread that got such great answers!) – Maury Markowitz Oct 30 '14 at 16:55