473,386 Members | 1,679 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,386 software developers and data experts.

Override property

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
4 8609
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Robin Becker | last post by:
Is there a way to override a data property in the instance? Do I need to create another class with the property changed? -- Robin Becker
7
by: Thomas Kehl | last post by:
Hello. I provided a class, which derives from the class DataView. The only thing, which I would like to do in this class am to overwrite the Property COUNT. I wars it however simply not. I get...
5
by: Eric Johannsen | last post by:
I have a simple object that inherits from CollectionBase and overrides the Count property: namespace MyTest { public class CollTest : System.Collections.CollectionBase { public override int...
5
by: Darius | last post by:
I'm writing here in hopes that someone can explain the difference between the new and virtual/override keywords in C#. Specifically, what is the difference between this: public class Window {...
7
by: Dave Y | last post by:
I am a newbie to C# and am having trouble trying to override a ListView property method. I have created a new class derived from the Forms.Listview and I cannot figure out the syntax to override...
1
by: Ĺ krat Bolfenk | last post by:
How can I override property in C++/CLI. Well I know how to override when the input and output types are the same (and number) ..., but I would like the derived (overridden) property to return...
8
by: bdeviled | last post by:
I am deploying to a web environment that uses load balancing and to insure that sessions persist across servers, the environment uses SQL to manage sessions. The machine.config file determines how...
4
by: R. MacDonald | last post by:
Hello, Group, I have been curious about a statement I see occasionally in the VB (v2003) help. Sometimes the information about a property will indicate that it is overridable, and I have been...
14
by: Dave Booker | last post by:
It looks like the language is trying to prevent me from doing this sort of thing. Nevertheless, the following compiles, and I'd like to know why it doesn't work the way it should: public class...
1
by: Joshua | last post by:
I'm creating a web control that has an image and a text box. I would like to then override the Visible and Text property of the usercontrol, so when you reference the visible property of the user...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.