This is best explained by looking at the comments in the sample code below.
Is this a VB.NET bug?
Option Strict On
Public Class Class1
End Class
Public Class Class2 : Inherits Class1
Public Sub bar()
Dim c3 As New Class3
Dim c2 As Class2
c3.foo(c2) '*** Compiler Error: Option Strict On disallows implicit
conversions from 'ClassLibrary1. Class1' to 'ClassLibrary1. Class2'
c3.foo(Me) 'However, this is OK even though Me is an instance of
Class 2
c3.foo(CType(c2 , Class2)) '?? This also OK, even though I am also
passing in an instance of Class 2
Dim c1 As Class1
c3.foo(c1) 'This is OK - no surprise
End Sub
End Class
Public Class Class3
Public Sub foo(ByRef arg1 As Class1)
End Sub
End Class 3 1484
You say it's ok "even though this is an instance". It isn't that having an
instance is somehow an exception to the rule, it is the rule. The only time
you are encountering a problem is when you are trying to pass a type (not an
instance). You need to pass instances.
"Earthlink" <ig*******@chee rful.com> wrote in message
news:4G******** *********@newsr ead2.news.pas.e arthlink.net... This is best explained by looking at the comments in the sample code below. Is this a VB.NET bug?
Option Strict On
Public Class Class1
End Class
Public Class Class2 : Inherits Class1
Public Sub bar() Dim c3 As New Class3
Dim c2 As Class2 c3.foo(c2) '*** Compiler Error: Option Strict On disallows implicit conversions from 'ClassLibrary1. Class1' to 'ClassLibrary1. Class2'
c3.foo(Me) 'However, this is OK even though Me is an instance of Class 2
c3.foo(CType(c2 , Class2)) '?? This also OK, even though I am also passing in an instance of Class 2
Dim c1 As Class1 c3.foo(c1) 'This is OK - no surprise End Sub
End Class
Public Class Class3
Public Sub foo(ByRef arg1 As Class1) End Sub
End Class
On 2004-08-29, Earthlink <ig*******@chee rful.com> wrote: This is best explained by looking at the comments in the sample code below. Is this a VB.NET bug?
It's confusing, but I don't see it as a bug, just a flaw :-). I'm not
sure if I can come up with language that would make it clearer, but to
my mind the thing to consider is the ByRef parameters are very concerned
with the variable declaration in the caller.
Option Strict On
Public Class Class1
End Class
Public Class Class2 : Inherits Class1
Public Sub bar() Dim c3 As New Class3
Dim c2 As Class2 c3.foo(c2) '*** Compiler Error: Option Strict On disallows implicit conversions from 'ClassLibrary1. Class1' to 'ClassLibrary1. Class2'
Presumably you see why, right? If this were allowed, then "foo" could
set c2 to an instance of Class1, and that can never be allowed. Make
sure you understand this clearly or the rest of the examples won't make
sense. c3.foo(Me) 'However, this is OK even though Me is an instance of Class 2
Forget "instance" in this case, consider declared variable. In this
case, you don't have a declared variable. "Me" is a keyword whose instance
will never change. In other words, ByRef has no effect here, "foo" can
only assign to a temporary Class1 variable, and that's fine.
The problem we saw in the first example can't happen here, so VB.Net
allows it.
c3.foo(CType(c2 , Class2)) '?? This also OK, even though I am also passing in an instance of Class 2
And a similar thing here. CType(c2, Class2) isn't an lvalue; it doesn't
refer to a variable name in the caller, so nothing untoward can happen
with an assignment in "foo".
Put another way, VB.Net creates a temporary variable to hold the results
of CType(c2,class2 ), and then sends that temporary variable to "foo".
foo can change what the temporary points to, but can't change what c2
points to, so VB.Net allows it. Dim c1 As Class1 c3.foo(c1) 'This is OK - no surprise End Sub
End Class
Public Class Class3
Public Sub foo(ByRef arg1 As Class1) End Sub
End Class
I'm honestly not sure if this explanation will help at all, so ask again
if there's something confusing.
BTW, the real confusion here is that VB.Net allows non-lvalues to sent
as ByRef parameters. This helps nothing of course and is a prime source
for some very tricky bugs, but I suppose the thinking is that it makes
things a tad easier for beginners.
The "If this were allowed ..." sentence makes it crystal clear even if I
were not to read anything else. Thanks.
"David" <df*****@woofix .local.dom> wrote in message
news:slrncj4s5q .mcc.df*****@wo ofix.local.dom. .. On 2004-08-29, Earthlink <ig*******@chee rful.com> wrote: This is best explained by looking at the comments in the sample code
below. Is this a VB.NET bug? It's confusing, but I don't see it as a bug, just a flaw :-). I'm not sure if I can come up with language that would make it clearer, but to my mind the thing to consider is the ByRef parameters are very concerned with the variable declaration in the caller.
Option Strict On
Public Class Class1
End Class
Public Class Class2 : Inherits Class1
Public Sub bar() Dim c3 As New Class3
Dim c2 As Class2 c3.foo(c2) '*** Compiler Error: Option Strict On disallows
implicit conversions from 'ClassLibrary1. Class1' to 'ClassLibrary1. Class2'
Presumably you see why, right? If this were allowed, then "foo" could set c2 to an instance of Class1, and that can never be allowed. Make sure you understand this clearly or the rest of the examples won't make sense.
c3.foo(Me) 'However, this is OK even though Me is an instance
of Class 2 Forget "instance" in this case, consider declared variable. In this case, you don't have a declared variable. "Me" is a keyword whose
instance will never change. In other words, ByRef has no effect here, "foo" can only assign to a temporary Class1 variable, and that's fine.
The problem we saw in the first example can't happen here, so VB.Net allows it.
c3.foo(CType(c2 , Class2)) '?? This also OK, even though I am
also passing in an instance of Class 2
And a similar thing here. CType(c2, Class2) isn't an lvalue; it doesn't refer to a variable name in the caller, so nothing untoward can happen with an assignment in "foo".
Put another way, VB.Net creates a temporary variable to hold the results of CType(c2,class2 ), and then sends that temporary variable to "foo". foo can change what the temporary points to, but can't change what c2 points to, so VB.Net allows it.
Dim c1 As Class1 c3.foo(c1) 'This is OK - no surprise End Sub
End Class
Public Class Class3
Public Sub foo(ByRef arg1 As Class1) End Sub
End Class
I'm honestly not sure if this explanation will help at all, so ask again if there's something confusing.
BTW, the real confusion here is that VB.Net allows non-lvalues to sent as ByRef parameters. This helps nothing of course and is a prime source for some very tricky bugs, but I suppose the thinking is that it makes things a tad easier for beginners. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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
|
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...
|
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...
|
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...
| |
by: Aaron Queenan |
last post by:
Given the classes:
class Class
{
public static implicit operator int(Class c)
{
return 0;
}
}
class Holder
|
by: Chad Z. Hower aka Kudzu |
last post by:
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/
"Programming is an art form that fights back"
|
by: arindam.mukerjee |
last post by:
I was running code like:
#include <stdio.h>
int main()
{
printf("%f\n", 9/5);
return 0;
}
|
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...
|
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |