473,748 Members | 2,328 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VB and implicit conversions

I have an implicit conversion set up in an assembly from a Stream to
something else. In C#, it works. In VB it does not.

Does VB support implicit conversions? And if so any idea why it would work in
a C# program but not VB?
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
ELKNews - Get your free copy at http://www.atozedsoftware.com

Nov 20 '05 #1
36 3631
Chad,

no, VB doesn't support implicit castings in the current version. That is, it
doesn't support implicit castings from non-primitive types. Only Whidbey
will support that (and hopefully it will be produce code that is compatible
with the code generated by C# when using the implicit operator); in the
meanwhile, you have to use the DirectCast keyword to unbox an object.

Klaus

"Chad Z. Hower aka Kudzu" <cp**@hower.org > schrieb im Newsbeitrag
news:Xn******** **********@127. 0.0.1...
I have an implicit conversion set up in an assembly from a Stream to
something else. In C#, it works. In VB it does not.

Does VB support implicit conversions? And if so any idea why it would work in a C# program but not VB?
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
ELKNews - Get your free copy at http://www.atozedsoftware.com

Nov 20 '05 #2
"Klaus Löffelmann" <fo***********@ loeffelmann.de> wrote in
news:40******** **************@ news.freenet.de :
Whidbey will support that (and hopefully it will be produce code that is
compatible with the code generated by C# when using the implicit
operator); in the meanwhile, you have to use the DirectCast keyword to
unbox an object.


Ack. :(

I understand that there are differences between VB and C#. But how can MS
leave someting like this out? Esp from VB, the king language of anti type
safety. Where you could string = integer. :(

Can you give me an example of DirectCast?

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
ELKNews - Get your free copy at http://www.atozedsoftware.com

Nov 20 '05 #3
"Chad Z. Hower aka Kudzu" <cp**@hower.org > schrieb
I have an implicit conversion set up in an assembly from a Stream to
something else. In C#, it works. In VB it does not.

Does VB support implicit conversions? And if so any idea why it would
work in a C# program but not VB?


Code?
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #4
Cor
Hi Kudzu,

Inclusive the integer to string, although it is originaly Armin territory
because of that sample.
:-))

Dim a As Object
a = 1
Dim b As Integer
b = DirectCast(a, Integer)
MessageBox.Show (b.ToString)

Cor
Nov 20 '05 #5
Chad,

I wouldn't call VB the king language of anti type safety. At least, not
anymore. And, of course, only if you set

Option Strict True

at the beginning of each source file or in the project settings.With this
setting, you can only cast those types implicitly, which are implicitly
castable in C#, too.

To provide more background: VB internally uses a special flag to recognize a
type for casting implicitly. When you write a static operator implicit
function with C#, C# generates a procedure called op_implicit, if I remember
correctly. You can use this function from VB, too, but only by this name (so
it's not implicitly usable, obviously...)

However, you can't set the flag from C# (and, unfortunately, neither from
VB), so a type conversion could be done implicitly from VB.

But DirectCast works fine in VB:

Dim var as mySpecialObject =DirectCast(spe cialObjectBoxed InWhatEverType,
mySpecialObject )

Keep in mind, that DirectCast only does the same as the cast operator does
in C#: (In this example specialObjectBo xedInWhatEverTy pe boxed an object of
type mySpecialObject ). It just unboxes a type. If you really want to do a
type conversion from one type to another, you should provide a constructor
with a parameter for your class, you could use like this:

Dim var as new mySpecialObject (OtherSpecialOb jectToCastFrom) .

In the constructor for mySpecialObject provide the code for the actual
conversion.

Klaus.


"Chad Z. Hower aka Kudzu" <cp**@hower.org > schrieb im Newsbeitrag
news:Xn******** **********@127. 0.0.1...
"Klaus Löffelmann" <fo***********@ loeffelmann.de> wrote in
news:40******** **************@ news.freenet.de :
Whidbey will support that (and hopefully it will be produce code that is
compatible with the code generated by C# when using the implicit
operator); in the meanwhile, you have to use the DirectCast keyword to
unbox an object.


Ack. :(

I understand that there are differences between VB and C#. But how can MS
leave someting like this out? Esp from VB, the king language of anti type
safety. Where you could string = integer. :(

Can you give me an example of DirectCast?

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
ELKNews - Get your free copy at http://www.atozedsoftware.com

Nov 20 '05 #6
"Cor" <no*@non.com> wrote in news:uS******** ******@TK2MSFTN GP12.phx.gbl:
Dim a As Object
a = 1
Dim b As Integer
b = DirectCast(a, Integer)
MessageBox.Show (b.ToString)


Thanks. A direct conversion will be much easier than that. :(

But thanks for the answer.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
ELKNews - Get your free copy at http://www.atozedsoftware.com

Nov 20 '05 #7
"Armin Zingler" <az*******@free net.de> wrote in news:uiS5Hoy5DH A.2392
@TK2MSFTNGP11.p hx.gbl:
Does VB support implicit conversions? And if so any idea why it would
work in a C# program but not VB?


Code?


Its the very last VB example here:
http://www.atozed.com/indy/Texts/VSIntro.iwp

You can see the C# version directly above it. The parameter type to the Get
method is Borland.VCL.Cla sses.TStrings, which has an implict operator added
to it that allows conversion to it from System.IO.Strea m.

It looks like VB users might have to do direct conversions, which at least is
easyer than the DirectBox exmaple posted here.

That is something like:

new TCLRStream(MySt ream)

instead of just:

MyStream

whenever TStrings is needed.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
ELKNews - Get your free copy at http://www.atozedsoftware.com

Nov 20 '05 #8
"Klaus Löffelmann" <fo***********@ loeffelmann.de> wrote in
news:40******** **************@ news.freenet.de :
I wouldn't call VB the king language of anti type safety. At least, not
anymore. And, of course, only if you set
Yes, its been cleaned up in .net. I just find it ironic that traditionaly
VB has been anything but typesafe, in fact explicitly breaking it. And now
that they've cleaned it up, they gave C# implicit covnersion, but not VB.
Option Strict True
Yes I have this. But its my understanding that this applies to being able
to put integers into short ints, and the type off poor variations that VB
used to be rampant with.
at the beginning of each source file or in the project settings.With
this setting, you can only cast those types implicitly, which are
implicitly castable in C#, too.
But it applies only to ordinals, etc AFAIK.
To provide more background: VB internally uses a special flag to
recognize a type for casting implicitly. When you write a static
operator implicit function with C#, C# generates a procedure called
op_implicit, if I remember correctly. You can use this function from VB,
too, but only by this name (so it's not implicitly usable, obviously...)
Yes, exactly. :)
However, you can't set the flag from C# (and, unfortunately, neither
from VB), so a type conversion could be done implicitly from VB.
Exactly the problem it seems. :(
But DirectCast works fine in VB:

Dim var as mySpecialObject =DirectCast(spe cialObjectBoxed InWhatEverType,
mySpecialObject )
specialObjectBo xedInWhatEverTy pe would be in my case System.IO.Strea m
(Source) or Borland.VCL.Cla sses.TStrings (Destination)?
Keep in mind, that DirectCast only does the same as the cast operator
does in C#: (In this example specialObjectBo xedInWhatEverTy pe boxed an
object of type mySpecialObject ). It just unboxes a type. If you really
Is this the destination type or the source tyep though?
want to do a type conversion from one type to another, you should
provide a constructor with a parameter for your class, you could use
like this:


Already have that, and thats what the implicit operator does. However the
point is that this conversion is used often to pass in an argument, and the
idea was to free the user from needing to know there was even a difference.

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
ELKNews - Get your free copy at http://www.atozedsoftware.com

Nov 20 '05 #9
"Chad Z. Hower aka Kudzu" <cp**@hower.org > schrieb
"Armin Zingler" <az*******@free net.de> wrote in
news:uiS5Hoy5DH A.2392 @TK2MSFTNGP11.p hx.gbl:
Does VB support implicit conversions? And if so any idea why it
would work in a C# program but not VB?


Code?


Its the very last VB example here:
http://www.atozed.com/indy/Texts/VSIntro.iwp

You can see the C# version directly above it. The parameter type to
the Get method is Borland.VCL.Cla sses.TStrings, which has an implict
operator added to it that allows conversion to it from
System.IO.Strea m.

It looks like VB users might have to do direct conversions, which at
least is easyer than the DirectBox exmaple posted here.

That is something like:

new TCLRStream(MySt ream)

instead of just:

MyStream

whenever TStrings is needed.


What is an "implicit operator"? Currently I don't understand the problem.
I'm happy now that I have to cast explicitly, otherwise casting errors might
occur at runtime because I don't get a error message at compile time.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #10

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

Similar topics

3
2617
by: Reneé | last post by:
I wanted to know the order of implicit conversions and which sort of values allow them. From searching around in books and the archive of this mailing list, it seems to be that only numbers are implicitly converted within each other and bools can be implicitly converted to ints? However, I'm unable to find any other implicit conversions and the order of the implicit conversions (something like int->float->long). Any help would be greatly...
2
3095
by: Russell Reagan | last post by:
In a newer version of a chess program I am writing, I have created classes that are (more or less) drop in replacements for things that used to be plain old integer or enumerated variables (colors, piece types, squares, etc.). To accomplish this, I used implicit conversions. For instance, a color used to be: typedef int Color; // and a few constants... Now a color is (paraphrased):
9
8516
by: Simon | last post by:
Hi All, Is it possible to disallow implicit casting for an operand of a function written in C? i.e. void foo(int a) {..} short b; foo(b) // error without explicit cast
15
2290
by: buda | last post by:
Let me see if I got this :) 1. I know the rules for type conversions in arithmetic expressions 2. I know that an implicit type conversion is done at assignment, so float x = 1.23; int t = (int) x; is equivalent to int t = x; (could the latter produce a warning on some complier?) 3. I know that implicit conversions take place with function arguments, but am a bit shaky here. I suppose that passing a char to a function accepting
11
7621
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type Test. I expected the same error from the following line, but it compiles fine. The double is silently truncated to an int and then fed in to the implicit conversion operator. Why does this happen? Is there any way that I can keep the implicit...
9
2085
by: Girish | last post by:
Im trying to understand implicit type conversions from object -> string and vice versa. I have two classes, one Driver and one called StringWrapper. These are just test classes that try and emulate the pattern im trying to follow in an existing project. These are my steps: 1) I have a method "printMe" existing in the application which originally used to take in a string. This method is static and sits in the Driver
11
12788
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
17
2511
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
82
4617
by: robert bristow-johnson | last post by:
here is a post i put out (using Google Groups) that got dropped by google: i am using gcc as so: $ gcc -v Using built-in specs. Target: i386-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man -- infodir=/usr/share/info --enable-shared --enable-threads=posix -- enable-checking=release --with-system-zlib --enable-__cxa_atexit --
0
9528
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9359
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9236
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...
0
8235
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6792
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
4592
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
4863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3298
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
2774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.