473,329 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,329 software developers and data experts.

CType question

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
Nov 21 '05 #1
3 1376
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2
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" <fi**********@apscomputers.be>
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

Nov 21 '05 #3
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/de...etypetopic.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

"Filip D'haene" <fi**********@apscomputers.be> wrote in message
news:%2*****************@TK2MSFTNGP11.phx.gbl... 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

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Douglas Buchanan | last post by:
I am using the following code instead of a very lengthly select case statement. (I have a lot of lookup tables in a settings form that are selected from a ListBox. The data adapters are given a...
3
by: Mark Kamoski | last post by:
Hi-- What is the difference between Convert.ToString(obj) and CType(obj, String)? (Assume obj is a variable of type Object.) Please advise. Thank you.
16
by: Craig Buchanan | last post by:
If I am trying to cast an object to one of its decendants, will the decendants' default constructor (New sub) fire? Thanks, Craig
7
by: Brian Henry | last post by:
is there any speed diffrences between doing Ctype or directcast? I know about the inherite diffrences, but process usage time wise, does one take up more cycles then the other? thanks
3
by: FB's .NET Dev PC | last post by:
I'm trying to create a general-purpose routine to update a value in a data location on a remote server. The client-server protocol is OPC, but that isn't immediately important to my question. The...
7
by: Joe | last post by:
Hello All: Does anyone know the difference between CType(x,Integer) vs. Integer.Parse(x)? Is there a performance advantage to one or the other? TIA, -- Joe
5
by: c_shah | last post by:
Very beginner question.. What's difference between cstr vs .ToString vs Ctype for converting to String?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.