Missing methods etc. in derived classes

E

eje

I'm trying to understand class hierarchies in VB .NET.
I'm designing an application where data come in textfiles
from different sources and will be stored in a database.
To avoid future problems the importdata must be well
validated. There are several tables and fields in the
database. My idea is to have some 'baseclasses' with one
property (value) and one validatingfunction to validate
the value before it is accepted. (See class StringBase
below) A limited number of 'baseclasses' where each
normally could be used for more than one field should
cover all my needs. Next step would be to build one class
per databasetable. (See class Car below). These classes
could finally be used in the importprocedures. (See class
ImportVehicleData, Sub ImportCarData below).

My problem is that Validate and Value are not available
as I have designed the Sub ImportCarData. If I dim Name
and Color instead of Car it's no problem but then I loose
much of my design idea. The .NET framework has an endless
number of derived classes with properties and methods so
I think it must be possible to solve my problem too. (The
code below is only an example to describe my problem.)

Public Class StringBase
Public Property Value() As String
. . .
End Property
Public Overridable Function Validate(ByVal value) As
Boolean
'Validate Len(value) <= 10. Return True if OK
. . .
End Function
End Class

Public Class Car
Public Sub New()
End Sub
Public Class Name
Inherits StringBase
End Class
Public Class Color
Inherits StringBase
End Class
End Class

Public Class ImportrVehicleData
Sub ImportCarData()
Dim Data As New Car
'Get first data
If Data.Name.Validate(value) Then
Data.Name.Value = value
Else
'Do something
End If
'Get next data
If Data.Color.Validate(value) Then
Data.Color.Value = value
Else
'Do something
End If
'If all data OK send data to database
End Sub
. . .
End Class

Thanks for any help

Eje
 
D

David Browne

eje said:
I'm trying to understand class hierarchies in VB .NET.
I'm designing an application where data come in textfiles
from different sources and will be stored in a database.
To avoid future problems the importdata must be well
validated. There are several tables and fields in the
database. My idea is to have some 'baseclasses' with one
property (value) and one validatingfunction to validate
the value before it is accepted. (See class StringBase
below) A limited number of 'baseclasses' where each
normally could be used for more than one field should
cover all my needs. Next step would be to build one class
per databasetable. (See class Car below). These classes
could finally be used in the importprocedures. (See class
ImportVehicleData, Sub ImportCarData below).

My problem is that Validate and Value are not available
as I have designed the Sub ImportCarData. If I dim Name
and Color instead of Car it's no problem but then I loose
much of my design idea. The .NET framework has an endless
number of derived classes with properties and methods so
I think it must be possible to solve my problem too. (The
code below is only an example to describe my problem.)

How about this


Public Class StringBase
Public Overridable Function Validate(ByVal value As String) As Boolean
Return True
End Function
End Class

Public Class FixedLengthString
Inherits StringBase
Protected length As Integer
Public Sub New(ByVal length As Integer)
Me.length = length
End Sub
Public Overrides Function Validate(ByVal Value As String) As Boolean
Return value.Length <= length
End Function
End Class

Public Class EnumeratedString
Inherits StringBase
Protected values() As String
Public Sub New(ByVal values() As String)
Me.values = values
End Sub
Public Overrides Function Validate(ByVal Value As String) As Boolean
For Each v As String In values
If v = Value Then Return True
Next
Return False
End Function
End Class

Public Class Car
Public Shared ReadOnly Name As New FixedLengthString(10)
Public Shared ReadOnly Color As New EnumeratedString(New String()
{"green", "red"})
Public Shared ReadOnly BodyStyle As New BodyStyleType

Public Class BodyStyleType
Inherits StringBase
Public Overrides Function Validate(ByVal Value As String) As Boolean
Return (Value.Length < 20 And Value.IndexOf(" "c) = -1)
End Function
End Class
End Class

Sub main()

Console.WriteLine(Car.Color.Validate("blue"))
Console.WriteLine(Car.Color.Validate("red"))
Console.WriteLine(Car.Name.Validate("This name is much too long"))
Console.WriteLine(Car.BodyStyle.Validate("vega"))

End Sub



David
 
G

Guest

-----Original Message-----



How about this


Public Class StringBase
Public Overridable Function Validate(ByVal value As String) As Boolean
Return True
End Function
End Class

Public Class FixedLengthString
Inherits StringBase
Protected length As Integer
Public Sub New(ByVal length As Integer)
Me.length = length
End Sub
Public Overrides Function Validate(ByVal Value As String) As Boolean
Return value.Length <= length
End Function
End Class

Public Class EnumeratedString
Inherits StringBase
Protected values() As String
Public Sub New(ByVal values() As String)
Me.values = values
End Sub
Public Overrides Function Validate(ByVal Value As String) As Boolean
For Each v As String In values
If v = Value Then Return True
Next
Return False
End Function
End Class

Public Class Car
Public Shared ReadOnly Name As New FixedLengthString (10)
Public Shared ReadOnly Color As New EnumeratedString (New String()
{"green", "red"})
Public Shared ReadOnly BodyStyle As New BodyStyleType

Public Class BodyStyleType
Inherits StringBase
Public Overrides Function Validate(ByVal Value As String) As Boolean
Return (Value.Length < 20 And Value.IndexOf (" "c) = -1)
End Function
End Class
End Class

Sub main()

Console.WriteLine(Car.Color.Validate("blue"))
Console.WriteLine(Car.Color.Validate("red"))
Console.WriteLine(Car.Name.Validate("This name is much too long"))
Console.WriteLine(Car.BodyStyle.Validate("vega"))

End Sub



David

Thanks David. This gave much to think of for a beginner!
Eje
 

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