Beginning C#

M

Michael D. Ober

I am attempting to learn C# and have run into a fundamental type casting
problem for input strings. After reviewing the VS 2005 documentation, I am
obviously missing simple type conversion operators - specifically, string
(textbox input values) to numbers of various types. In C++ this would be
done with sscanf and in both VB6 and VB 2005 this would be done with either
C<type> (CDbl, CLng, etc.) or with the generic VB 2005 CType(textbox1.text,
Long).

My question is two fold - first what are the direct equivalents to the CDbl
and CLng series of VB functions and secondly, what is the general type
caster replacement for CType?

Thanks,
Mike Ober.
 
N

Nicholas Paldino [.NET/C# MVP]

Michael,

In all of the methods that you have mentioned, the string was not cast
to the type that you wanted. Rather, those are functions that perform a
conversion.

These functions exist in .NET as well. They are static methods on the
Convert class, such as ToInt32, ToInt64, etc, etc.

They are available to all languages in .NET as well, since they are part
of the framework.

Hope this helps.
 
O

Omatase

I don't know that there are direct replacements.

The best tools for casting in C# that I have found are in
System.Convert.

For instance:

System.Convert.ToInt32("3");

casting like:

object myObject = (int)15;

for the sake of argument, works as well (let's ignore for the moment
that it's something you would likely never do)
 
M

Michael D. Ober

Thanks - VB 2005 doesn't require this class, though I suspect it converts
the VB conversion functions into calls into the Convert class.

Mike.

Nicholas Paldino said:
Michael,

In all of the methods that you have mentioned, the string was not cast
to the type that you wanted. Rather, those are functions that perform a
conversion.

These functions exist in .NET as well. They are static methods on the
Convert class, such as ToInt32, ToInt64, etc, etc.

They are available to all languages in .NET as well, since they are part
of the framework.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michael D. Ober said:
I am attempting to learn C# and have run into a fundamental type casting
problem for input strings. After reviewing the VS 2005 documentation, I
am
obviously missing simple type conversion operators - specifically, string
(textbox input values) to numbers of various types. In C++ this would be
done with sscanf and in both VB6 and VB 2005 this would be done with
either
C<type> (CDbl, CLng, etc.) or with the generic VB 2005
CType(textbox1.text,
Long).

My question is two fold - first what are the direct equivalents to the
CDbl
and CLng series of VB functions and secondly, what is the general type
caster replacement for CType?

Thanks,
Mike Ober.
 

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