By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,827 Members | 813 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,827 IT Pros & Developers. It's quick & easy.

Casting Help Needed

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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" <br***********@yahoo.com> escribió en el mensaje
news:Oc**************@TK2MSFTNGP10.phx.gbl...
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

Nov 21 '05 #2

P: n/a
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
Nov 21 '05 #3

P: n/a
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
Nov 21 '05 #4

P: n/a

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" <br***********@yahoo.com> wrote in message
news:Oc**************@TK2MSFTNGP10.phx.gbl...
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

Nov 21 '05 #5

P: n/a
"Brian" <br***********@yahoo.com> schrieb

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

Nov 21 '05 #6

P: n/a
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.

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

Nov 21 '05 #7

P: n/a
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
"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
| "Brian" <br***********@yahoo.com> schrieb
| >
| > 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
|
Nov 21 '05 #8

P: n/a
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb
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
Nov 21 '05 #9

P: n/a
>
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
Doing a conversion creates a
new object.


Nov 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.