Click to See Complete Forum and Search --> : Ctype in VB.Net
Josch
Aug 4th, 2002, 06:25 AM
Is there a function in the system.convert namespace that equals the ctype function ?
For example the Cdbl function is equal to the system.convert.todouble function.
Cander
Aug 5th, 2002, 08:30 AM
System.Convert.ChangeType(object, typetochangeto)
Josch
Aug 5th, 2002, 04:23 PM
Give me an example.
I have this piece of code:
Dim objControl As Control
Dim objPicBox As PictureBox
objPicBox = CType(objControl, PictureBox)
Show me how to do ....
Buy2easy.com
Aug 5th, 2002, 04:52 PM
you can still use ctype.
Dim objControl As Control
Dim objPicBox As PictureBox
objPicBox = CType(objControl, PictureBox)
i believe that will work!?!
But i could be wrong, i do use it in my current application though
Tygur
Aug 5th, 2002, 11:03 PM
What's wrong with just using CType?
Cander
Aug 6th, 2002, 08:27 AM
you use it the EXACT same way as CType
one reason not to use CType is that it is a part of the Microsoft.VisualBasic namespace that was added for backwards compatability. I myself try to avoid it becuase it is 1 less reference I need since that dll provides me nothing I cant do with the standard .NET classes.
Josch
Aug 6th, 2002, 12:16 PM
Hey Cander,
did you ever try this ?
Check it out and you will see that it can't be used in the same way.
system.convert.changetype(objControl, PictureBox) does not work !
So try it and tell me how to do ... ( if you can )
Cander
Aug 6th, 2002, 12:26 PM
hmm no I didnt try it, but according to the win class viewer, that is all you should have to do. Ill have to try it tomorrow when I get back to work.
Cander
Aug 6th, 2002, 12:29 PM
ohh.try this
System.Convert.ChangeType(objcontrol, TypeOf(PictureBox))
maybe that will work..I dont have .NET where I am now, so I cant test.
Tygur
Aug 6th, 2002, 12:42 PM
Originally posted by Cander
one reason not to use CType is that it is a part of the Microsoft.VisualBasic namespace that was added for backwards compatability.
That's not true. There's absolutely nothing wrong with using that namespace. The one to avoid that's there for backwards compatibility is the Microsoft.VisualBasic.Compatibility namespace.
The Microsoft.VisualBasic namespace is an integral part of the VB.NET language. Check the documentation. Nowhere does it say that it's only for backwards compatibility. There's also a Microsoft.CSharp namespace. Is that one supposed to be for backwards compatibility, too?
Tygur
Aug 6th, 2002, 12:50 PM
Also, CType isn't even found in Microsoft.VisualBasic to begin with. This quote is straight out of Microsoft's documentation:
CType is compiled inline, meaning the conversion code is part of the code that evaluates the expression. Execution is faster because there is no call to a procedure to accomplish the conversion.
This means that all these substitutes you people are cooking up are actually slower.
CType is just VB.NET's implementation of casting. There's no reason to avoid it, just like there's nothing wrong with this line in C#:
int i=(int)longVariable;
Josch
Aug 6th, 2002, 12:53 PM
TypeOf(PictureBox) does not work
Cander
Aug 7th, 2002, 08:27 AM
Originally posted by Tygur
Also, CType isn't even found in Microsoft.VisualBasic to begin with. This quote is straight out of Microsoft's documentation:
This means that all these substitutes you people are cooking up are actually slower.
CType is just VB.NET's implementation of casting. There's no reason to avoid it, just like there's nothing wrong with this line in C#:
int i=(int)longVariable;
Ok Im a retard! :(
I had assumed it was part of the backwards compat namespace....
PT Exorcist
Aug 7th, 2002, 05:24 PM
is there any option to put off the visual basic namespace so i just can't use its functions? it would be a good way to me get used to dont use it anymore
Cander
Aug 7th, 2002, 05:38 PM
I found out something interesting today. The compiler already refs that namespace built into it, so it raelly doesnt matter.
ljlevend
Aug 7th, 2002, 11:02 PM
Originally posted by PT Exorcist
is there any option to put off the visual basic namespace so i just can't use its functions? it would be a good way to me get used to dont use it anymore
Yes! Just Right-Click on your project name in the Solution Explorer, then select Properties - Imports. In the “Project Imports:” list, delete Microsoft.VisualBasic. I strongly recommend adding the Microsoft namespace so that you don't have to include "Microsoft." in front of everything from the VisualBasic namespace. I did this early on in the program that I'm working on and I've NEVER regretted it. No more accidental MsgBox's for this guy :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.