473,320 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Confusion on Override

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


Nov 21 '05 #1
4 2161
In article <ux**************@TK2MSFTNGP12.phx.gbl>, Stephen Costanzo wrote:
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.

--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 2 Build 2600
System Up Time: 0 Days, 2 Hours, 11 Minutes, 32 Seconds
Nov 21 '05 #2
Ok, missed the fact that the override was Char as opposed to string. Thank
you

"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:O2**************@tk2msftngp13.phx.gbl...
In article <ux**************@TK2MSFTNGP12.phx.gbl>, Stephen Costanzo
wrote:
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.

--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 2 Build 2600
System Up Time: 0 Days, 2 Hours, 11 Minutes, 32 Seconds

Nov 21 '05 #3
"Stephen Costanzo" <sx********@hotmail.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4

"Stephen Costanzo" <sx********@hotmail.com> wrote in message
news:ux**************@TK2MSFTNGP12.phx.gbl...
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
Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ashkon | last post by:
Can someone please help explain the following behaviour, or point me to a post that explains it? While i was debugging one of my company's programs i noticed some objects behaving strangely. I...
4
by: Chris Mabee | last post by:
Hello all, and Merry Christmas, I'm having a problem understanding an example of an array based implementation of a stack in a textbook of mine. The code in question is written below. The syntax...
15
by: Rhy Mednick | last post by:
I have a class (let's call it ClassA) that I've written which has events. In another class (let's call it ClassB) I create a collection of ClassA objects. In a third class (ClassC) I create a...
3
by: flat_ross | last post by:
For anyone who is just getting into VB.NET and/or is starting to work with inheritance I would like to point out a potential pitfall. We found this confusion recently when code-reviewing an...
2
by: Ron Dahl | last post by:
I'm very confused on how the Protected Overrides works. I created a new project with a new form1 and a new datagrid called myDataGrid. I created a simple DataTable and put 5 rows and 5 columns of...
4
by: Paul Cheetham | last post by:
Hi, I have a combo box in a Windows Forms app (c# VS2003, .Net 1.1) where I am adding items at runtime using the Items.Add method. I need to be able to also set the Value for each item in the...
6
by: 31337one | last post by:
Hello all, Let me introduce myself. I would say that I am an intermediate programmer in C++. I am studying comptuer science in college. However, I have never had a formal introduction to...
5
by: Cameron Laird | last post by:
Question: import subprocess, StringIO input = StringIO.StringIO("abcdefgh\nabc\n") # I don't know of a compact, evocative, and # cross-platform way to exhibit this behavior. # For now, depend...
6
by: Donn Ingle | last post by:
Hi, Here's some code, it's broken: class Key( object ): def __init__(self): self.props = KeyProps() def __getattr__(self, v): return getattr( self.props,v ) def __setattr__(self,var,val):
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.