473,506 Members | 14,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Type Conversion

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
8 2740
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1670
by: Mark Oliver | last post by:
Hi, I want to put a type conversion in my class, but I don't want the conversion to be usable in a passed parameter because it makes no sense. class cData { string s; public cData(string s)...
7
2232
by: Madhu Gopinathan | last post by:
Hi, I hope this is the right forum for this question. I am extending ICollection to create a Collection Type (say MyCollection) wherein I can control the types of objects being added to the...
27
5591
by: Yuriy Solodkyy | last post by:
Hi VS 2005 beta 2 successfully compiles the following: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
3
1873
by: pgconnolly | last post by:
/* foreach does implicit type conversion on elements of a params argument or Generic.List. * This is not good. * Examples of evil follow... */ using System; // I love it when C# is strict...
16
12772
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
2
1872
by: Martin v. Lwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version:...
1
3256
by: lovecreatesbeauty | last post by:
There is a warning/(error? I remember it is an error for line 10 on some compilers before. At least on g++, it is an error.) for line 10. I first read a similar example from `Expert C Programming...
669
25394
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
4
2088
by: zaeminkr | last post by:
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) :...
8
3132
by: Smithers | last post by:
Are there any important differences between the following two ways to convert to a type?... where 'important differences' means something more profound than a simple syntax preference of the...
0
7220
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7105
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7479
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.