By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,662 Members | 1,278 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,662 IT Pros & Developers. It's quick & easy.

Override property

P: n/a
Hello All,

I have two classes, only the type of one of the properties is different.
How should I declare the two classes?

For example:
I want to declare 2 Classes, named MySQLIntfield and MySQLStrField. Both
have the Name and Value properties, but one of the type of value
property is string and the other is Int.
Their Methods are common, like get NameValue string or getNameValue xml
string.

I listed my code below. But I don't like the idea to declare mValue as
object. Is there any other way?

Many thanks.

Gary

-----------------------------------------------------

Class MySQLField
Protected mName As String
Protected mValue As Object

Property SQLName() As String
Get
Return mName
End Get
Set(ByVal Value As String)
mName = Value
End Set
End Property

ReadOnly Property NameValue()
Get
Return mName.ToString + mValue.ToString
End Get
End Property

'I try to declare like this, but doesn't work.
'The error message is:
'can not overload each other, because they differ
'only return types.
'MustOverride Property Value() as object

End Class

Class MySQLIntField
Inherits MySQLField
Property Value() As Int16
Get
Return CType(mvalue, Int16)
End Get
Set(ByVal Value As Int16)
mvalue = Value
End Set
End Property
End Class

Class MySQLStringField
Inherits MySQLField

Property Value() As String
Get
Return CType(mvalue, String)
End Get
Set(ByVal Value As String)
mvalue = Value
End Set
End Property

End Class

Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies

P: n/a
Gary,

Is it something as this that you want to do?

\\\
Class MySQLIntField
Inherits MySQLField
Shadows Property Value() As Int16
///

I hope this helps?

Cor
Nov 21 '05 #2

P: n/a
Cor Ligthert [MVP] wrote:
Gary,

Is it something as this that you want to do?


Many thanks for your reply. I'm not sure whether "shadows property"
works. Maybe I didn't express myself clearly. Let me try again using
another example. I listed the code below.

The Case:
The base class is "SearchField", which has three properties, Checked,
SQLFieldName, Value and one method named GetSQLString(). In the base
class, the type of value is declared as object. There are two child
classes "StringSearchField" and "IntSearchField" inherit from
"SearchField".

What I want to do:
1. In the StringSearchField class, the type of value could be string,
and in the IntSearchField, it could be int.
2. Implement the getSQLString in the base class instead of in the child
class.
If the type of the value is string, then getSQLString should be:
Return mSQLFieldName + " like " + mValue + "%"

If the type of the value is int, then getSQLString should be:
Return mSQLFieldName + " = " + mValue + "%"

I tried many solutions but failed. At the end, I have to declare the
value property in the child class and implement the getSQLString in the
child. Far from satisfaction. Could you give some advice?

Thanks again.

Gary.

------------------------------------------------------------
<Serializable()> _
Public MustInherit Class SearchField
Protected mChecked As Boolean
Protected mSQLFieldName As String

ReadOnly Property SQLFieldName() As String
Get
Return mSQLFieldName
End Get
End Property

Property Checked() As Boolean
Get
Return mChecked
End Get
Set(ByVal Value As Boolean)
mChecked = Value
End Set
End Property

Private Sub New()

End Sub

Sub New(ByVal SQLFieldName As String)
mChecked = False
mSQLFieldName = SQLFieldName
End Sub
MustOverride Function GetSQLString() As String

End Class

<Serializable()> _
Public Class StringSearchField
Inherits SearchField

Private mValue As String

Public Property Value() As String
Get
Return mValue
End Get

Set(ByVal Value As String)
mValue = mValue
End Set
End Property

Public Sub New(ByVal SQLFieldName As String)
MyBase.New(SQLFieldName)
End Sub

Public Overrides Function GetSQLString() As String
Return mSQLFieldName + " like " + mValue + "%"
End Function

End Class

<Serializable()> _
Public Class IntSearchField
Inherits SearchField

Private mValue As Int16

Public Property Value() As Int16
Get
Return mValue
End Get

Set(ByVal Value As Int16)
mValue = Value
End Set
End Property

Public Sub New(ByVal SQLFieldName As String)
MyBase.New(SQLFieldName)
End Sub

Public Overrides Function GetSQLString() As String
Return mSQLFieldName + " = " + mValue
End Function

End Class
Nov 21 '05 #3

P: n/a
"Gary12009" <ga*******@yahoo.com> schrieb:
I have two classes, only the type of one of the properties is different.
How should I declare the two classes?

For example:
I want to declare 2 Classes, named MySQLIntfield and MySQLStrField. Both
have the Name and Value properties, but one of the type of value property
is string and the other is Int.
Their Methods are common, like get NameValue string or getNameValue xml
string.


The problem is that overriding a property which differs in the return type
would lead to undesired behavior. Imagine the base class containing an
overridable property of type 'String' and a derived class overrinding the
property with return type 'Integer'. A reference of the base type can
reference an instance of the derived type. By assiging a value to the
property the compiler can only check if the assigned value has the type of
the property in the base class, but actually this type is incompatible to
the type of the property that exists in the derived class.

As you already said, one solution is to make the property return an
'Object'. Alternatively you could make 'Value' a non-virtual property, but
you will loose the benefits of polymorphism.

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

Nov 21 '05 #4

P: n/a
Gary,

I thought the answer from Herfried was sufficient, however than I thought,
why does Gary not just use an overriden function?

(The reason can be that you want to bind or that you want to use a property
grid, otherwise I would do that)

I hope this helps,

Cor
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.