C# equivelant of VB.NET's Asc()

M

Michel Posseth [MCP]

Arne Vajhøj said:
OK.

But to me it is still a function that only exists for compatibility
reasons and is a procedural leftover in an object oriented world.

But that is of course not a technical view.

Arne



No it is VB syntax that is something completely different as compatibility,
it is pointed out several times by the Development team of VB
that all compatibility dll`s are hosted in the
Microsoft.VisualBasic.Compatibility namespace what is the "normal" VB
namespace is VB syntax the essence of the language the thing that makes
VB.Net .

I also do not see your OOP point as the VB namespace is a classic example of
good usage of OOP , and if you do not want to have the helper methods and
contstants that VB provides you have the posibility to remove the VB
namespace completely , however this will make VB a non rad dev environment
just as all other "new" languages out there remember that VB has a proven
language history as a rad and that the complete late binding mechanism of
VB is part of the VB namespace .


regards

Michel
..


Michel
 
J

Joergen Bech

Or if he wants unicode, which you normally would in .NET ...

The poster specifically asked how he could convert the Asc
function to C#. That, to me, suggests that he wants to translate
existing, working code, rather than creating something new from
scratch in order to satisfy someone's requirements for doing things
the "right" way.

If he wants *exactly* the same conversion, he can copy the
whole function (thereby avoiding having to include the namespace)
or - if the characters are known to be <0x80, get equivalent
functionality by grabbing the first part only.

So as I see it, there are two discussions going on in this thread:
How to do a direct translation or how to obtain similar results
some other way. Any of the latter would require testing and
documentation of differences in behavior.

If the requirement is Asc with Asc being what it is, then I fail
to see how int(c) alone would satisfy this requirement?!?
But as you can see from using .Net Reflector (below), the full
Asc(char) function is a little more elaborate than that.
public static int Asc(char String)
{
int num;
int num2 = Convert.ToInt32(String);
if (num2 < 0x80)
{
return num2;
}
try
{
byte[] buffer;
Encoding fileIOEncoding = Utils.GetFileIOEncoding();
char[] chars = new char[] { String };
if (fileIOEncoding.IsSingleByte)
{
buffer = new byte[1];
int num3 = fileIOEncoding.GetBytes(chars, 0, 1, buffer,
0);
return buffer[0];
}
buffer = new byte[2];
if (fileIOEncoding.GetBytes(chars, 0, 1, buffer, 0) == 1)
{
return buffer[0];
}
if (BitConverter.IsLittleEndian)
{
byte num4 = buffer[0];
buffer[0] = buffer[1];
buffer[1] = num4;
}
num = BitConverter.ToInt16(buffer, 0);
}
catch (Exception exception)
{
throw exception;
}
return num;
}

Yuck - a piece of code.

Do you dislike code examples in general or just that piece
of code?

I did not write it. That is what it looks like in the framework
(never mind the variable names).

Regards,

Joergen Bech
 
J

Joergen Bech

OK.

But to me it is still a function that only exists for compatibility
reasons and is a procedural leftover in an object oriented world.

But that is of course not a technical view.

Arne

Asc(char) is a static/shared method of the
Microsoft.VisualBasic.Strings class, so saying that
it is a procedural leftover in an object oriented world
is the same as saying that all public static/shared
methods are procedural leftovers.

I am fairly sure that it would be difficult to write a
pure oo program given such a criteria.

Regards,

Joergen Bech
 
A

Arne Vajhøj

Joergen Bech said:
Asc(char) is a static/shared method of the
Microsoft.VisualBasic.Strings class, so saying that
it is a procedural leftover in an object oriented world
is the same as saying that all public static/shared
methods are procedural leftovers.

I am fairly sure that it would be difficult to write a
pure oo program given such a criteria.

When I call a shared method in VB.NET i use the syntax:

classname.methodname(argument)

are you saying that I in general can just use methodname(argument) ??

Arne
 
A

Arne Vajhøj

Joergen Bech said:
The poster specifically asked how he could convert the Asc
function to C#. That, to me, suggests that he wants to translate
existing, working code, rather than creating something new from
scratch in order to satisfy someone's requirements for doing things
the "right" way.

If he wants *exactly* the same conversion, he can copy the
whole function (thereby avoiding having to include the namespace)
or - if the characters are known to be <0x80, get equivalent
functionality by grabbing the first part only.

So as I see it, there are two discussions going on in this thread:
How to do a direct translation or how to obtain similar results
some other way. Any of the latter would require testing and
documentation of differences in behavior.

In general I think it is bad to port a pre-.NET design to
..NET 1:1.
If the requirement is Asc with Asc being what it is, then I fail
to see how int(c) alone would satisfy this requirement?!?

I don't think anyone has said anything differently.
But as you can see from using .Net Reflector (below), the full
Asc(char) function is a little more elaborate than that.
public static int Asc(char String)
{
int num;
int num2 = Convert.ToInt32(String);
if (num2 < 0x80)
{
return num2;
}
try
{
byte[] buffer;
Encoding fileIOEncoding = Utils.GetFileIOEncoding();
char[] chars = new char[] { String };
if (fileIOEncoding.IsSingleByte)
{
buffer = new byte[1];
int num3 = fileIOEncoding.GetBytes(chars, 0, 1, buffer,
0);
return buffer[0];
}
buffer = new byte[2];
if (fileIOEncoding.GetBytes(chars, 0, 1, buffer, 0) == 1)
{
return buffer[0];
}
if (BitConverter.IsLittleEndian)
{
byte num4 = buffer[0];
buffer[0] = buffer[1];
buffer[1] = num4;
}
num = BitConverter.ToInt16(buffer, 0);
}
catch (Exception exception)
{
throw exception;
}
return num;
}
Yuck - a piece of code.

Do you dislike code examples in general or just that piece
of code?

That piece of code.

Arne
 
M

Michel Posseth [MCP]

Well there is lack of an agreed-upon and rigorous definition of OOP
and method(arg) syntax seems to be one of them
 
N

Nathan Sokalski

You say that the value is "compared to a char value". Would you mind telling
me where I do this? If you are thinking of typedchar, that is not a char
(actually, it doesn't even exist in the final result). In the last few lines
of my code, you will notice that I replace the text "typedchar" with either
event.keyCode or event.which, depending on the browser. If you look at what
values the event.keyCode and event.which have, you will see why I do not
want the Unicode value.
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/

No, I do not want the Unicode value. I do know the difference between
Asc()
and AscW() (Although I do appreciate that you took into account that it is
an easy mistake to make), and I am looking for the equivelant of Asc(). My
planned use is for generating JavaScript (You can see the VB.NET version
of
the code I am trying to convert on my site
athttp://www.nathansokalski.com/code/RestrictInputMethod.aspx).

You use Asc() to generate an integer literal that is inserted into
JavaScript, where it is compared to a char value. Given that
JavaScript uses Unicode strings the same way .NET does, it would seem
that you do indeed need the Unicode version, unless I'm missing
something else.

Anyway, assuming you really do need Asc() and nothing else, the C#
equivalent would be Encoding.Default.GetBytes(ch)[0] - but that is
ignoring the fact that "ch" might be a multibyte character.
 
N

Nathan Sokalski

I haven't tested it yet, but does anybody think the following would work:

System.Text.ASCIIEncoding charcode=new System.Text.ASCIIEncoding();
byte asciivalue=charcode.GetBytes(acceptchar)[0];
 
J

Jon Skeet [C# MVP]

Nathan Sokalski said:
I haven't tested it yet, but does anybody think the following would work:

System.Text.ASCIIEncoding charcode=new System.Text.ASCIIEncoding();
byte asciivalue=charcode.GetBytes(acceptchar)[0];

For genuinely ASCII characters, that will give the same result as just
casting the character. For non-ASCII characters, I believe it will
return 3F ('?').

(If I *were* to use ASCIIEncoding, I'd suggest the Encoding.ASCII
property instead of creating a new one, btw.)
 
J

Joergen Bech

When I call a shared method in VB.NET i use the syntax:

classname.methodname(argument)

are you saying that I in general can just use methodname(argument) ??

Arne

An example from the VB world:

---snip---
Imports System.Drawing.Graphics
....
Private Sub Test()
Dim bm As New Bitmap("c:\test.bmp")
Dim g As Graphics = FromImage(bm) '<<<<<<<<<<<<
End Sub
---snip---

instead of

---snip---
Private Sub Test()
Dim bm As New Bitmap("c:\test.bmp")
Dim g As Graphics = Graphics.FromImage(bm) '<<<<<<<<<
End Sub
---snip---

When you create a WinForms VB project, System.Drawing is already
included as a reference, but System.Drawing.Graphics is not.

In the last example above, you would have to prefix the Graphics
class to access the shared method.

One thing I will admit, though: Asc can be accessed using
Microsoft.VisualBasic.Asc and
Microsoft.VisualBasic.Strings.Asc.

Strings is a module, so in this case I suppose you are right,
though in practical terms I do not see much of a difference
between accessing Asc from a module and - as in the example
above - FromImage directly from a class by importing the
Graphics namespace.

Regards,

Joergen Bech
 
J

Joergen Bech

---snip---
In general I think it is bad to port a pre-.NET design to
.NET 1:1.

Agreed. And on the same note, Microsoft should never have
provided a VB6->VB.Net upgrade wizard, but I suppose they
had to in order to give (some) VB6 developers the illusion that
VB6 to VB.Net was a natural migration path and keep them
from jumping to something else entirely.
I don't think anyone has said anything differently.
Nope.

---snip---


That piece of code.

OK. Plenty where that came from.

/Joergen Bech
 
J

Joergen Bech

On Sun, 29 Jun 2008 08:48:23 +0200, Joergen Bech

---snip---
One thing I will admit, though: Asc can be accessed using
Microsoft.VisualBasic.Asc and
Microsoft.VisualBasic.Strings.Asc.

Strings is a module, so in this case I suppose you are right,
though in practical terms I do not see much of a difference
between accessing Asc from a module and - as in the example
above - FromImage directly from a class by importing the
Graphics namespace.

Doh! Remind me not to post on a Sunday. My head is
not working today. Let me correct myself before someone
else does:

Strings is a class, of course - just stuffed in a module.

From http://discuss.joelonsoftware.com/default.asp?dotnet.12.353213.14

"A module IS just a class where Shared is implicitly understood for
each member. And the module name doesn't need to be supplied when the
members are used. That's it."

So Asc is just as OO (or not) as the rest of the framework.
Technically the same. Just a difference of style.

Regards,

Joergen Bech
 
M

Michel Posseth

Nathan Sokalski schreef:
VB.NET has a function, Asc(), that gets the Ascii value of a character. I am
attempting to find an equivelant function for C#. Can somebody help me here?

Nathan

Currently i am sitting behind my dev laptopn wich has VS.Net 2008 and
Sharp develop installed

one of the nice features of sharp develop is that it can translate code
back and forward from VB to C# or BOO and vice versa ( maybe you should
have a look at it )


when i say in VB.Net

Private Sub test
Dim i As Integer = Asc("A")
End Sub

this is translated to C# and BOO as
C#
private void test()
{
int i = Strings.Asc("A");
}
BOO
private def test():
i as int = Strings.Asc('A')


In VS.Net 2008

this code ( note that i needed the value thrown in asc to be a non
constant value otherwise the IL that is generated is for this one
constant and you do not see the actuall call)

Private Sub test()
Dim i As Integer = Asc(My.Settings.testval)
MsgBox(i.ToString)
End Sub

becomes after compiling and decompiling with Reflector

Private Sub test()

Interaction.MsgBox(Strings.Asc(MySettingsProperty.Settings.testval).ToString,
MsgBoxStyle.OkOnly, Nothing)
End Sub

and in C#

private void test()
{

Interaction.MsgBox(Strings.Asc(MySettingsProperty.Settings.testval).ToString(),
MsgBoxStyle.OkOnly, null);
}

Conclusion ?

Asc(My.Settings.testval) = strings.asc(My.Settings.testval)

HTH

Michel Posseth
 
C

Cor Ligthert[MVP]

Michiel,

I don't know if you are aware that there is in C# a strict distinct between
a char and a string.

Even a sql nchar(1) will in a datacontext be used as a Char

There is in VB as well a Char, however beside the IndexOff in an array I
don't remember that I have ever used that.

"X"c

Cor
 
M

Michel Posseth

Cor ,

Yes i am aware of this fact however the strings.Asc method is overloaded
just as VB`s ASC function so the IDE doesn`t care although i do code
strict

:)

But for the unknowing it might be good info to know
regards
michel

Cor Ligthert[MVP] schreef:
 
J

Joergen Bech

On Sun, 29 Jun 2008 17:57:56 +0200, Michel Posseth

---snip---
Conclusion ?

Asc(My.Settings.testval) = strings.asc(My.Settings.testval)

HTH

Michel Posseth

Yes, of course it is.

Asc is a public shared method of the

Microsoft.VisualBasic.String

class.

But to use it in C# you would still have create a reference to the
Microsoft.VisualBasic namespace in order to make use of Asc
(or Strings.Asc, if you like).

Regards,

Joergen Bech
 
J

Joergen Bech

A Char is a Char no matter if the language is
C# or VB.Net.

Whatever distinction there is between a Char and a String in
C# exists in VB.Net as well.

Regards,

Joergen Bech
 
H

Herfried K. Wagner [MVP]

Joergen Bech @ post1.tele.dk> said:
A Char is a Char no matter if the language is
C# or VB.Net.

Whatever distinction there is between a Char and a String in
C# exists in VB.Net as well.

Note that VB allows an implicit widening conversion between 'Char' and
'String' which C# does not support.
 
M

Michel Posseth

yes it is that or

char[] chrBuffer = { Convert.ToChar(strChar) };
byte[] bytBuffer = Encoding.Default.GetBytes(chrBuffer);
return (int)bytBuffer[0];

michel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top