Optional argument defaulted to Single.NaN cannot be overridden?

J

Joe Duchtel

Hello -

I am defaulting an optional parameter to Single.NaN so I can determine
whether it was passed in or not. The problem is when I do this in an
overriden Sub/Function it will not compile. Here is the code ...

MustInherit Class cParent
Public MustOverride Sub XYZ(Optional ByVal aA As Single =
Single.NaN)
End Class

Class cChild
Inherits cParent
Public Overrides Sub XYZ(Optional ByVal aA As Single = Single.NaN)
' ...
End Sub
End Class

This causes a 'Public Overrides Sub XYZ([aA As Single = -1.#IND])'
cannot override 'Public MustOverride Sub XYZ([aA As Single = -1.#IND)'
because they differ by the default values of optional parameters.

Is this a glitch in Visual Studio or am I doing something wrong? I
tried it in both Visual Studio 2003 (.NET 1.1) and Visual Studio 2008
(.NET 2.0, 3.0, 3.5) ...

Thanks,
Joe
 
F

Family Tree Mike

Joe Duchtel said:
Hello -

I am defaulting an optional parameter to Single.NaN so I can determine
whether it was passed in or not. The problem is when I do this in an
overriden Sub/Function it will not compile. Here is the code ...

MustInherit Class cParent
Public MustOverride Sub XYZ(Optional ByVal aA As Single =
Single.NaN)
End Class

Class cChild
Inherits cParent
Public Overrides Sub XYZ(Optional ByVal aA As Single = Single.NaN)
' ...
End Sub
End Class

This causes a 'Public Overrides Sub XYZ([aA As Single = -1.#IND])'
cannot override 'Public MustOverride Sub XYZ([aA As Single = -1.#IND)'
because they differ by the default values of optional parameters.

Is this a glitch in Visual Studio or am I doing something wrong? I
tried it in both Visual Studio 2003 (.NET 1.1) and Visual Studio 2008
(.NET 2.0, 3.0, 3.5) ...

Thanks,
Joe


I suspect that the reason is that checking dim b as Boolean = (Single.NaN =
Single.NaN) returns false.

MSDN Says:

Use IsNaN to determine whether a value is not a number. It is not possible
to determine whether a value is not a number by comparing it to another
value equal to NaN.

I don't know a work arround for your situation.
 
M

Michel Posseth [MCP]

In my opinion you could solve this in 2 ways

1 encapsulate the method in a object and set the field ( or not ) with a
property
2 use one of the other contstants that do work but are out of range for your
purposes ( like Single.NegativeInfinity or Single.PositiveInfinity )


HTH

Michel Posseth
 
A

AMercer

Below is a different approach using ParamArray. It gains by letting you
detect when zero parameters were passed. It loses by allowing more than one
parameter to be passed.

MustInherit Class cParent
Public MustOverride Sub XYZ(ByVal ParamArray aA() As Single)
End Class

Class cChild
Inherits cParent
Public Overrides Sub XYZ(ByVal ParamArray aA() As Single)
' aA.Length is the number of parameters passed
' if aA.Length=0, no parameters were passed
' if aA.Length=1, one parameter is in aA(0)
' if aA.Length=2, two parameters are in aA(0) and aA(1)
' etc
End Sub
End Class
 
J

Joe Duchtel

In my opinion you could solve this in 2 ways

1 encapsulate the method in a object and set the field ( or not ) with a
property
2 use one of the other contstants that do work but are out of range for your
purposes ( like Single.NegativeInfinity or Single.PositiveInfinity )

HTH

Michel Posseth

"Joe Duchtel" <[email protected]> schreef in bericht

I am defaulting an optional parameter to Single.NaN so I can determine
whether it was passed in or not.  The problem is when I do this in an
overriden Sub/Function it will not compile.  Here is the code ...
MustInherit Class cParent
   Public MustOverride Sub XYZ(Optional ByVal aA As Single =
Single.NaN)
End Class
Class cChild
   Inherits cParent
   Public Overrides Sub XYZ(Optional ByVal aA As Single = Single.NaN)
       ' ...
   End Sub
End Class
This causes a 'Public Overrides Sub XYZ([aA As Single = -1.#IND])'
cannot override 'Public MustOverride Sub XYZ([aA As Single = -1.#IND)'
because they differ by the default values of optional parameters.
Is this a glitch in Visual Studio or am I doing something wrong?  I
tried it in both Visual Studio 2003 (.NET 1.1) and Visual Studio 2008
(.NET 2.0, 3.0, 3.5) ...
Thanks,
Joe- Hide quoted text -

- Show quoted text -

Hello Michel -

I do not have the NegativeInfinity nor PositiveInfinity in Visual
Studio 2003 (.NET 1.1) and that is currently the main development
platform. I will think about your first suggestion ... there might be
a way I could do it like that.

Thanks,
Joe
 
J

Joe Duchtel

Below is a different approach using ParamArray.  It gains by letting you
detect when zero parameters were passed.  It loses by allowing more than one
parameter to be passed.

MustInherit Class cParent
  Public MustOverride Sub XYZ(ByVal ParamArray aA() As Single)
End Class

Class cChild
  Inherits cParent
  Public Overrides Sub XYZ(ByVal ParamArray aA() As Single)
    ' aA.Length is the number of parameters passed
    ' if aA.Length=0, no parameters were passed
    ' if aA.Length=1, one parameter is in aA(0)
    ' if aA.Length=2, two parameters are in aA(0) and aA(1)
    ' etc
  End Sub
End Class

Hello -

The ParamArray is a great idea but the example I posted is only a
simplified version of the interface. There is more than one optional
argument and some mandatory arguments as well. The problem is that
the optional arguments are of different types (e.g. String) so I could
not replace them by a single ParamArray and mixing the Optional with
the ParamArray will not work.

Thanks,
Joe
 
A

Armin Zingler

Joe said:
Hello -

I am defaulting an optional parameter to Single.NaN so I can determine
whether it was passed in or not. The problem is when I do this in an
overriden Sub/Function it will not compile. Here is the code ...

MustInherit Class cParent
Public MustOverride Sub XYZ(Optional ByVal aA As Single =
Single.NaN)
End Class

Class cChild
Inherits cParent
Public Overrides Sub XYZ(Optional ByVal aA As Single = Single.NaN)
' ...
End Sub
End Class

This causes a 'Public Overrides Sub XYZ([aA As Single = -1.#IND])'
cannot override 'Public MustOverride Sub XYZ([aA As Single = -1.#IND)'
because they differ by the default values of optional parameters.

Is this a glitch in Visual Studio or am I doing something wrong? I
tried it in both Visual Studio 2003 (.NET 1.1) and Visual Studio 2008
(.NET 2.0, 3.0, 3.5) ...


I'd use Nullable(Of Single) instead. Though, not available in .Net 1.1.


Armin
 
M

Michel Posseth [MCP]

Armin Zingler said:
Joe said:
Hello -

I am defaulting an optional parameter to Single.NaN so I can determine
whether it was passed in or not. The problem is when I do this in an
overriden Sub/Function it will not compile. Here is the code ...

MustInherit Class cParent
Public MustOverride Sub XYZ(Optional ByVal aA As Single =
Single.NaN)
End Class

Class cChild
Inherits cParent
Public Overrides Sub XYZ(Optional ByVal aA As Single = Single.NaN)
' ...
End Sub
End Class

This causes a 'Public Overrides Sub XYZ([aA As Single = -1.#IND])'
cannot override 'Public MustOverride Sub XYZ([aA As Single = -1.#IND)'
because they differ by the default values of optional parameters.

Is this a glitch in Visual Studio or am I doing something wrong? I
tried it in both Visual Studio 2003 (.NET 1.1) and Visual Studio 2008
(.NET 2.0, 3.0, 3.5) ...


I'd use Nullable(Of Single) instead. Though, not available in .Net 1.1.


Armin

That wouldn`t work as nullable datatypes are not allowed as optional
parameters

Michel
 
A

Armin Zingler

Michel said:
This causes a 'Public Overrides Sub XYZ([aA As Single = -1.#IND])'
cannot override 'Public MustOverride Sub XYZ([aA As Single =
-1.#IND)' because they differ by the default values of optional
parameters.

Is this a glitch in Visual Studio or am I doing something wrong? I
tried it in both Visual Studio 2003 (.NET 1.1) and Visual Studio
2008 (.NET 2.0, 3.0, 3.5) ...


I'd use Nullable(Of Single) instead. Though, not available in .Net
1.1.


Armin

That wouldn`t work as nullable datatypes are not allowed as optional
parameters

Using Nullable, the parameter does not have to be optional anymore.


Armin
 
M

Michel Posseth [MCP]

Armin Zingler said:
Michel said:
This causes a 'Public Overrides Sub XYZ([aA As Single = -1.#IND])'
cannot override 'Public MustOverride Sub XYZ([aA As Single =
-1.#IND)' because they differ by the default values of optional
parameters. Is this a glitch in Visual Studio or am I doing something
wrong? I
tried it in both Visual Studio 2003 (.NET 1.1) and Visual Studio
2008 (.NET 2.0, 3.0, 3.5) ...


I'd use Nullable(Of Single) instead. Though, not available in .Net
1.1. Armin

That wouldn`t work as nullable datatypes are not allowed as optional
parameters

Using Nullable, the parameter does not have to be optional anymore.


Armin

I thought about that too ( you know that i actually had the same idea ) but
in that case he must always suply a parameter value even if it is Nothing
and i just guess that this is exactly what he did not want in the first
place

or am i missing something here ?

regards

Michel
 
A

Armin Zingler

Michel said:
Armin Zingler said:
Michel said:
This causes a 'Public Overrides Sub XYZ([aA As Single = -1.#IND])'
cannot override 'Public MustOverride Sub XYZ([aA As Single =
-1.#IND)' because they differ by the default values of optional
parameters. Is this a glitch in Visual Studio or am I doing
something wrong? I
tried it in both Visual Studio 2003 (.NET 1.1) and Visual Studio
2008 (.NET 2.0, 3.0, 3.5) ...


I'd use Nullable(Of Single) instead. Though, not available in .Net
1.1. Armin

That wouldn`t work as nullable datatypes are not allowed as optional
parameters

Using Nullable, the parameter does not have to be optional anymore.


Armin

I thought about that too ( you know that i actually had the same idea
) but in that case he must always suply a parameter value even if it
is Nothing and i just guess that this is exactly what he did not want
in the first place

or am i missing something here ?

I should have added that I'd overload the function then.

Public MustOverride Sub XYZ()
Public MustOverride Sub XYZ(ByVal aA As Nullable(of Single))

'inheriting class:

Public Overrides Sub XYZ()
XYZ(Nothing) 'Or is there a better way to pass
'a nullable type with HasValue = False?
End Sub

Public Overrides Sub XYZ(ByVal aA As Nullable(of Single))
if aA.HasValue then
else
end if
End Sub


Armin
 
M

Michel Posseth [MCP]

Armin Zingler said:
Michel said:
Armin Zingler said:
Michel Posseth [MCP] wrote:
This causes a 'Public Overrides Sub XYZ([aA As Single = -1.#IND])'
cannot override 'Public MustOverride Sub XYZ([aA As Single =
-1.#IND)' because they differ by the default values of optional
parameters. Is this a glitch in Visual Studio or am I doing
something wrong? I
tried it in both Visual Studio 2003 (.NET 1.1) and Visual Studio
2008 (.NET 2.0, 3.0, 3.5) ...


I'd use Nullable(Of Single) instead. Though, not available in .Net
1.1. Armin

That wouldn`t work as nullable datatypes are not allowed as optional
parameters

Using Nullable, the parameter does not have to be optional anymore.


Armin

I thought about that too ( you know that i actually had the same idea
) but in that case he must always suply a parameter value even if it
is Nothing and i just guess that this is exactly what he did not want
in the first place

or am i missing something here ?

I should have added that I'd overload the function then.

Public MustOverride Sub XYZ()
Public MustOverride Sub XYZ(ByVal aA As Nullable(of Single))

'inheriting class:

Public Overrides Sub XYZ()
XYZ(Nothing) 'Or is there a better way to pass 'a nullable type
with HasValue = False?
End Sub

Public Overrides Sub XYZ(ByVal aA As Nullable(of Single))
if aA.HasValue then
else
end if
End Sub


Armin


You convinced me with this construction :) ,
i would recomend the TS to keep this construction in mind when upgrading

regards

Michel
 

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