Override property

G

Gary12009

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
 
C

Cor Ligthert [MVP]

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
 
G

Gary12009

Cor said:
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
 
H

Herfried K. Wagner [MVP]

Gary12009 said:
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.
 
C

Cor Ligthert [MVP]

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
 

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