Abstract Class inheritance problem

T

tshad

In VS 2003, I am setting up an abstract class that is setting up classes for
each datatype of VB.Net (as well as C#).

I am trying to set it up so that most of the work is done in the Abstract
Class. It seems to work pretty well. I have the actual data memory
variables stored as an object as each class will use it as a different type
of object (not sure if this is a problem).

The system can tell if the variable is a string, boolean or integer pretty
well by the initial settings.

But it has a problem with decimal, singles and doubles.

When looking at the variables as they are created in the watch window:

*********************************************
Dim temp As New StringType
Dim temp2 As New StringType("The 2nd String")
Dim tempBool As New BooleanType
Dim tempBool2 As New BooleanType(True)
Dim tempInteger As New IntegerType
Dim tempInteger2 As New IntegerType(25)
Dim tempDecimal As New DecimalType
Dim tempDecimal2 As New DecimalType(15)
Dim tempSingle As New SingleType
Dim TempSingle2 As New SingleType(12.15)
Dim TempDouble As New DoubleType
Dim TempDouble2 As New DoubleType(15.23)
**********************************************

The system thinks the variables in the DecimalType class is an integer and
the variables in the SingleType and DoubleType class are Doubles.

Here is the Class so far:
********************************************
' Create to new variable types to handle nulls and track changes
' to standard variable types. This is for use with database variables.
' This will tell us if a vaiable has changed, give us the original and
current value,
' and tell us whether the current value and original value is/was null or
not.

imports System
imports System.IO

Namespace FtsData
MustInherit Class DataType
Protected _first As Object
Protected _data As Object
Private _changed As Boolean = False 'value changed from set
Private nullFirst As Boolean = False 'Was Null at start?
Private nullData As Boolean = False 'current data null

Public Function IsNull() As Boolean
return nullData
End Function

Public Function IsFirstNull() As Boolean
return nullFirst
End Function

Public Sub SetNull()
nullData = true
_data = ""
End Sub

Public Sub SetFirstNull()
nullFirst = true
_first = ""
End Sub

' Properties

Public Property First As Object
Get
return _first
End Get
Set
_first = value
End Set
End Property

Public Property Data As Object
Get
return _data
End Get
Set
_data = value
_changed = true
nullData = false
End Set
End Property

Public Property Changed as Boolean
Get
return _changed
End Get
Set
_changed = value
End Set
End Property
End Class

Class StringType : Inherits DataType

Public Sub New()
_first = "" 'original data
_data = "" 'current data
End Sub

Public Sub New(initial As Object)
_first = initial
_data = initial
End Sub

End Class

Class IntegerType : Inherits DataType

Public Sub New()
_first = 0 'original data
_data = 0 'current data
End Sub

Public Sub New(initial As Object)
_first = 0
_data = 0
End Sub

End Class

Class IntType : Inherits DataType

Public Sub New()
_first = 0 'original data
_data = 0 'current data
End Sub

Public Sub New(initial As Object)
_first = initial
_data = initial
End Sub

End Class

Class BoolType : Inherits DataType

Public Sub New()
_first = False 'original data
_data = False 'current data
End Sub

Public Sub New(initial As Object)
_first = initial
_data = initial
End Sub

End Class

Class BooleanType : Inherits DataType

Public Sub New()
_first = False 'original data
_data = False 'current data
End Sub

Public Sub New(initial As Object)
_first = initial
_data = initial
End Sub

End Class

Class DecimalType : Inherits DataType

Public Sub New()
_first = 0 'original data
_data = 0 'current data
End Sub

Public Sub New(initial As Object)
_first = initial
_data = initial
End Sub

End Class

Class SingleType :Inherits DataType

Public Sub New()
_first = 0.0 'original data
_data = 0.0 'current data
End Sub

Public Sub New(initial As Object)
_first = initial
_data = initial
End Sub
End Class

Class DoubleType :Inherits DataType

Public Sub New()
_first = 0.0 'original data
_data = 0.0 'current data
End Sub

Public Sub New(initial As Object)
_first = initial
_data = initial
End Sub
End Class

End Namespace
********************************************

I could initally set them up in the classes, something like:
************************************
Class DoubleType :Inherits DataType
Private _first As Decimal
Private _data As Decimal

Public Sub New()
_first = 0.0 'original data
_data = 0.0 'current data
End Sub

Public Sub New(initial As Object)
_first = initial
_data = initial
End Sub
**********************************

But then the Properties in the Abstract class wouldn't be able to access
them. Actually as set up here the SetNull and SetFirstNull methods are not
correct as the variables are not always strings.

Do I have to set each each variable type in each class instead of an object
in the Abstract class?

If so, is there a way to get the Properties in the Abstract class to access
them?

Thanks,

Tom
 
T

Tom Shelton

In VS 2003, I am setting up an abstract class that is setting up classes for
each datatype of VB.Net (as well as C#).

I am trying to set it up so that most of the work is done in the Abstract
Class. It seems to work pretty well. I have the actual data memory
variables stored as an object as each class will use it as a different type
of object (not sure if this is a problem).

The system can tell if the variable is a string, boolean or integer pretty
well by the initial settings.

But it has a problem with decimal, singles and doubles.

When looking at the variables as they are created in the watch window:

*********************************************
Dim temp As New StringType
Dim temp2 As New StringType("The 2nd String")
Dim tempBool As New BooleanType
Dim tempBool2 As New BooleanType(True)
Dim tempInteger As New IntegerType
Dim tempInteger2 As New IntegerType(25)
Dim tempDecimal As New DecimalType
Dim tempDecimal2 As New DecimalType(15)
Dim tempSingle As New SingleType
Dim TempSingle2 As New SingleType(12.15)
Dim TempDouble As New DoubleType
Dim TempDouble2 As New DoubleType(15.23)
**********************************************

The system thinks the variables in the DecimalType class is an integer and
the variables in the SingleType and DoubleType class are Doubles.

That's because you are using object. Those are boxing rules. If you
pass a 15 to the single and double type they are going to look like
ints as well. If you want them to be typed, then your going to have
to type them (or upgrade to 2005 so you can use generics).

In 2003, it's going to take a little more coding - but you can do
something like the System.Data.SqlTypes.
 
T

tshad

Tom Shelton said:
That's because you are using object. Those are boxing rules. If you
pass a 15 to the single and double type they are going to look like
ints as well. If you want them to be typed, then your going to have
to type them (or upgrade to 2005 so you can use generics).

In 2003, it's going to take a little more coding - but you can do
something like the System.Data.SqlTypes.
 
T

tshad

Tom Shelton said:
That's because you are using object. Those are boxing rules. If you
pass a 15 to the single and double type they are going to look like
ints as well. If you want them to be typed, then your going to have
to type them (or upgrade to 2005 so you can use generics).

In 2003, it's going to take a little more coding - but you can do
something like the System.Data.SqlTypes.

To type them, I assume I would have to do it in each class like so:

******************************************
Class StringType : Inherits DataType
Protected _first As String
Protected _data As String

Public Sub New()
_first = "" 'original data
_data = "" 'current data
End Sub

Public Sub New(initial As Object)
_first = initial
_data = initial
End Sub

End Class

Class IntegerType : Inherits DataType
Protected _first As Integer
Protected _data As Integer

Public Sub New()
_first = 0 'original data
_data = 0 'current data
End Sub

Public Sub New(initial As Object)
_first = 0
_data = 0
End Sub

End Class

Class BooleanType : Inherits DataType
Protected _first As Boolean
Protected _data As Boolean

Public Sub New()
_first = False 'original data
_data = False 'current data
End Sub

Public Sub New(initial As Object)
_first = initial
_data = initial
End Sub

End Class

Class DecimalType : Inherits DataType
Protected _first As Decimal
Protected _data As Decimal

Public Sub New()
_first = 0 'original data
_data = 0 'current data
End Sub

Public Sub New(initial As Object)
_first = initial
_data = initial
End Sub

End Class
******************************************

If I do it this way, I assume I can't access the types from the Base class.
So I would need to change the Class to something like this:

******************************************
imports System
imports System.IO

Namespace FtsData
MustInherit Class DataType
Private _changed As Boolean = False 'value changed from set
Private nullFirst As Boolean = False 'Was Null at start?
Private nullData As Boolean = False 'current data null

Public Function IsNull() As Boolean
return nullData
End Function

Public Function IsFirstNull() As Boolean
return nullFirst
End Function
******************************************

But does that mean that I would need to the last part of the Base Class in
each of the Child Classes?
******************************************
Public Sub SetNull()
nullData = true
_data = ""
End Sub

Public Sub SetFirstNull()
nullFirst = true
_first = ""
End Sub

' Properties

Public Property First As Object
Get
return _first
End Get
Set
_first = value
End Set
End Property

Public Property Data As Object
Get
return _data
End Get
Set
_data = value
_changed = true
nullData = false
End Set
End Property

Public Property Changed as Boolean
Get
return _changed
End Get
Set
_changed = value
End Set
End Property
End Class

******************************************

This was why I was trying to use object so that I didn't have to use
duplicate code in each of my new classes

Thanks,

Tom

 
T

Tom Shelton

To type them, I assume I would have to do it in each class like so:

******************************************
Class StringType : Inherits DataType
Protected _first As String
Protected _data As String

Public Sub New()
_first = "" 'original data
_data = "" 'current data
End Sub

Public Sub New(initial As Object)
_first = initial
_data = initial
End Sub

End Class

Class IntegerType : Inherits DataType
Protected _first As Integer
Protected _data As Integer

Public Sub New()
_first = 0 'original data
_data = 0 'current data
End Sub

Public Sub New(initial As Object)
_first = 0
_data = 0
End Sub

End Class

Class BooleanType : Inherits DataType
Protected _first As Boolean
Protected _data As Boolean

Public Sub New()
_first = False 'original data
_data = False 'current data
End Sub

Public Sub New(initial As Object)
_first = initial
_data = initial
End Sub

End Class

Class DecimalType : Inherits DataType
Protected _first As Decimal
Protected _data As Decimal

Public Sub New()
_first = 0 'original data
_data = 0 'current data
End Sub

Public Sub New(initial As Object)
_first = initial
_data = initial
End Sub

End Class
******************************************

If I do it this way, I assume I can't access the types from the Base class.
So I would need to change the Class to something like this:

******************************************
imports System
imports System.IO

Namespace FtsData
MustInherit Class DataType
Private _changed As Boolean = False 'value changed from set
Private nullFirst As Boolean = False 'Was Null at start?
Private nullData As Boolean = False 'current data null

Public Function IsNull() As Boolean
return nullData
End Function

Public Function IsFirstNull() As Boolean
return nullFirst
End Function
******************************************

But does that mean that I would need to the last part of the Base Class in
each of the Child Classes?
******************************************
Public Sub SetNull()
nullData = true
_data = ""
End Sub

Public Sub SetFirstNull()
nullFirst = true
_first = ""
End Sub

' Properties

Public Property First As Object
Get
return _first
End Get
Set
_first = value
End Set
End Property

Public Property Data As Object
Get
return _data
End Get
Set
_data = value
_changed = true
nullData = false
End Set
End Property

Public Property Changed as Boolean
Get
return _changed
End Get
Set
_changed = value
End Set
End Property
End Class

******************************************

This was why I was trying to use object so that I didn't have to use
duplicate code in each of my new classes

Thanks,

Tom


Unfortunately, your not going to do much better in 2003.
 
T

tshad

Tom Shelton said:
Unfortunately, your not going to do much better in 2003.

So Base Class can't access a variable in a derived class, as I assumed.

So the properties have to be in each Class.

Thanks,

Tom
 

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