EDN Admin
Well-known member
Hi, I have an inheritance question. I am cleaning up some code and I have multiple classes that are 99% exactly the same.
So my understanding is that my options are...
1. Create a class and all other classes inherit from it and modify it a bit
2. Create an abstract class and all classes inherit from it and modify a bit ** I took this route
3. Create an interface and all classes Implement that interface.
Here is my original class below (the one that all of them look 99% identical). This class is used in an arraylist so I have a listing of all my images from a folder I have searched.
=================class start
Public Class DesignImagesPics<br/>
Private _FileName As String<br/>
Private _FullName As String<br/>
Private _HttpName As String
Public ReadOnly Property FileName() As String<br/>
Get<br/>
Return _FileName<br/>
End Get<br/>
End Property
Public ReadOnly Property Fullname() As String<br/>
Get<br/>
Return _FullName<br/>
End Get<br/>
End Property
Public ReadOnly Property HttpName() As String<br/>
Get<br/>
Return _HttpName<br/>
End Get<br/>
End Property
Public Sub New(ByVal FileName As String, ByVal FullName As String)<br/>
_FileName = FileName<br/>
_FullName = FullName<br/>
If WebCFGParams.LocalTesting Then<br/>
_HttpName = SessionHandler.GetUrlWebPath & "Images/Designs/" & FileName ****** This is what I am trying to fix (read below)<br/>
Else<br/>
_HttpName = " http://www.bla.com/Images/Designs/ http://www.bla.com/Images/Designs/ " & FileName
****** This is what I am trying to fix (read below)<br/>
End If<br/>
End Sub<br/>
End Class
=================class end
If you look at the above code that has been marked, both use a folder called "Images/Designs/"
That is the only thing that changes between all the classes that are the same.
My original thought was to put a variable in the top of the class as follows:
Private MustOverride _PartialPath As String
and then in the Sub New class just change the line so it reads...
_HttpName = " http://www.bla.com/ http://www.bla.com/ " & _PartialPath & FileName
However I cannot do this as the variable itself cannot be set to be overriden.
<br/>
So what are my options here. Its really just the path of the images that changes each time between each classes. The whole structure of the class remains the same.
Is my only solution to make a function to return the path and to make it mustoverride ?
Below is my only solution as so far: (that has 1 issue I do not like)
========== new code of my new abstract class
Public MustInherit Class ImagesPics<br/>
Private _FileName As String<br/>
Private _FullName As String<br/>
Private _HttpName As String
Public ReadOnly Property FileName() As String<br/>
Get<br/>
Return _FileName<br/>
End Get<br/>
End Property
Public ReadOnly Property Fullname() As String<br/>
Get<br/>
Return _FullName<br/>
End Get<br/>
End Property
Public ReadOnly Property HttpName() As String<br/>
Get<br/>
Return _HttpName<br/>
End Get<br/>
End Property
MustOverride Function GetPartialPath() As String
Public Sub New(ByVal FileName As String, ByVal FullName As String)<br/>
_FileName = FileName<br/>
_FullName = FullName
If WebCFGParams.LocalTesting Then<br/>
_HttpName = SessionHandler.GetUrlWebPath & GetPartialPath() & FileName<br/>
Else<br/>
_HttpName = " http://www.bla.com/ http://www.bla.com/ " & GetPartialPath() & FileName<br/>
End If<br/>
End Sub<br/>
End Class
Public Class DesignsImages<br/>
Inherits ImagesPics
Overrides Function GetPartialPath() As String ***** here is my new issue<br/>
Return "Images/Designs/"<br/>
End Function
Public Sub New(ByVal FileName As String, ByVal fullname As String)<br/>
MyBase.new(FileName, fullname)<br/>
End Sub<br/>
End Class
========== end new code class
How do I make Overrides Function GetPartialPath() As String a private function. I do not want this accessible outside of the class. Currently it is.
Later when I will need to make the path a public property, I will make a new property return the function.
I am hoping I have the right concept here to clean up all these classes into little classes all inherting from the ImagesPics abstract class.
<br/>
Thank you for your time
<br/>
Miro
<br/>
View the full article
So my understanding is that my options are...
1. Create a class and all other classes inherit from it and modify it a bit
2. Create an abstract class and all classes inherit from it and modify a bit ** I took this route
3. Create an interface and all classes Implement that interface.
Here is my original class below (the one that all of them look 99% identical). This class is used in an arraylist so I have a listing of all my images from a folder I have searched.
=================class start
Public Class DesignImagesPics<br/>
Private _FileName As String<br/>
Private _FullName As String<br/>
Private _HttpName As String
Public ReadOnly Property FileName() As String<br/>
Get<br/>
Return _FileName<br/>
End Get<br/>
End Property
Public ReadOnly Property Fullname() As String<br/>
Get<br/>
Return _FullName<br/>
End Get<br/>
End Property
Public ReadOnly Property HttpName() As String<br/>
Get<br/>
Return _HttpName<br/>
End Get<br/>
End Property
Public Sub New(ByVal FileName As String, ByVal FullName As String)<br/>
_FileName = FileName<br/>
_FullName = FullName<br/>
If WebCFGParams.LocalTesting Then<br/>
_HttpName = SessionHandler.GetUrlWebPath & "Images/Designs/" & FileName ****** This is what I am trying to fix (read below)<br/>
Else<br/>
_HttpName = " http://www.bla.com/Images/Designs/ http://www.bla.com/Images/Designs/ " & FileName
****** This is what I am trying to fix (read below)<br/>
End If<br/>
End Sub<br/>
End Class
=================class end
If you look at the above code that has been marked, both use a folder called "Images/Designs/"
That is the only thing that changes between all the classes that are the same.
My original thought was to put a variable in the top of the class as follows:
Private MustOverride _PartialPath As String
and then in the Sub New class just change the line so it reads...
_HttpName = " http://www.bla.com/ http://www.bla.com/ " & _PartialPath & FileName
However I cannot do this as the variable itself cannot be set to be overriden.
<br/>
So what are my options here. Its really just the path of the images that changes each time between each classes. The whole structure of the class remains the same.
Is my only solution to make a function to return the path and to make it mustoverride ?
Below is my only solution as so far: (that has 1 issue I do not like)
========== new code of my new abstract class
Public MustInherit Class ImagesPics<br/>
Private _FileName As String<br/>
Private _FullName As String<br/>
Private _HttpName As String
Public ReadOnly Property FileName() As String<br/>
Get<br/>
Return _FileName<br/>
End Get<br/>
End Property
Public ReadOnly Property Fullname() As String<br/>
Get<br/>
Return _FullName<br/>
End Get<br/>
End Property
Public ReadOnly Property HttpName() As String<br/>
Get<br/>
Return _HttpName<br/>
End Get<br/>
End Property
MustOverride Function GetPartialPath() As String
Public Sub New(ByVal FileName As String, ByVal FullName As String)<br/>
_FileName = FileName<br/>
_FullName = FullName
If WebCFGParams.LocalTesting Then<br/>
_HttpName = SessionHandler.GetUrlWebPath & GetPartialPath() & FileName<br/>
Else<br/>
_HttpName = " http://www.bla.com/ http://www.bla.com/ " & GetPartialPath() & FileName<br/>
End If<br/>
End Sub<br/>
End Class
Public Class DesignsImages<br/>
Inherits ImagesPics
Overrides Function GetPartialPath() As String ***** here is my new issue<br/>
Return "Images/Designs/"<br/>
End Function
Public Sub New(ByVal FileName As String, ByVal fullname As String)<br/>
MyBase.new(FileName, fullname)<br/>
End Sub<br/>
End Class
========== end new code class
How do I make Overrides Function GetPartialPath() As String a private function. I do not want this accessible outside of the class. Currently it is.
Later when I will need to make the path a public property, I will make a new property return the function.
I am hoping I have the right concept here to clean up all these classes into little classes all inherting from the ImagesPics abstract class.
<br/>
Thank you for your time
<br/>
Miro
<br/>
View the full article