473,661 Members | 2,502 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 2748
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(ZeroNot hing(o)) ' passes null
End Sub
Public Function ZeroNothing(ByV al 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_i s" <zs*********@ho tmail.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.db l(0))
somesub(cst.st r(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
1679
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) { this.s=s;
7
2238
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 collection. Thus, my interface now looks like this public interface IMyCollection : ICollection { void Add (string toBeAdded);
27
5631
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
1875
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 with me... using System.Collections.Generic;
16
12785
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
1878
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: $Revision: 42333 $
1
3273
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 -- Deep Secrets, Perter van der Linden'. But I wonder why line 9 is ok but line 10 is illegal? Is what Keith Thompson said in another post also helpful to understand this question: "... The implicit conversion rule applies only to void*, not...
669
25860
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 paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
4
2102
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) : x0(a), y0(b),
8
3144
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 developer. x = someObject as MySpecificType; x = (MySpecificType) someObject; Thanks.
0
8432
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6185
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.