PC Review


Reply
Thread Tools Rate Thread

ByRef and Implicit Conversions

 
 
Earthlink
Guest
Posts: n/a
 
      30th Aug 2004
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


 
Reply With Quote
 
 
 
 
Scott M.
Guest
Posts: n/a
 
      30th Aug 2004
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" <(E-Mail Removed)> wrote in message
news:4GtYc.2142$(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
David
Guest
Posts: n/a
 
      30th Aug 2004
On 2004-08-29, Earthlink <(E-Mail Removed)> 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.

 
Reply With Quote
 
Ed Johnson
Guest
Posts: n/a
 
      30th Aug 2004
The "If this were allowed ..." sentence makes it crystal clear even if I
were not to read anything else. Thanks.


"David" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 2004-08-29, Earthlink <(E-Mail Removed)> 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.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Implicit Conversions in DataRow =?Utf-8?B?SGFycnkgVg==?= Microsoft C# .NET 0 23rd Jul 2007 05:40 PM
Reflection implicit and explicit conversions. =?Utf-8?B?QnJpYW4gU3RhbmZvcnRo?= Microsoft Dot NET Framework 1 29th Jun 2005 02:30 AM
Primitive types and implicit conversions Dennis Myrén Microsoft C# .NET 6 20th Oct 2004 05:08 PM
implicit conversions on FontStyles Brian Henry Microsoft VB .NET 3 14th May 2004 02:26 PM
VB and implicit conversions Chad Z. Hower aka Kudzu Microsoft VB .NET 36 30th Jan 2004 10:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:45 PM.