Confusion on Override

  • Thread starter Thread starter Stephen Costanzo
  • Start date Start date
S

Stephen Costanzo

My goal is to have a property or function that returns different types of
returns with no initial arguments. For example

Public Class Something

Private sData as string
Private lData as long

Public Property X () as String
Get
return sData
End Get

Set (byVal Value as string)
sData = Value
end set
end Property

Public Property X () as Long
Get
return lData
End Get

Set (byVal Value as Long)
lData = Value
end set
end Property

end class

Then have my code simply have the correct data type and set it equal, i.e.
Dim sInfo as String = Something.X

When I compile the above code the error I get is Public Property X() as
String and Public Property as Long cannot overload each other because they
differ only by return types.

Now, the VAL function in VB.NET allows you to pass in a string and has two
possible return types: Double and Integer. I am to assume that this is
something that can only be done by the .NET code or am i missing a keyword
in the property statement? I would need to be able to do this in a function
as well; I have tried and gotten the same error as above.

Thank you
 
My goal is to have a property or function that returns different types of
returns with no initial arguments. For example

Public Class Something

Private sData as string
Private lData as long

Public Property X () as String
Get
return sData
End Get

Set (byVal Value as string)
sData = Value
end set
end Property

Public Property X () as Long
Get
return lData
End Get

Set (byVal Value as Long)
lData = Value
end set
end Property

end class

Then have my code simply have the correct data type and set it equal, i.e.
Dim sInfo as String = Something.X

When I compile the above code the error I get is Public Property X() as
String and Public Property as Long cannot overload each other because they
differ only by return types.

Now, the VAL function in VB.NET allows you to pass in a string and has two
possible return types: Double and Integer. I am to assume that this is
something that can only be done by the .NET code or am i missing a keyword
in the property statement? I would need to be able to do this in a function
as well; I have tried and gotten the same error as above.

Thank you

Val function has several overloaded versions (3 actually).

Public Overloads Function Val (ByVal Expression As String) As Double
Public Overloads Function Val (ByVal Expression As Object) As Double
Public Overloads Function Val (ByVal Expression As Char) As Integer

You can not overload on return type alone.
 
Ok, missed the fact that the override was Char as opposed to string. Thank
you
 
Stephen Costanzo said:
My goal is to have a property or function that returns different types of
returns with no initial arguments.

You can archieve that using the 'Shadows' keyword if you indroduce the 2nd
property with the same name in a derived class, but this is not recommended
because 'Shadows' has a different purpose and its use somewhat "breaks"
polymorphism. I suggest to give the property a different name.
 
Stephen Costanzo said:
My goal is to have a property or function that returns different types of
returns with no initial arguments. For example

Public Class Something

Private sData as string
Private lData as long

Public Property X () as String
Get
return sData
End Get

Set (byVal Value as string)
sData = Value
end set
end Property

Public Property X () as Long
Get
return lData
End Get

Set (byVal Value as Long)
lData = Value
end set
end Property

end class

Private sData As Object

Public Property X() As Object
Get
Return sData
End Get
Set
sData = Value
End Set
End Property

This will allow different types (String, Long, Double, etc) based on the
type sData contains. But, to use, you'll want to cast it as the type you
want (IE: CStr(Something.X) or CLng(Something.X))

Another thing, overloads is the word you should be using instead of
overrides. Overrides is a different keyword with a different purpose
altogether :) Overloads allows multiple methods with the same name that
have different signatures. Overrides allows a derived class to contain the
same property/method but with different implementation.

Example of overrides:

Class A
Public Overridable Function GetHeight() As Integer
Return 123
End Function
End Class

Class B
Inherits A

Public Overrides Function GetHeight() As Integer
Return 321
End Function
End Class

Example of overloads:

Class A
Public Overloads Function Calculate(ByVal X As Integer, ByVal Y As
Integer) As Integer
Return X * Y
End Function

Public Overloads Function Calculate(ByVal X As Long, ByVal Y As Long) As
Long
Return X * Y
End Function

Public Overloads Function Calculate(ByVal X As Double, ByVal Y As
Double) As Double
Return X * Y
End Function
End Class

Hope this helps :)

Mythran
 

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

Back
Top