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

Type Conversion

P: n/a
I would like to know if there is any equivelent to the
(as) keyword in c# which will try to convert a variable to
a specific type and assign it to null if it faild ...
(does not throw an excpetion as CTYPE.

If not ... can you tell me how can I achieve the same in
VB.Net

Regards
Nov 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Hi zsilence,

Someone was asking about 'as' just a couple of days ago. VB.NET hasno
'as', unfortunately, but here's the example that I showed then.

C#
object o = 24;
SomeSub (o as string); // passes null
string s = o as string; // s = null
VB
Dim o As Object
o = 24
Dim s As String
If o.GetType Is GetType(String) = False Then
s = Nothing
Else
s = o
End If
SomeSub (s) 'Needs the whole If statement first.

Regards,
Fergus
Nov 20 '05 #2

P: n/a
Cor
Hi Fergus,
I was looking at your code and then I thought, can that not nicer and I did
made this
\\\\\
Dim o As Object
o = 24
somesub(ZeroNothing(o)) ' passes null
End Sub
Public Function ZeroNothing(ByVal o As Object) As String
If Not o Is Nothing Then
If o.GetType Is GetType(String) = True Then
Return o.ToString
End If
End If
Return Nothing
End Function

(Added something to keep of Armin from our back too :-)

I don't understand the problem but I think this is nicer code

Cor
Nov 20 '05 #3

P: n/a
Hello,

"zsilence_is" <zs*********@hotmail.com> schrieb:
I would like to know if there is any equivelent to the
(as) keyword in c# which will try to convert a variable to
a specific type and assign it to null if it faild ...
(does not throw an excpetion as CTYPE.


This question has been discussed some days ago (complete thread):

http://groups.google.de/groups?selm=...TNGP12.phx.gbl

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #4

P: n/a
Howdy Cor,

Although my example was specific to 24 and String, in the general case you
<do> need to check for Nothing first.

I'll just refine it a bit more. :-)

Public Function CastOrNothing (ByVal o As Object) As String
If o Is Nothing OrElse Not o.GetType Is GetType(String) Then
Return Nothing
End If
Return o.ToString
End Function

How about this one?

Public Function CastOrNothing (ByVal o As Object) As String
If Not o Is Nothing _
AndAlso o.GetType Is GetType(String) _
Then Return o.ToString _
Else Return Nothing
End Function

Does that make you shudder? :-)

The trouble is that these only work for Strings. This is a function
waiting for VB.NET's new generics.

Regards,
Fergus
Nov 20 '05 #5

P: n/a
Guys .. This wont do ... very simply reason is that ..
both types should not be 100% equal .. I can still ctype
an int to double.. This wouldnt do .. cause they are not
the same type ...

What do u think ?

-----Original Message-----
Howdy Cor,

Although my example was specific to 24 and String, in the general case you<do> need to check for Nothing first.

I'll just refine it a bit more. :-)

Public Function CastOrNothing (ByVal o As Object) As String If o Is Nothing OrElse Not o.GetType Is GetType(String) Then Return Nothing
End If
Return o.ToString
End Function

How about this one?

Public Function CastOrNothing (ByVal o As Object) As String If Not o Is Nothing _
AndAlso o.GetType Is GetType(String) _
Then Return o.ToString _
Else Return Nothing
End Function

Does that make you shudder? :-)

The trouble is that these only work for Strings. This is a functionwaiting for VB.NET's new generics.

Regards,
Fergus
.

Nov 20 '05 #6

P: n/a
Hi Z,

I think I don't understand the question. :-)

|| both types should not be 100% equal

??

|| I can still ctype an int to double.. This wouldnt do
|| .. cause they are not the same type ...

Are you saying that you want this to be null too?
'as' in C# only works for objects anyway.
How about this alternative:

Try
s = DirectCast (x, FooType) 'or CType()
Catch
s = Nothing
End Try

Regards,
Fergus
Nov 20 '05 #7

P: n/a
I think I have no any other choice but to use catch and
exception .. though I know that it is the worse I can
do... But it seems that there is no better solution for
that :(
-----Original Message-----
Fergus,
That "try catch end try" was my first thought, but someone told me that itis bad programming.
( I testet what you said with a double in a class, but with such a classspecialist as you are I wont send what I did, but you can make somethingvery nice from it, I am sure)
somesub(cst.dbl(0))
somesub(cst.str(0))
This are not my favorite problems, I alway says you have bytes whichcontains bits.
I do not like types (but have to life with it)

Cor
.

Nov 20 '05 #8

P: n/a
Hi Cor,

|| That "try catch end try" was my first thought, but someone
|| told me that it is bad programming.

I only gave it because Z isn't satified yet. :-)

Using Exceptions for normal coding is seen as bad practice for a few
reasons as far as I know.

Firstly because they should be reserved for things that actually <are>
exceptional rather than used for doing 'tricks'.

Secondly the mechanism they use is, at a lower level, a bit like electric
shock treatment - in this case a jolt to the stack. That's if I understand it
correctly. Using C, under Unix, there was a function called setjump() which
would put a marker in the stack. When something bad happened, you could call
longjump() which would 'wipe out' the stack and return control directly to
where setjump() had been called. I'm sure that it's not that crude with
Exception handling but it <is> an abnormal interruption.

Thirdly, they are comparitively slow to execute - definitely not for heavy
use within the regular flow of program logic.

|| ... I alway says you have bytes which contains bits.

Talking of bites. I need to go and have a bit to eat. :-)

See you,
Fergus
Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.