My Bug or VB.Net Bug ??? 
Author Message
 My Bug or VB.Net Bug ???

Hopefully its my own lack of knowledge, but here's the issue:

I have a class with a ReadOnly property:
--------------------------------
Class Emp
    Private m_Age As Integer
    ReadOnly Property Age() As Integer
        Get
            Return m_Age
        End Get
    End Property
End Class
------------------------------
In my module:

Sub Main
    Dim e As New Emp()
    Console.WriteLine(e.Age)
    ResetValue(e.Age)
    Console.WriteLine(e.Age)

    Console.ReadLine()
End Sub

Sub ResetValue(ByRef Value As Integer)
      Value = 22
End Sub
--------------------------------------------------------
Why it does not complain when ResetValue is being called?
1.  If the property is Read/Write, call to ResetValue updates the
property value in the Sub Main.  OK so far.
2.  With Property being ReadOnly, any attempt to update/change it in
Sub Main results in compile error.  Still OK
3.  My question is, why it does not complain when same property is
being changed through ResetValue?  Isn't this what is called late
binding?  What's WRONG here?

Thanks
Ali  



Fri, 15 Apr 2005 07:36:01 GMT  
 My Bug or VB.Net Bug ???
Ali,
Your Age property in .NET is actually represented by get_Age & set_Age
methods in IL, seeing as its Readonly, only the get_Age is implemented IL.
You can see what is written to the IL by using the ILDASM.EXE utility.

Normally found here for VS.NET
C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin\ildasm.exe.

When you call the ResetValue method, you are not actually sending the Age
property, you are sending a temporary variable. VB.NET will call the get_Age
method to get the age and put it into the temporary variable, then it calls
your ResetValue, for a read/write property VB.NET then calls the set_Age
method, however it appears that VB.NET does not code anything when the
property is Readonly.

I will post something to MS to see what they say.

Hope this helps
Jay


Quote:
> Hopefully its my own lack of knowledge, but here's the issue:

> I have a class with a ReadOnly property:
> --------------------------------
> Class Emp
>     Private m_Age As Integer
>     ReadOnly Property Age() As Integer
>         Get
>             Return m_Age
>         End Get
>     End Property
> End Class
> ------------------------------
> In my module:

> Sub Main
>     Dim e As New Emp()
>     Console.WriteLine(e.Age)
>     ResetValue(e.Age)
>     Console.WriteLine(e.Age)

>     Console.ReadLine()
> End Sub

> Sub ResetValue(ByRef Value As Integer)
>       Value = 22
> End Sub
> --------------------------------------------------------
> Why it does not complain when ResetValue is being called?
> 1.  If the property is Read/Write, call to ResetValue updates the
> property value in the Sub Main.  OK so far.
> 2.  With Property being ReadOnly, any attempt to update/change it in
> Sub Main results in compile error.  Still OK
> 3.  My question is, why it does not complain when same property is
> being changed through ResetValue?  Isn't this what is called late
> binding?  What's WRONG here?

> Thanks
> Ali



Fri, 15 Apr 2005 11:00:39 GMT  
 My Bug or VB.Net Bug ???
I believe it's because your called function expects a reference to an
Integer, but what you are asking it to do is to take a reference to a
property procedure.  It therefore casts the contents of the property to an
integer and passes that instead.
A property does not behave in the same way as a base data type or object, as
you have just found :)
It is a rather unusual thing that you are trying to do here, and normal
approach to this kind of processing would either be to implement an
"Emp.Reset" method, or to pass the Object itself to a routine, e.g.
ResetValue(e) instead of ResetValue(e.Age)

Cheers,
  Jason


Quote:
> Hopefully its my own lack of knowledge, but here's the issue:

> I have a class with a ReadOnly property:
> --------------------------------
> Class Emp
>     Private m_Age As Integer
>     ReadOnly Property Age() As Integer
>         Get
>             Return m_Age
>         End Get
>     End Property
> End Class
> ------------------------------
> In my module:

> Sub Main
>     Dim e As New Emp()
>     Console.WriteLine(e.Age)
>     ResetValue(e.Age)
>     Console.WriteLine(e.Age)

>     Console.ReadLine()
> End Sub

> Sub ResetValue(ByRef Value As Integer)
>       Value = 22
> End Sub
> --------------------------------------------------------
> Why it does not complain when ResetValue is being called?
> 1.  If the property is Read/Write, call to ResetValue updates the
> property value in the Sub Main.  OK so far.
> 2.  With Property being ReadOnly, any attempt to update/change it in
> Sub Main results in compile error.  Still OK
> 3.  My question is, why it does not complain when same property is
> being changed through ResetValue?  Isn't this what is called late
> binding?  What's WRONG here?

> Thanks
> Ali



Fri, 15 Apr 2005 11:30:59 GMT  
 My Bug or VB.Net Bug ???
I would argue that is not the case, since changing the property to
Read/Write, and then calling the function ResetValue does change the
value of the property; specially since its a ByRef call, rather then
ByVal, so there is no way an integer will be passed anyway.

Yes, I agree its not a "usual" problem, but again, the issue is that
its not expected behavior.  And the bigger issue is, that its silent.
it does not raise any kind of error.  It does not/cannot  change the
value, and it does not complain while doing that.

- Ali

On Mon, 28 Oct 2002 14:30:59 +1100, "Jason Sobell \(iGadget\)"

Quote:

>I believe it's because your called function expects a reference to an
>Integer, but what you are asking it to do is to take a reference to a
>property procedure.  It therefore casts the contents of the property to an
>integer and passes that instead.
>A property does not behave in the same way as a base data type or object, as
>you have just found :)
>It is a rather unusual thing that you are trying to do here, and normal
>approach to this kind of processing would either be to implement an
>"Emp.Reset" method, or to pass the Object itself to a routine, e.g.
>ResetValue(e) instead of ResetValue(e.Age)

>Cheers,
>  Jason



>> Hopefully its my own lack of knowledge, but here's the issue:

>> I have a class with a ReadOnly property:
>> --------------------------------
>> Class Emp
>>     Private m_Age As Integer
>>     ReadOnly Property Age() As Integer
>>         Get
>>             Return m_Age
>>         End Get
>>     End Property
>> End Class
>> ------------------------------
>> In my module:

>> Sub Main
>>     Dim e As New Emp()
>>     Console.WriteLine(e.Age)
>>     ResetValue(e.Age)
>>     Console.WriteLine(e.Age)

>>     Console.ReadLine()
>> End Sub

>> Sub ResetValue(ByRef Value As Integer)
>>       Value = 22
>> End Sub
>> --------------------------------------------------------
>> Why it does not complain when ResetValue is being called?
>> 1.  If the property is Read/Write, call to ResetValue updates the
>> property value in the Sub Main.  OK so far.
>> 2.  With Property being ReadOnly, any attempt to update/change it in
>> Sub Main results in compile error.  Still OK
>> 3.  My question is, why it does not complain when same property is
>> being changed through ResetValue?  Isn't this what is called late
>> binding?  What's WRONG here?

>> Thanks
>> Ali



Fri, 15 Apr 2005 12:53:03 GMT  
 My Bug or VB.Net Bug ???
With or Without ReadOnly Access, output is exactly same in IL.
Jay, where are you seeing the difference between the output?
I am missing something.

With ReadOnly Property:
-----------------------
  IL_0014:  callvirt   instance int32 ClassFundamentals.Emp::get_Age()
  IL_0019:  stloc.1
  IL_001a:  ldloca.s   _Vb_t_i4_0
  IL_001c:  call       void
ClassFundamentals.ClassVB::ResetValue(int32&)
  IL_0021:  nop
  IL_0022:  ldloc.0
  IL_0023:  callvirt   instance int32 ClassFundamentals.Emp::get_Age()

With Read/Write Access:
-----------------------
  IL_0014:  callvirt   instance int32 ClassFundamentals.Emp::get_Age()
  IL_0019:  stloc.1
  IL_001a:  ldloca.s   _Vb_t_i4_0
  IL_001c:  call       void
ClassFundamentals.ClassVB::ResetValue(int32&)
  IL_0021:  nop
  IL_0022:  ldloc.0
  IL_0023:  callvirt   instance int32 ClassFundamentals.Emp::get_Age()

Thanks
Ali



Quote:
>I would argue that is not the case, since changing the property to
>Read/Write, and then calling the function ResetValue does change the
>value of the property; specially since its a ByRef call, rather then
>ByVal, so there is no way an integer will be passed anyway.

>Yes, I agree its not a "usual" problem, but again, the issue is that
>its not expected behavior.  And the bigger issue is, that its silent.
>it does not raise any kind of error.  It does not/cannot  change the
>value, and it does not complain while doing that.

>- Ali

>On Mon, 28 Oct 2002 14:30:59 +1100, "Jason Sobell \(iGadget\)"

>>I believe it's because your called function expects a reference to an
>>Integer, but what you are asking it to do is to take a reference to a
>>property procedure.  It therefore casts the contents of the property to an
>>integer and passes that instead.
>>A property does not behave in the same way as a base data type or object, as
>>you have just found :)
>>It is a rather unusual thing that you are trying to do here, and normal
>>approach to this kind of processing would either be to implement an
>>"Emp.Reset" method, or to pass the Object itself to a routine, e.g.
>>ResetValue(e) instead of ResetValue(e.Age)

>>Cheers,
>>  Jason



>>> Hopefully its my own lack of knowledge, but here's the issue:

>>> I have a class with a ReadOnly property:
>>> --------------------------------
>>> Class Emp
>>>     Private m_Age As Integer
>>>     ReadOnly Property Age() As Integer
>>>         Get
>>>             Return m_Age
>>>         End Get
>>>     End Property
>>> End Class
>>> ------------------------------
>>> In my module:

>>> Sub Main
>>>     Dim e As New Emp()
>>>     Console.WriteLine(e.Age)
>>>     ResetValue(e.Age)
>>>     Console.WriteLine(e.Age)

>>>     Console.ReadLine()
>>> End Sub

>>> Sub ResetValue(ByRef Value As Integer)
>>>       Value = 22
>>> End Sub
>>> --------------------------------------------------------
>>> Why it does not complain when ResetValue is being called?
>>> 1.  If the property is Read/Write, call to ResetValue updates the
>>> property value in the Sub Main.  OK so far.
>>> 2.  With Property being ReadOnly, any attempt to update/change it in
>>> Sub Main results in compile error.  Still OK
>>> 3.  My question is, why it does not complain when same property is
>>> being changed through ResetValue?  Isn't this what is called late
>>> binding?  What's WRONG here?

>>> Thanks
>>> Ali



Fri, 15 Apr 2005 13:28:26 GMT  
 My Bug or VB.Net Bug ???
Actually, it appears something is a little odd :)
The method you describe is the way VB6 worked, but in VB.NET you can pass a
property in this way and the called function will indeed modify the object
correctly, but only when processing has returned to the calling code...
unless you set it readonly in which case it seems to throw away the return
value instead of displaying an error. I suspect this is a bugette (a small
round bread-like bug?)

so
     ResetValue(e.Age)
is actually equal to
     Dim temp As Integer
     temp = e.age
     ResetValue(temp)
     e.age = temp

_unless_ it's a read-only property, in which case it uses the example you
show.

Cheers,
  Jason


Quote:

> > Hopefully its my own lack of knowledge, but here's the issue:

> > I have a class with a ReadOnly property:
> > --------------------------------
> > Class Emp
> >     Private m_Age As Integer
> >     ReadOnly Property Age() As Integer
> >         Get
> >             Return m_Age
> >         End Get
> >     End Property
> > End Class
> > ------------------------------
> > In my module:

> > Sub Main
> >     Dim e As New Emp()
> >     Console.WriteLine(e.Age)
> >     ResetValue(e.Age)
> >     Console.WriteLine(e.Age)

> >     Console.ReadLine()
> > End Sub

> > Sub ResetValue(ByRef Value As Integer)
> >       Value = 22
> > End Sub
> > --------------------------------------------------------
> > Why it does not complain when ResetValue is being called?
> > 1.  If the property is Read/Write, call to ResetValue updates the
> > property value in the Sub Main.  OK so far.
> > 2.  With Property being ReadOnly, any attempt to update/change
> > it in Sub Main results in compile error.  Still OK
> > 3.  My question is, why it does not complain when same property is
> > being changed through ResetValue?  Isn't this what is called late
> > binding?  What's WRONG here?

> Nothing is wrong. :)

> It doesn't matter if the property is readonly. The property value is
> retrieved and afterwards the value it is passed ByRef to ResetValue.

>     ResetValue(e.Age)

> is equal to

>     Dim temp As Integer
>     temp = e.age
>     ResetValue(temp)

> Armin



Fri, 15 Apr 2005 16:30:09 GMT  
 My Bug or VB.Net Bug ???
No, I suspect you've accidentaly disassembled the same file twice :)

The Read/Write one looks like this:

  IL_0000:  nop
  IL_0001:  newobj     instance void WindowsApplication6.Emp::.ctor()
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  callvirt   instance int32 WindowsApplication6.Emp::get_Age()
  IL_000d:  call       void [mscorlib]System.Console::WriteLine(int32)
  IL_0012:  nop
  IL_0013:  ldarg.0
  IL_0014:  ldloc.0
  IL_0015:  stloc.2
  IL_0016:  ldloc.2
  IL_0017:  callvirt   instance int32 WindowsApplication6.Emp::get_Age()
  IL_001c:  stloc.1
  IL_001d:  ldloca.s   _Vb_t_i4_0
  IL_001f:  callvirt   instance void
WindowsApplication6.Form1::ResetValue(int32&)
  IL_0024:  nop
  IL_0025:  ldloc.2
  IL_0026:  ldloc.1
  IL_0027:  callvirt   instance void WindowsApplication6.Emp::set_Age(int32)
  IL_002c:  nop
  IL_002d:  ldloc.0
  IL_002e:  callvirt   instance int32 WindowsApplication6.Emp::get_Age()
  IL_0033:  call       void [mscorlib]System.Console::WriteLine(int32)
  IL_0038:  nop
  IL_0039:  nop
  IL_003a:  ret

IL_0027 shows where the property is writen back again when the ResetValue
function has returned.
It's worth noting that changes you make to an objects properties are not
made until the function returns, because this could easily catch you out :)
Having said that, I'm not a fan of passing a property by reference. I can't
see too many uses for that, and it tends to hide what's going on in your
implementation.

Cheers,
  Jason



Fri, 15 Apr 2005 16:42:23 GMT  
 My Bug or VB.Net Bug ???
Ah, yes, you're right.
Good and important point!
As that's the case, I'd expect an error, too, or at least a warning.
...or we have to learn to deal with this..feature. :)

Armin


Quote:
> Actually, it appears something is a little odd :)
> The method you describe is the way VB6 worked, but in VB.NET you
> can pass a property in this way and the called function will
> indeed modify the object correctly, but only when processing has
> returned to the calling code... unless you set it readonly in
> which case it seems to throw away the return value instead of
> displaying an error. I suspect this is a bugette (a small round
> bread-like bug?)

> so
>      ResetValue(e.Age)
> is actually equal to
>      Dim temp As Integer
>      temp = e.age
>      ResetValue(temp)
>      e.age = temp

> _unless_ it's a read-only property, in which case it uses the
> example you show.

> Cheers,
>   Jason




> > > Hopefully its my own lack of knowledge, but here's the issue:

> > > I have a class with a ReadOnly property:
> > > --------------------------------
> > > Class Emp
> > >     Private m_Age As Integer
> > >     ReadOnly Property Age() As Integer
> > >         Get
> > >             Return m_Age
> > >         End Get
> > >     End Property
> > > End Class
> > > ------------------------------
> > > In my module:

> > > Sub Main
> > >     Dim e As New Emp()
> > >     Console.WriteLine(e.Age)
> > >     ResetValue(e.Age)
> > >     Console.WriteLine(e.Age)

> > >     Console.ReadLine()
> > > End Sub

> > > Sub ResetValue(ByRef Value As Integer)
> > >       Value = 22
> > > End Sub
> > > --------------------------------------------------------
> > > Why it does not complain when ResetValue is being called?
> > > 1.  If the property is Read/Write, call to ResetValue
> > > updates the property value in the Sub Main.  OK so far.
> > > 2.  With Property being ReadOnly, any attempt to update/change
> > > it in Sub Main results in compile error.  Still OK
> > > 3.  My question is, why it does not complain when same
> > > property is being changed through ResetValue?  Isn't this
> > > what is called late binding?  What's WRONG here?

> > Nothing is wrong. :)

> > It doesn't matter if the property is readonly. The property
> > value is retrieved and afterwards the value it is passed ByRef
> > to ResetValue.

> >     ResetValue(e.Age)

> > is equal to

> >     Dim temp As Integer
> >     temp = e.age
> >     ResetValue(temp)

> > Armin



Fri, 15 Apr 2005 18:31:20 GMT  
 My Bug or VB.Net Bug ???

Quote:
> Jason,

> >Perhaps this explains why they have the 'ReadOnly' prefix (which I always
> >thought was pretty redundant) ?

> The ReadOnly and WriteOnly modifiers aren't redundant for MustOverride
> properties, or properties on an interface, where you don't have a body
> with Get/Set accessors.

Excellent point!
I was just suprised to see a keyword used in this way after using the C#
method: int Age{get; }
but I suppose since they give us silly references like 'mybase' and 'me'
then 'ReadOnly' sounds quite tame in comparison :)

Cheers,
  Jason



Fri, 15 Apr 2005 20:46:53 GMT  
 My Bug or VB.Net Bug ???
So it is, but considering how VB.NET holds our hand in preventing every
possible flaw in our solutions, I would have expected at least a runtime
error to indicate that you were attempting a write to a read-only attribute
:)

Cheers,
  Jason


Quote:
> Ali,

> This is the documented behaviour, see the language spec section 9.3.5
> (the first of the three bullets).

> http://msdn.microsoft.com/library/en-us/vbls7/html/vblrfVBSpec9_3_5.asp

> Mattias

> ===

> http://www.msjogren.net/dotnet/
> Please reply only to the newsgroup.



Fri, 15 Apr 2005 20:50:07 GMT  
 My Bug or VB.Net Bug ???
Ali,

This is the documented behaviour, see the language spec section 9.3.5
(the first of the three bullets).

http://msdn.microsoft.com/library/en-us/vbls7/html/vblrfVBSpec9_3_5.asp

Mattias

===

http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.



Fri, 15 Apr 2005 20:18:25 GMT  
 
 [ 16 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Project98 OLE & VB: bugs, bugs, bugs

2. IDE BUGs, BUGs, BUGs....

3. BUGS BUGS BUGS!

4. Bug in VisualStudio.NET editor for VB.NET

5. ADO.Net to fetch ActiveDirectory this function bugs in Asp.net but works in Vb.n

6. BUG BUG in VB

7. BUG BUG in VB

8. Bug in Visio Save as Web in VB.net

9. Mysterious bug in VB.NET 1.1 compiler

10. ActiveX OCXs and VB.NET - possible bug

11. is this the bug of vb.net?

12. is this the bug of vb.net

 

 
Powered by phpBB® Forum Software