B
Beth
Hello.
I'm trying to figure out how to create subclasses with properties specific
to the subclass and so far it isn't going well.
Right now I have a class with an enum representing the type. The class has
all the properties specific to all the types, but what I want instead is to
move those properties to subclasses specific to each type.
I have code like this:
Friend Class ValidateValue
Private _textMaxLen As Integer
Private _textMinLen As Integer
Private _validateType As validateTypes
Friend Sub New(ByVal validate As String)
Dim compoundType As String
Dim remainder As String
Dim openParenPos As Integer
Dim commaPos As Integer
openParenPos = InStr(validate, "(")
If openParenPos > 0 Then
compoundType = Mid(validate, 1, openParenPos - 1)
remainder = Mid(validate, openParenPos + 1)
remainder = Mid(remainder, 1, Len(remainder) - 1) ' strip closing paren
Select Case compoundType
Case "text", "textnum"
If compoundType = "text" Then
_validateType = validateTypes.text
Else
_validateType = validateTypes.textNum
End If
commaPos = InStr(remainder, ",")
If commaPos = 0 Then
_textMaxLen = CInt(remainder)
Else
_textMaxLen = CInt(Mid(remainder, 1, commaPos - 1))
_textMinLen = CInt(Mid(remainder, commaPos + 1))
End If
But since the maxLen and minLen properties are specific to the two text
types, what I want to do is declare a subclass which inherits from the
existing class and contains and sets the validateType, textMaxLen, and
textMinLen properties given the compoundType and remainder.
The problem I'm having is with the subclass's constructor. In the subclass,
I want to pass the compoundType and remainder, and have code like:
'Friend Class ValidateText
' Inherits ValidateValue
' Private _textMaxLen As Integer
' Private _textMinLen As Integer
' Private _validateType As validateTypes
' Friend Sub New(ByVal compoundType As String, ByVal remainder As String)
' Dim commaPos As Integer
' If compoundType = "text" Then
' _validateType = validateTypes.text
' Else
' _validateType = validateTypes.textNum
' End If
' commaPos = InStr(remainder, ",")
' If commaPos = 0 Then
' _textMaxLen = CInt(remainder)
' Else
' _textMaxLen = CInt(Mid(remainder, 1, commaPos - 1))
' _textMinLen = CInt(Mid(remainder, commaPos + 1))
' End If
' End Sub
But that doesn't work because the base class doesn't have a constructor
without parameters. I want to use a parameter into a base class as a key to
determine the subclass to create and then create one of my subclasses.
I'm thinking in the base class I need a variable like:
Private _child as ValidateValue
And in the base class's constructor, have code like:
Select Case compoundType
Case "text", "textnum"
_child = new ValidateText(compoundType, remainder)
But I can't get the subclass to work.
What should I be doing differently?
Thanks for any help,
-Beth
I'm trying to figure out how to create subclasses with properties specific
to the subclass and so far it isn't going well.
Right now I have a class with an enum representing the type. The class has
all the properties specific to all the types, but what I want instead is to
move those properties to subclasses specific to each type.
I have code like this:
Friend Class ValidateValue
Private _textMaxLen As Integer
Private _textMinLen As Integer
Private _validateType As validateTypes
Friend Sub New(ByVal validate As String)
Dim compoundType As String
Dim remainder As String
Dim openParenPos As Integer
Dim commaPos As Integer
openParenPos = InStr(validate, "(")
If openParenPos > 0 Then
compoundType = Mid(validate, 1, openParenPos - 1)
remainder = Mid(validate, openParenPos + 1)
remainder = Mid(remainder, 1, Len(remainder) - 1) ' strip closing paren
Select Case compoundType
Case "text", "textnum"
If compoundType = "text" Then
_validateType = validateTypes.text
Else
_validateType = validateTypes.textNum
End If
commaPos = InStr(remainder, ",")
If commaPos = 0 Then
_textMaxLen = CInt(remainder)
Else
_textMaxLen = CInt(Mid(remainder, 1, commaPos - 1))
_textMinLen = CInt(Mid(remainder, commaPos + 1))
End If
But since the maxLen and minLen properties are specific to the two text
types, what I want to do is declare a subclass which inherits from the
existing class and contains and sets the validateType, textMaxLen, and
textMinLen properties given the compoundType and remainder.
The problem I'm having is with the subclass's constructor. In the subclass,
I want to pass the compoundType and remainder, and have code like:
'Friend Class ValidateText
' Inherits ValidateValue
' Private _textMaxLen As Integer
' Private _textMinLen As Integer
' Private _validateType As validateTypes
' Friend Sub New(ByVal compoundType As String, ByVal remainder As String)
' Dim commaPos As Integer
' If compoundType = "text" Then
' _validateType = validateTypes.text
' Else
' _validateType = validateTypes.textNum
' End If
' commaPos = InStr(remainder, ",")
' If commaPos = 0 Then
' _textMaxLen = CInt(remainder)
' Else
' _textMaxLen = CInt(Mid(remainder, 1, commaPos - 1))
' _textMinLen = CInt(Mid(remainder, commaPos + 1))
' End If
' End Sub
But that doesn't work because the base class doesn't have a constructor
without parameters. I want to use a parameter into a base class as a key to
determine the subclass to create and then create one of my subclasses.
I'm thinking in the base class I need a variable like:
Private _child as ValidateValue
And in the base class's constructor, have code like:
Select Case compoundType
Case "text", "textnum"
_child = new ValidateText(compoundType, remainder)
But I can't get the subclass to work.
What should I be doing differently?
Thanks for any help,
-Beth