Strange behavior - Overloaded Property and Enum parameter - Bug?

A

Arnold Schrijver

I wrote a program that draws items to the screen and maintains a set of
Offset values.
There was a bug in the code, because objects were positioned wrongly. While
debugging
I found some peculiar behavior with the overloaded Property Offsets:

In class CanvasBlock:
------------------------------

Public Enum EOffsetTypes
HorizontalOffset = 0
VerticalOffset = 1
End Enum

'Update ArrayList with Horz/Vertical offsets, that are always
present.
Protected WriteOnly Property BlockOffset(ByVal offsetType As
EOffsetTypes) As Double
Set(ByVal offset As Double)
'Add new value to ArrayList1 at specified index (=
CInt(offsetType)).
End Set
End Property

'Store any additional Offsets in a separate ArrayList. Create
additional entries as needed.
Protected WriteOnly Property BlockOffset(Optional ByVal offsetIndex
As Integer = Nothing) As Double
Set(ByVal offset As Double)

If offsetIndex.Equals(Nothing) = True Then
'Add new value to ArrayList2
Else
'Add new value to ArrayList2 at specified offsetIndex .
End If
End Set
End Property

In the calling code:
------------------------------

'Correct. ArrayList1(0) is updated.
objBlock.Offsets(EOffsetTypes.HorizontalOffset) = 7.0

'Correct. ArrayList1(1) is updated.
objBlock.Offsets(EOffsetTypes.VerticalOffset) = 7.0

'Correct ArrayList2(1) is updated.
objBlock.Offsets(1) = 7.0

'Incorrect!! ArrayList1(0) is updated. The first overloaded method
with the Enum signature is called.
'Since the previous call succeeded, I would expect this call to
succeed as well. Strange? A bug?
objBlock.Offsets(0) = 7.0

Am I doing something wrong here? Can .NET not differentiate between
signatures with Integer and
Enums (which are basically also integer values), or is this a bug in .NET?

Cheers,

Arnold Schrijver.
 
H

Herfried K. Wagner [MVP]

Arnold Schrijver said:
I wrote a program that draws items to the screen and maintains a set of
Offset values.
There was a bug in the code, because objects were positioned wrongly.
While
debugging
I found some peculiar behavior with the overloaded Property Offsets:

In class CanvasBlock:
------------------------------

Public Enum EOffsetTypes
HorizontalOffset = 0
VerticalOffset = 1
End Enum

'Update ArrayList with Horz/Vertical offsets, that are always
present.
Protected WriteOnly Property BlockOffset(ByVal offsetType As
EOffsetTypes) As Double
Set(ByVal offset As Double)
'Add new value to ArrayList1 at specified index (=
CInt(offsetType)).
End Set
End Property

'Store any additional Offsets in a separate ArrayList. Create
additional entries as needed.
Protected WriteOnly Property BlockOffset(Optional ByVal offsetIndex
As Integer = Nothing) As Double
Set(ByVal offset As Double)

If offsetIndex.Equals(Nothing) = True Then
'Add new value to ArrayList2
Else
'Add new value to ArrayList2 at specified offsetIndex .
End If
End Set
End Property

In the calling code:
------------------------------

'Correct. ArrayList1(0) is updated.
objBlock.Offsets(EOffsetTypes.HorizontalOffset) = 7.0

'Correct. ArrayList1(1) is updated.
objBlock.Offsets(EOffsetTypes.VerticalOffset) = 7.0

'Correct ArrayList2(1) is updated.
objBlock.Offsets(1) = 7.0

'Incorrect!! ArrayList1(0) is updated. The first overloaded method
with the Enum signature is called.
'Since the previous call succeeded, I would expect this call to
succeed as well. Strange? A bug?
objBlock.Offsets(0) = 7.0

Am I doing something wrong here? Can .NET not differentiate between
signatures with Integer and
Enums (which are basically also integer values), or is this a bug in .NET?


I am not able to repro this behavior with .NET 1.0 using the code below:

\\\
Public Enum Foo
Goo = 0
Baz = 1
End Enum

Public WriteOnly Property Test(Optional ByVal i As Integer = 0) As Object
Set(ByVal Value As Object)
Debug.WriteLine("'Test(Integer)'")
End Set
End Property

Public WriteOnly Property Test(ByVal i As Foo) As Object
Set(ByVal Value As Object)
Debug.WriteLine("'Test(Foo)'")
End Set
End Property
..
..
..
Test(1) = 1
Test(Foo.Baz) = 1
///

Output:
 
A

Arnold Schrijver

I found that the information is indeed a bug, but then in the VB language. An excellent
explanation can be found on Rob Gruen's weblog:
http://weblogs.asp.net/robgruen/archive/2004/03/26/97104.aspx

There is also an extensive discussion on this issue on dotnet247.com:
http://www.dotnet247.com/247reference/msgs/56/284220.aspx


Arnold Schrijver said:
Hi,

Given your code, I could reproduce the strange behavior again! I added the VB project
in the attached zip-file.

With your code in Class1 these calls:

Dim objTest As New Class1

objTest.Test(0) = "0 Integer"
objTest.Test(1) = "1 Integer"
objTest.Test(Class1.Foo.Goo) = "0 Enum"
objTest.Test(Class1.Foo.Baz) = "1 Enum"

Result in:

'Test(Foo)'
'Test(Integer)'
'Test(Foo)'
'Test(Foo)'

I am using Visual Studio 2003 (v. 7.1.3088) with .NET Framework 1.1 (v. 1.1.4322 SP1)
on Windows 2000 SP4 (v. 5.00.2195)

As a temporary workaround to fix the code I appended a dummy Boolean to the overloaded
enum Method, to create a method signature that differs more significantly. This works, but is
not pretty.

Thanks for your response!

Cheers,

Arnold.
<SNIP/>
 
H

Herfried K. Wagner [MVP]

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top