Type Conversion

  • Thread starter Thread starter zsilence_is
  • Start date Start date
Z

zsilence_is

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
 
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
 
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
 
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
 
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 ?
 
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
 
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 :(
 
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
 
Back
Top