Setting properties differently in child Class.

How can iSet() properties differently in my base class. Cos i want to validate the values parsed in differently. I felt that overriding theGet() is not very good, its copy and paste. any better way to do this?
[337 byte] By [IceAngel89] at [2007-12-28]
# 1

Hi,

Can you expand on your question please?

Are you wanting to change a datatype within a CLASS or simply it's value?

One idea you could do is have all your datatypes as STRING and convert to type string.

E.g. CLASS name is Account.Vb

If account has AccountName, SortCode and AccountNumber elements all As type STRING.

Then in code if you use any other datatype you could do for example>>

Dim accountNum As Integer

Dim newAccount As New Account 'You would need a NEW Sub in your CLASS code.

newAccount.AccountNumber=CSTR(accountNum)

If that's not what your meaning then please expand on your question.

Regards,

S_DS

Spidermans_DarkSide at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2
Ok. u may have remembered answering my past questions, I am doing a IP class. So, the base class is IPv4Base(Parent). Then the child classes are IPv4 & SubnetMask. So in my IPv4base class my properties FirstOct, SecOct, ... are Bytes, In the child class still bytes but i want the Set() to be different. I wanted to validate it differently. So i need to override it? but by overidding it, it means i also repeat the code in the get(), not a very good practice is it?

Public Class IPv4Base

Private _FirstOct As Byte
Private _SecOct As Byte
Private _ThirdOct As Byte
Private _FourthOct As Byte
Private _Class As String

' ++++++++++++++++++++++++++++++++++++++++++++++++++++
' Constructors
' ++++++++++++++++++++++++++++++++++++++++++++++++++++

Public Sub New()
_FirstOct = 0
.....
End Sub

' New() accepting Bytes
Public Sub New(ByVal byte1 As Byte, ByVal byte2 As Byte, ByVal byte3 As Byte, ByVal byte4 As Byte)
_FirstOct = byte1
.....
End Sub

' New() accepting Strings
' Requires validation
Public Sub New(ByVal str1 As String, ByVal str2 As String, ByVal str3 As String, ByVal str4 As String)
Dim bytesArray(3) As Byte
Dim index As Int16 = 0

' Validating Strings
Try
bytesArray(0) = CByte(str1)
...
Catch ex As Exception
MessageBox.Show("Error .....)
' Exit Sub New() if have errors
Exit Sub
End Try

' If validated then set values
_FirstOct = bytesArray(0)
_SecOct = bytesArray(1)
_ThirdOct = bytesArray(2)
_FourthOct = bytesArray(3)

End Sub

' ++++++++++++++++++++++++++++++++++++++++++++++++++++
' Properties
' ++++++++++++++++++++++++++++++++++++++++++++++++++++

Public Overridable Property FirstOct() As Byte
Get
Return _FirstOct
End Get
Set(ByVal value As Byte)
_FirstOct = value
End Set
End Property

Public Overridable Property SecOct() As Byte
Get
Return _SecOct
End Get
Set(ByVal value As Byte)
_SecOct = value
End Set
End Property

Public Overridable Property ThirdOct() As Byte
Get
Return _ThirdOct
End Get
Set(ByVal value As Byte)
_ThirdOct = value
End Set
End Property

Public Overridable Property FourthOct() As Byte
Get
Return _FourthOct
End Get
Set(ByVal value As Byte)
_FourthOct = value
End Set
End Property

Public Property IPClass() As String
Get
Return _Class
End Get
Set(ByVal value As String)
_Class = value
End Set
End Property

Public Class SubnetMask
Inherits IPv4Base

Private _SlashNotation As Integer
Private validValues() As Byte = {0, 128, 192, 224, 240, 248, 252}

Public Property SlashNotation() As Integer
Get
Return _SlashNotation
End Get
Set(ByVal value As Integer)
' Validating
......
Exit Property
End If
_SlashNotation = value

' Sync with Decimal notation

End Set
End Property

Public Sub New()

End Sub

' New() accepting Bytes
' My Sub New() are copied and pasted from IPv4Base is this supposed to be done,
' I thought they should be inherited?

Public Sub New(ByVal byte1 As Byte, ByVal byte2 As Byte, ByVal byte3 As Byte, ByVal byte4 As Byte)
FirstOct = byte1
.....
End Sub

' New() accepting Strings
' Requires validation
Public Sub New(ByVal str1 As String, ByVal str2 As String, ByVal str3 As String, ByVal str4 As String)
Dim bytesArray(3) As Byte
Dim index As Int16 = 0

' Validating Strings
Try
bytesArray(0) = CByte(str1)
bytesArray(1) = ......
Catch ex As Exception
MessageBox.Show("Error .....)
' Exit Sub New() if have errors
Exit Sub
End Try

' If validated then set values
FirstOct = bytesArray(0)
SecOct = bytesArray(1)
ThirdOct = bytesArray(2)
FourthOct = bytesArray(3)

End Sub

So basically i want to change the way set() is done in the SubnetMask class. I tried overidding:

Public Overrides Property FirstOct() As Byte
Get
Return _FirstOct
' error:IPv4Base._FirstOct is not accessible because its 'Private'

End Get
Set(ByVal value As Byte)
_FirstOct = value ' same Error Here also
End Set
End Property


IceAngel89 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

Hi

I would recommend that you follow your instincts and override the property. You do not need to cut and paste the base class code if you do not want to change the getter .. simply type Get : Return MyBase.PropertyName : End Get.

Sometime I do prefer to split the Get and Set operation into base class methods and allow those to be overridden. This allows you to easily override just the part you want to.

Hope this is what you were looking for.

Richard

Eg

Pubic Property Foo As String

Get : Return Me.getFoo: End Get

Set (ByVal value As String) : Me.setFoo(value) : End Set

End Property

Protected Overridable Function getFoo As String

Return Me.mFoo

End Function

Protected Overridable Sub setFoo (ByVal value As String)

Me.mFoo = Value

End Sub

DickDonny at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

Additionally, you don't need to copy the code in your sub new() functions if it's the same functionality. Just call the appropriate base function MyBase.New(...) (didn't we already do this in another thread?)

SJWhiteley at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5
SJWhiteley wrote:

Additionally, you don't need to copy the code in your sub new() functions if it's the same functionality. Just call the appropriate base function MyBase.New(...) (didn't we already do this in another thread?)

Ya, i remembered, but didn't quite understand, so just to clarify, if in MyBaseClass, i have the Sub New() with code already, i just need to put Sub New() with no code in MyChildClass? sorry i just a bit confused.

IceAngel89 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 6

That's OK...

Public Class BaseClass

Sub New()

End Sub

Sub New(ByVal a As Integer, ByVal b As Integer)

' Lots of code here...

End Sub

End Class

Public Class InheritedClass : Inherits BaseClass

Sub New(ByVal a As Integer, ByVal b As Integer)

MyBase.New(a, b)

End Sub

End Class

As you can see in the inherited class, instead of duplicating all the code from the prameterized constructor in the base class, we simply call the base parameterized constructor. The reason is that the inherited class does not expose the base class parameterized constructor: we must expose it manually.

Hope that makes a bit more sense.

SJWhiteley at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 7
Thanks, now i know.
IceAngel89 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...