Casting Help Needed

  • Thread starter Thread starter Brian
  • Start date Start date
B

Brian

Hello!

What is the proper syntax for casting? For example, how do I change an
Integer in to a String if the variable is called Joe1 and has 20 assigned to
it.

Thanks,
Brian
 
Hi Brian,

The casting operators in VB.NET are DirectCast (if the object is already of
the requested type, such a base or an interface) or CType(when a type
conversion is required), but in your case of strings and integers:

- For conversions to String, you can use the ToString() method:

Dim s As String
Dim Joe1 As Integer = 20

s = Joe1.ToString() ' Notice that this is an overloaded method that can
receive the format as parameter

- For the converse operation, you use the Parse method of integers (with
proper exception handling omitted here):

Dim Joe2 As Integer

Joe2 = Integer.Parse(s)

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
Brian,

Just as you like it

mystring as string
mystring = Joe1.ToString
mystring = Cstr(Joe1)
mystring = Ctype(joe1, String)
mystring = Convert.ToString(Joe1)

I use Joe1.ToString

I hope this helps,

Cor
 
Technically, the example you give isn't called casting:

Dim Joe1 as integer = 20
Dim theString as string

theString = Joe1.ToString ()



..... and back again you can try this:

Joe1 = Integer.Parse ( theString )
 
Brian said:
What is the proper syntax for casting? For example, how do I change
an Integer in to a String if the variable is called Joe1 and has 20
assigned to it.


That's not called casting. It's a conversion. Casting changes the type of an
expression but does not create a new object. Doing a conversion creates a
new object.

dim Joe1 as integer = 20
dim s as string

s = Joe1.ToString


Armin
 
Brian,

I forgot to tell it is converting not casting. In C# the call everything
casting as I do when I am playing with my angle because I never catch a
fish.

Cor
 
No problem...I am using a very good study book that has now asked me to turn
on STRICT TYPING and I am having a devil of a time now with it.
 
Armin,
| Casting changes the type of an
| expression but does not create a new object.
I need to remember that one!

Hope you don't mind if I use it later...
Jay


| >
| > What is the proper syntax for casting? For example, how do I change
| > an Integer in to a String if the variable is called Joe1 and has 20
| > assigned to it.
|
|
| That's not called casting. It's a conversion. Casting changes the type of
an
| expression but does not create a new object. Doing a conversion creates a
| new object.
|
| dim Joe1 as integer = 20
| dim s as string
|
| s = Joe1.ToString
|
|
| Armin
|
 
Jay B. Harlow said:
Armin,
| Casting changes the type of an
| expression but does not create a new object.
I need to remember that one!

Hope you don't mind if I use it later...

No, not at all. :)

Armin
 
That's not called casting. It's a conversion. Casting changes the type of an
expression but does not create a new object.

Yes, it is the same memory space, but interpreted
differently.
And there is something about the size of the two
operands, also. They must either be the same size,
or the 'cast to' can not be more than the 'cast from'.
Roger
 

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

Back
Top