[RESOLVED] Replacements for vbCrLf, vbTab, etc
Recently stummbled on the issue of using a '.NET' replacement for the constants 'vbCrLf, vbTab', etc.
Figured out that vbCrLf = Environment.Newline, but others are not so easy. I have had a look at Wossnames' Char Initializer class (nice job! :thumb: ), and it works well for creating other control characters.
Was this an oversight on M$ part, not providing a .NET equivalent for the Chr and Asc functions, do you think? (Is it still the same in V2005?)
Re: Replacements for vbCrLf, vbTab, etc
You can use the Convert function. ;)
VB Code:
MessageBox.Show("Test" & Convert.ToChar(13) & "Test")
'"Test"
'"Test"
Edit: I think its more of a choice MS made to advance VB towards OOP and get away from the days of BASIC language.
Re: Replacements for vbCrLf, vbTab, etc
Hers the Tab equilivalent.
VB Code:
MessageBox.Show("Test" & Convert.ToChar(9) & "Test")
'"Test Test"
Re: Replacements for vbCrLf, vbTab, etc
Thanks Rob, I didn't know about the Convert function. :)
I would have to say that I am totally new to all things VB, so am not burdened with any problems upgrading from VB6. It is all new to me! :sick:
Re: Replacements for vbCrLf, vbTab, etc
I know its hard for VB6 programmers to convert to vb.net :) as it was and still is to a point for me too. ;) Seems if you have no VB6 experience your better off because your not trying to convert old vb6 habits, which usually aren't the best, to new vb.net.
Re: Replacements for vbCrLf, vbTab, etc
ControlChars.Tab, ControlChars.NewLine, ControlChars.Quote
These come from the VisualBasic namespace but not from the Compatibility namespace. NOTE: You can't unreference the VisualBasic namespace in a VB application nor is it bad.
In C# you could use \r\n or \t.
Re: Replacements for vbCrLf, vbTab, etc
Convert comes from the System namespace so I think its best to use those since it is more relative to .NET. ;)
Re: Replacements for vbCrLf, vbTab, etc
Ed, isnt that the same as JavaScript? Makes sense ince C# is very similar to C++ and JavaScript.
Re: Replacements for vbCrLf, vbTab, etc
The easiest way to create characters for things like Tab, etc. is to use ControlChars, as suggested in the VS help. If you don't like VB Runtime constants then the next easiest way is to combine Rob's suggestion with the Keys enumeration. The Keys enumeration gives a descriptive name to ASCII and Unicode codes so you don't need to remember the codes yourself. To get the Tab character you just use Convert.ToChar(Keys.Tab). Intellisense will give you a list of the members of the enumeration so if you aren't sure what you're looking for you can type "keys." and browse the listing.
Quote:
Originally Posted by RobDog888
Convert comes from the System namespace so I think its best to use those since it is more relative to .NET.
I guarantee you that the overwhelming majority of C# developers will use the escaped C characters rather than using the Convert class, so ControlChars is no more language-specific than that.
Re: Replacements for vbCrLf, vbTab, etc
Quote:
To get the Tab character you just use Convert.ToChar(Keys.Tab).
Thanks JM, that's cool.
It appears there are many ways to skin many cats in .NET!
Re: [RESOLVED] Replacements for vbCrLf, vbTab, etc
Note that Convert.ToChar(Int32) and Convert.ToInt32(Char) work the same as ChrW() and AscW(). The Keys enumeration also has the Return (CR) and LineFeed (LF) members.