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 1479
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*******@cheerful.com> wrote in message
news:4G*****************@newsread2.news.pas.earthl ink.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*******@cheerful.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*****@woofix.local.dom... On 2004-08-29, Earthlink <ig*******@cheerful.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...
|
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)...
|
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...
|
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...
|
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...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
| |