473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

.NET Framework Equivalent to ASCW & ChrW

I would really rather use the "dot-nettified" syntax rather than digging
global functions out of Microsoft.VisualBasic.

Anyone know the newfangled way to achieve the same results?

--
Peace & happy computing,

Mike Labosh, MCSD
"Musha ring dum a doo dum a da!" -- James Hetfield
Nov 21 '05 #1
17 5774
>I would really rather use the "dot-nettified" syntax rather than digging
global functions out of Microsoft.VisualBasic.

Anyone know the newfangled way to achieve the same results?


Well if you really want to you can use the System.Convert class.

But I would think twice before using that over ChrW/AscW. They are
compile-time evaluated expressions that result in code that's more
efficient than any function call ever can be.
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2
I agree. AscW and ChrW are reliable functions that are the most elegant way
to do this. For instance, Convert.ToInt32 will do what AscW does, but you
will have to ensure you're passing exactly one character - AscW takes care of
that for you by only looking at the first character if you pass a string.

You could write your own AscW and ChrW functions using 'pure' .NET code, but
what's the point? (very well tested versions already exist)
--
David Anton
www.tangiblesoftwaresolutions.com
Home of:
Clear VB: Cleans up outdated VB.NET code
Instant C#: Converts from VB.NET to C#
Instant VB: Converts from C# to VB.NET
Instant J#: Converts from VB.NET to J#
"Mattias Sjögren" wrote:
I would really rather use the "dot-nettified" syntax rather than digging
global functions out of Microsoft.VisualBasic.

Anyone know the newfangled way to achieve the same results?


Well if you really want to you can use the System.Convert class.

But I would think twice before using that over ChrW/AscW. They are
compile-time evaluated expressions that result in code that's more
efficient than any function call ever can be.
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 21 '05 #3
On 2005-08-30, David Anton <Da********@discussions.microsoft.com> wrote:
I agree. AscW and ChrW are reliable functions that are the most elegant way
to do this. For instance, Convert.ToInt32 will do what AscW does, but you
will have to ensure you're passing exactly one character - AscW takes care of
that for you by only looking at the first character if you pass a string.


That's not exactly what Mattias was pointing out. While there are
functions named AscW and ChrW in the Strings namespace, that function call
is not necessarily what you get when you type AscW("C") into your code.
Rather, they are compile time expressions that result in different
compiled code depending on what parameter types you send to them. (I
don't really know what to call them, they function like keywords, but
they aren't keywords).

The equivalent to the ChrW function call is System.Convert.ToChar().
AscW is more interesting. There really isn't a .Net equivalent since
most .Net languages don't need a function call here, but as you point
out, Convert.ToInt32 will do more or less the same thing with single
characters.
You could write your own AscW and ChrW functions using 'pure' .NET code, but

what's the point? (very well tested versions already exist)


Well, if you switch .Net languages a lot or work in a multi-language shop,
there's an advantage to staying away from the VisualBasic namespace and
the VB conversion keywords. It's a huge namespace with many useful
functions that are similar but not quite identical to the "main"
namespaces of .Net. If you use the same functions across languages,
then you don't have to worry about the subtle differences.

Nov 21 '05 #4
David,
| For instance, Convert.ToInt32 will do what AscW does, but
Consider the case where you pass a literal or constant to AscW or ChrW.

In the case of a literal or constant AscW & ChrW are evaluated at compile
time, there is no function called! Only "pure" inline IL opcodes!

In the case of a char or integer variables AscW is emitted as IL code.

In the case of a Char variable ChrW is emitted as a library call, even then
its a library of .NET functions. My understanding is so as to ensure
negative values are converted into their corresponding "unsigned value".

In the case of String variables are AscW & ChrW is emitted as a library
call, even then its a library of .NET functions. As you stated, this is to
return the first character.
See below for IL code:

//000065:
//000066: Dim ch As Char
//000067: Dim charCode As Integer
//000068: Dim s As String
//000069:
//000070: ch = ControlChars.Lf
IL_0016: ldc.i4.s 10
IL_0018: stloc.0
//000071:
//000072: s = ch
IL_0019: ldloc.0
IL_001a: call string
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StringType: :FromChar(char)
IL_001f: stloc.2
//000073:
//000074: charCode = AscW(s)
IL_0020: ldloc.2
IL_0021: call int32
[Microsoft.VisualBasic]Microsoft.VisualBasic.Strings::AscW(string)
IL_0026: stloc.1
//000075:
//000076: charCode = AscW(ch)
IL_0027: ldloc.0
IL_0028: stloc.1
//000077:
//000078: ch = ChrW(charCode)
IL_0029: ldloc.1
IL_002a: call char
[Microsoft.VisualBasic]Microsoft.VisualBasic.Strings::ChrW(int32)
IL_002f: stloc.0
//000079:
//000080: s = ChrW(charCode)
IL_0030: ldloc.1
IL_0031: call char
[Microsoft.VisualBasic]Microsoft.VisualBasic.Strings::ChrW(int32)
IL_0036: call string
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StringType: :FromChar(char)
IL_003b: stloc.2
//000081:
| You could write your own AscW and ChrW functions using 'pure' .NET code,
but
| what's the point? (very well tested versions already exist)
Do people really believe that AscW & ChrW are not "pure" .NET code?

What an odd mis-conception people must have of the framework. :-|

Hope this helps
Jay

"David Anton" <Da********@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...
|I agree. AscW and ChrW are reliable functions that are the most elegant
way
| to do this. For instance, Convert.ToInt32 will do what AscW does, but you
| will have to ensure you're passing exactly one character - AscW takes care
of
| that for you by only looking at the first character if you pass a string.
|
| You could write your own AscW and ChrW functions using 'pure' .NET code,
but
| what's the point? (very well tested versions already exist)
| --
| David Anton
| www.tangiblesoftwaresolutions.com
| Home of:
| Clear VB: Cleans up outdated VB.NET code
| Instant C#: Converts from VB.NET to C#
| Instant VB: Converts from C# to VB.NET
| Instant J#: Converts from VB.NET to J#
|
|
| "Mattias Sjögren" wrote:
|
| > >I would really rather use the "dot-nettified" syntax rather than
digging
| > >global functions out of Microsoft.VisualBasic.
| > >
| > >Anyone know the newfangled way to achieve the same results?
| >
| > Well if you really want to you can use the System.Convert class.
| >
| > But I would think twice before using that over ChrW/AscW. They are
| > compile-time evaluated expressions that result in code that's more
| > efficient than any function call ever can be.
| >
| >
| > Mattias
| >
| > --
| > Mattias Sjögren [MVP] mattias @ mvps.org
| > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
| > Please reply only to the newsgroup.
| >
Nov 21 '05 #5
I wasn't disagreeing with anything Mattias said.
I'm not disputing the compile-time nature of the calls.
I was just stating another reason that someone might want to use AscW and
ChrW rather than trying to find 'pure' .NET equivalents.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code
"dfoster" wrote:
On 2005-08-30, David Anton <Da********@discussions.microsoft.com> wrote:
I agree. AscW and ChrW are reliable functions that are the most elegant way
to do this. For instance, Convert.ToInt32 will do what AscW does, but you
will have to ensure you're passing exactly one character - AscW takes care of
that for you by only looking at the first character if you pass a string.


That's not exactly what Mattias was pointing out. While there are
functions named AscW and ChrW in the Strings namespace, that function call
is not necessarily what you get when you type AscW("C") into your code.
Rather, they are compile time expressions that result in different
compiled code depending on what parameter types you send to them. (I
don't really know what to call them, they function like keywords, but
they aren't keywords).

The equivalent to the ChrW function call is System.Convert.ToChar().
AscW is more interesting. There really isn't a .Net equivalent since
most .Net languages don't need a function call here, but as you point
out, Convert.ToInt32 will do more or less the same thing with single
characters.
You could write your own AscW and ChrW functions using 'pure' .NET code, but

what's the point? (very well tested versions already exist)


Well, if you switch .Net languages a lot or work in a multi-language shop,
there's an advantage to staying away from the VisualBasic namespace and
the VB conversion keywords. It's a huge namespace with many useful
functions that are similar but not quite identical to the "main"
namespaces of .Net. If you use the same functions across languages,
then you don't have to worry about the subtle differences.

Nov 21 '05 #6
As I mentioned in my other post, I'm not disputing this at all. See my
original post.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code
"Jay B. Harlow [MVP - Outlook]" wrote:
David,
| For instance, Convert.ToInt32 will do what AscW does, but
Consider the case where you pass a literal or constant to AscW or ChrW.

In the case of a literal or constant AscW & ChrW are evaluated at compile
time, there is no function called! Only "pure" inline IL opcodes!

In the case of a char or integer variables AscW is emitted as IL code.

In the case of a Char variable ChrW is emitted as a library call, even then
its a library of .NET functions. My understanding is so as to ensure
negative values are converted into their corresponding "unsigned value".

In the case of String variables are AscW & ChrW is emitted as a library
call, even then its a library of .NET functions. As you stated, this is to
return the first character.
See below for IL code:

//000065:
//000066: Dim ch As Char
//000067: Dim charCode As Integer
//000068: Dim s As String
//000069:
//000070: ch = ControlChars.Lf
IL_0016: ldc.i4.s 10
IL_0018: stloc.0
//000071:
//000072: s = ch
IL_0019: ldloc.0
IL_001a: call string
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StringType: :FromChar(char)
IL_001f: stloc.2
//000073:
//000074: charCode = AscW(s)
IL_0020: ldloc.2
IL_0021: call int32
[Microsoft.VisualBasic]Microsoft.VisualBasic.Strings::AscW(string)
IL_0026: stloc.1
//000075:
//000076: charCode = AscW(ch)
IL_0027: ldloc.0
IL_0028: stloc.1
//000077:
//000078: ch = ChrW(charCode)
IL_0029: ldloc.1
IL_002a: call char
[Microsoft.VisualBasic]Microsoft.VisualBasic.Strings::ChrW(int32)
IL_002f: stloc.0
//000079:
//000080: s = ChrW(charCode)
IL_0030: ldloc.1
IL_0031: call char
[Microsoft.VisualBasic]Microsoft.VisualBasic.Strings::ChrW(int32)
IL_0036: call string
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StringType: :FromChar(char)
IL_003b: stloc.2
//000081:
| You could write your own AscW and ChrW functions using 'pure' .NET code,
but
| what's the point? (very well tested versions already exist)
Do people really believe that AscW & ChrW are not "pure" .NET code?

What an odd mis-conception people must have of the framework. :-|

Hope this helps
Jay

"David Anton" <Da********@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...
|I agree. AscW and ChrW are reliable functions that are the most elegant
way
| to do this. For instance, Convert.ToInt32 will do what AscW does, but you
| will have to ensure you're passing exactly one character - AscW takes care
of
| that for you by only looking at the first character if you pass a string.
|
| You could write your own AscW and ChrW functions using 'pure' .NET code,
but
| what's the point? (very well tested versions already exist)
| --
| David Anton
| www.tangiblesoftwaresolutions.com
| Home of:
| Clear VB: Cleans up outdated VB.NET code
| Instant C#: Converts from VB.NET to C#
| Instant VB: Converts from C# to VB.NET
| Instant J#: Converts from VB.NET to J#
|
|
| "Mattias Sjögren" wrote:
|
| > >I would really rather use the "dot-nettified" syntax rather than
digging
| > >global functions out of Microsoft.VisualBasic.
| > >
| > >Anyone know the newfangled way to achieve the same results?
| >
| > Well if you really want to you can use the System.Convert class.
| >
| > But I would think twice before using that over ChrW/AscW. They are
| > compile-time evaluated expressions that result in code that's more
| > efficient than any function call ever can be.
| >
| >
| > Mattias
| >
| > --
| > Mattias Sjögren [MVP] mattias @ mvps.org
| > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
| > Please reply only to the newsgroup.
| >

Nov 21 '05 #7
>I wasn't disagreeing with anything Mattias said.
I'm not disputing the compile-time nature of the calls.
I was just stating another reason that someone might want to use AscW and
ChrW rather than trying to find 'pure' .NET equivalents.


But the [OP] would prefer to avoid the Microsoft.VisualBasic Namespace.
That was the original point.

--
Peace & happy computing,

Mike Labosh, MCSD
"Musha ring dum a doo dum a da!" -- James Hetfield
Nov 21 '05 #8
This is becoming worse than an Abbot & Costello routine... (that dates me)

The original poster wants to avoid the VisualBasic namespace - agreed.
I just gave a reason why you might not feel compelled to do this (as does
Mattias, Jay, etc.).

It is perfectly valid to suggest B when someone is asking for a way to do A
- it's up to the original poster to evaluate the reasoning and choose A or B.

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code
"Mike Labosh" wrote:
I wasn't disagreeing with anything Mattias said.
I'm not disputing the compile-time nature of the calls.
I was just stating another reason that someone might want to use AscW and
ChrW rather than trying to find 'pure' .NET equivalents.


But the [OP] would prefer to avoid the Microsoft.VisualBasic Namespace.
That was the original point.

--
Peace & happy computing,

Mike Labosh, MCSD
"Musha ring dum a doo dum a da!" -- James Hetfield

Nov 21 '05 #9
David,
I was just stating another reason that someone might want to use AscW and
ChrW rather than trying to find 'pure' .NET equivalents.
--


AscW and ChrW are 'pure' .Net, you are mixing up the namespace system.Net
with the .Net framework, somethig the same as some people think that .Net is
only for InterNet.

Cor
Nov 21 '05 #10
Mike,

See your subject, don't mix up

System.Net namespace
..Net framework (containing much more than only the System.Net namespace)
InterNet

I hope this helps,

Cor
Nov 21 '05 #11
> It is perfectly valid to suggest B when someone is asking for a way to do
A
- it's up to the original poster to evaluate the reasoning and choose A or
B.


Precisely. That's why I asked.
--Who's on first?
--
Peace & happy computing,

Mike Labosh, MCSD
"Musha ring dum a doo dum a da!" -- James Hetfield
Nov 21 '05 #12
David,

I posted another answer to the OP containing the same, I see now that it is
not your point.

Cor
Nov 21 '05 #13
Mike,
Yes, I got the impression that the OP *naively* wants to avoid the
Microsoft.VisualBasic namespace also. Ergo my post to David, which was
really directed at the OP & other developers who *naively* want to avoid the
Microsoft.VisualBasic namespace. It was not specifically directed at David.

As Mattias, David, dfoster, & myself have pointed out. It doesn't really
makes sense to totally avoid the namespace. Its generally better to know
what the namespace offers & use it (the namespace) intelligently.

For example AscW, ChrW & the other "conversion" functions (operators really)
in the namespace are really "keywords" and create different IL based on what
you throw at it.

Where as I find the collections in System.Collection a better fit for my
needs then the VB.Collection object.

I liken avoiding the namespace: similar to stating that I'm going to totally
avoid the If statement, as the Select statement is more flexibly...

IMHO the "best" approach is to use the right tool (in this case possibly
ChrW & AscW) for the right job.

See the following for more details:

http://www.panopticoncentral.net/arc...6/07/1200.aspx

Although it is talking about conversion operators & not AscW & ChrW per se,
IMHO the same rational applies.

Just a thought
Jay
"Mike Labosh" <ml*****@hotmail.com> wrote in message
news:e$**************@TK2MSFTNGP11.phx.gbl...
| >I wasn't disagreeing with anything Mattias said.
| > I'm not disputing the compile-time nature of the calls.
| > I was just stating another reason that someone might want to use AscW
and
| > ChrW rather than trying to find 'pure' .NET equivalents.
|
| But the [OP] would prefer to avoid the Microsoft.VisualBasic Namespace.
| That was the original point.
|
| --
| Peace & happy computing,
|
| Mike Labosh, MCSD
| "Musha ring dum a doo dum a da!" -- James Hetfield
|
|
Nov 21 '05 #14
I absolutely agree - that's why I stated 'pure' in single quotes - some
people (not me) consider the VisualBasic namespace as not .NET.

Once again, I'll state that AscW and ChrW are perfectly valid and there is
reason to choose it over the 'pure' .NET equivalents (not the single
quotes...).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code
"Cor Ligthert [MVP]" wrote:
David,
I was just stating another reason that someone might want to use AscW and
ChrW rather than trying to find 'pure' .NET equivalents.
--


AscW and ChrW are 'pure' .Net, you are mixing up the namespace system.Net
with the .Net framework, somethig the same as some people think that .Net is
only for InterNet.

Cor

Nov 21 '05 #15
On 2005-08-30, David Anton <Da********@discussions.microsoft.com> wrote:
I wasn't disagreeing with anything Mattias said.
I'm not disputing the compile-time nature of the calls.
I know, I was just filling out some details. The compile-time nature of
keywords and expressions are an interesting aspect of VB and I generally
find that newer users are either confused by them or don't know they
exist at all (assuming everything's a function call).
I was just stating another reason that someone might want to use AscW and
ChrW rather than trying to find 'pure' .NET equivalents.


I find it humorous that you used quotes here and people still jumped all
over the word "pure".
Nov 21 '05 #16
On 2005-08-30, Cor Ligthert [MVP] <no************@planet.nl> wrote:
Mike,

See your subject, don't mix up

System.Net namespace
.Net framework (containing much more than only the System.Net namespace)
InterNet


I seriously doubt that anybody here is confusing those things. You can
jump all over nomenclature, but everybody knows exactly what the OP is
asking: what's the System.* namespace equivalent to a function found
found in Microsoft.VisualBasic.*?

It's a perfectly legitimate and perfectly understandable query, and
sometimes there's reasons to use MS.VisualBasic and sometimes there's
reasons to avoid it. It's a complex topic, and certainly not one
appropriate to the religious conviction some people seem to have on
the topic.

In this case, the original subject is even more clear, since it turns
out he's not asking about a function, but about what is esentially a
VB.Net keyword. And to my mind, it's a bit of a stretch to say that a
VB keyword is part of the .Net framework.

Nov 21 '05 #17
> I find it humorous that you used quotes here and people still jumped all
over the word "pure".


Though it would have been funnier if he had said "pure virtual" :)

--
Peace & happy computing,

Mike Labosh, MCSD
"Musha ring dum a doo dum a da!" -- James Hetfield
Nov 21 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Job Lot | last post by:
I have an enumeration as follows Public Enum Delimiters Tab Semicolon Comma Space End Enum How can I return character equivalent of the elements in the enumeration?
6
by: VB Programmer | last post by:
I have a line of code in my project: mobjText.Append(ChrW(Bytes(iIndex))) I took out the reference to Microsoft.VisualBasic in my imports so that I am forced to use all VB.NET stuff. What is...
7
by: Zytan | last post by:
I am trying to set a const string made up of Chr(), but if the value passed into Chr() is too large, the compiler complains that it is not constant!! Example: Public Const m_Data As String =...
17
by: AWW | last post by:
Using Textbox.Text = "123" & CStr(13) & "456" I expect 2 lines on the screen but get 1 line with 13 in it. It worked once in a RichTextBox but then stopped. I just want to display aligned tabular...
3
Scott Price
by: Scott Price | last post by:
Hello again... This time I'm trying to get M$ Access 2003 VBA to accept the ChrW(215b) (this is the unicode number for the 1/8 character) standard... Each time I try to enter this, it beeps and...
25
by: cmdolcet69 | last post by:
Public Value As Integer = 4096 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim bytes As Byte() = BitConverter.GetBytes(Value) End...
6
by: =?Utf-8?B?UmljaGFyZA==?= | last post by:
VB has the AscW function. There is no corresponding C# function. How can I do this in C#? I do not want to reference the Microsoft.VisualBasic namespace for just this function.
4
by: David Griffiths | last post by:
Hi all I thought I would convert an old program to use just .net, removing the Microsoft.Visual Basic namespace. I seem to have succeeded except for just one are. What is the .net equivalent...
2
by: Joe Duchtel | last post by:
Hello - I was wondering why I can do a ... Public Const A As Char = Chr(36) .... but not a ... Public Const B As Char = Chr(165)? It seems like 0-127 are okay with the Chr(). Is this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.