Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2004

Tip: Looking for answers? Try searching our database.

CType question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Filip D'haene - 29 Oct 2004 20:44 GMT
Hi,

I want to make a function like CType but can't figure out how to do this.

Function ConvertWhithoutException(ByVal Item As Object, ByVal aType As Type)
As Object
   Dim output As Object = Nothing
   Try
       output = CType(Item, aType)
   Catch ex As Exception
   End Try

   Return (output)
End Function

But this doesn't compile.
Does anyone know how to make a function I can use in the same manner that I
can use the CType-function?

Thanks,

Filip
Mattias Sj?gren - 29 Oct 2004 21:52 GMT
Filip,

>I want to make a function like CType but can't figure out how to do this.

The short answer is that you can't.

>    Catch ex As Exception
>    End Try

You should never do this.

>Function ConvertWhithoutException(ByVal Item As Object, ByVal aType As Type)
>As Object
...
>    Return (output)

And even if you could do what you want, what good would it do when the
return type of your function is Object? Then you'd still have to cast
the function's return value to the appropriate type.

What you probably should do is guard your CType by first using the
TypeOf..Is operator to check if the cast will succeed. In VB 2005 you
can also use the TryCast operator.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Cor Ligthert - 30 Oct 2004 08:56 GMT
Filip,

In addition to Mattias

It makes in my opinion no sense what you want to do, you are not the first
one who wants to try to make a kind of "Variable" in the VBNet.

It makes no sense because doing that is the reason why scripting language
are slow, it needs mostly a lot of useless processing time while you know
exactly the object. When you want what you ask, than use a scripting
language or use late binding. What is in fact the same.

Late Binding searches for your proper object at run time and is therefore
much slower.
When your object has no match with the needed object it will of course even
not work.

You set latebinding on by  Option Strict Off. What means in the current
VBNet versions by not setting it to On. That while the last is always
advices in the dotNet newsgroup because than there is with a VBNet program
no difference in performance with a C# program.

I hope this gives some idea's?

Cor.

"Filip D'haene" <filip.dhaene@apscomputers.be>

> Hi,
>
[quoted text clipped - 18 lines]
>
> Filip
Jay B. Harlow [MVP - Outlook] - 30 Oct 2004 17:16 GMT
Filip,
> Does anyone know how to make a function I can use in the same manner that
> I can use the CType-function?
Have you looked at System.Type.ChangeType?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fsystemconvertclasschangetypetopic.asp


>    Catch ex As Exception
>    End Try
I agree with Mattias, this is rarely a good idea...

Be certain to read the documentation for Convert.ChangeType.

Hope this helps
Jay

> Hi,
>
[quoted text clipped - 18 lines]
>
> Filip

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.