converting to char

Anti-Rich

Well-known member
Joined
Jul 1, 2006
Messages
325
Location
Perth, Australia
Programming Experience
1-3
hi guys

just wondering, i am using a function to determine if there are any non-numerical characters in the string as the user inputs a payrate. (the only exception being the . character.

the question i have is, i have used a way to convert a string to char as follows..

"."c

that little c converts it to char for use in the if statement. now, i know i can also use cchar("."), i was just wondering is either way better than the other. and also, is there another way i could be doing it?

cheers all
adam
 
this is the function i use...
VB.NET:
private function returntext(byval t as string) as string
dim temp as string=""
for i as integer=0 to t.length-1
        if Char.isnumeric(t.chars(i)) or t.chars(i) = "."c
        temp &= t.chars(i)
end if
 
next
return temp
end function
what do you think?
 
Last edited:
The little c does NOT convert anything to a Char. Using the little c suffix means that the literal IS a Char. Doing this:
VB.NET:
Dim myChar As Char = "."c
simply creates a Char literal and assigns it to the myChar variable. There are no Strings involved. Doing this:
VB.NET:
Dim myChar As Char = CChar(".")
creates a String literal, then converts that to a Char and assigns it to the variable. It creates a String object for nothing.
 
Concur with JMc.. example, in a C-flavoured language you have:

VB.NET:
char c = 'a';

in vb, that's:
VB.NET:
Dim c as char = "a"c

because ' is used for comment


Raven mentions regex. the following regex checks that only a (maybe decimal) number is entered:

System.Text.RegularExpressions.Regex.IsMatch(input, "[0-9]+([.][0-9]+)?")

broken down:
"[0-9]+([.][0-9]+)?"
[0-9]+ = one or more occurrance of 0 to 9
( = open a group
[.] = followed by .
[0-9]+ = see above
)? = close group, and allow 0 or 1 of the group


BUt you know what? all this is so much harder than just trying to convert it to a decimal and noting the fail:

VB.NET:
Dim d as Decimal
If Decimal.TryParse(input, d) Then
  'thankyou for your correct input
Else
  'your input was not a number
Endif
 
ok i think i get it. so using the c like "."c simply means that it IS a char...

so why would people ever bother using cchar(".") if its doing something thats unnecessary? or im i missing something, because it seems that using the little c is much quicker than converting it, as using the c simply means that it is a char (therefore avoiding both conversion and creation of unnecessary string).

does that sound right?

cheers for your feedback guys
 
The "A"c convension is very limited, in that it will only work with a typed string of exactly one character, it won't work with "AA"c. You can't make a string variable 'myA' set it to "A" and do 'myChar=myAc' and expect that 'c' to be understood as anything special, you'd have to write 'myChar=cchar(myA)'.
 
im not sure i understand what you mean... i always thought when you are using a char it was SUPPOSED to be one character and one character only

cheers for your feedback as well
 
Sometimes one hardcode these characters and it is a shortcut just writing the "A" string and specifically denote it to be interpreted as a Char type.
 
ok, but if you do it what i would perceive to be the 'right way' it would be like this...

Private c as Char = "c"

so doesnt then create another unnecessary string? wouldnt it be better for the value (in this case "c") to be iterpreted straightaway as a char?

sorry if it sounds dumb, but im a bit lost at the moment *sheepish grin*
 
You don't understand, "A" is a String, "A"c is a Char.
Create a new project, turn Option Strict on and try to code sloppy like this:
VB.NET:
Dim c As Char = "A"
You'll get this error:
Option Strict On disallows implicit conversions from 'String' to 'Char'.
You could access by index the first character in that string like this:
VB.NET:
Dim c As Char = "A"(0)
..which goes fine with strict type code, but "A"c denotion is easier.
 
Last edited:
ok,

so if i was to instead use

Private f as Char = "f"c


taht error wouldnt happen due to the fact it is being interpreted (the 'f') as a char from the get-go

is that correct?
 
That is just so totally correct.
 
Back
Top