Using Select Case in Constructor of a Class

  • Thread starter Thread starter Paul Bromley
  • Start date Start date
P

Paul Bromley

I have just started to use Classes a lot in my code. Often I find that I
need to use the same contstructor, but wish to invoke a different action
depending on where I am clling the class from. I hav therefors started
sending an extra parameter to initialise the Class that I tend to call
sArea. I then use this in my code with a series of Select Statements in the
constructor, so that I can use the same constructor for parameters, but
carry out different actions depending on the case statement.

Is this an acceptable way of doing this or should I be looking at doing
something different??

Best wishes

Paul Bromley
 
* "Paul Bromley said:
I have just started to use Classes a lot in my code. Often I find that I
need to use the same contstructor, but wish to invoke a different action
depending on where I am clling the class from. I hav therefors started
sending an extra parameter to initialise the Class that I tend to call
sArea. I then use this in my code with a series of Select Statements in the
constructor, so that I can use the same constructor for parameters, but
carry out different actions depending on the case statement.

I am not sure if I understand your problem. You can implement the
constructor with the 'Select Case' in a base class and then call the
base class' ctor from within inherited classes:

\\\
Public Class TheBaseClass
Public Sub New(ByVal Number As Integer)
Select Case Number
...
End Select
End Sub
End Class

Public Class Foo
Inherits TheBaseClass

Public Sub New(ByVal Number As Integer)
MyBase.New(Number)
End Sub
End Class
///
 
Paul,
In addition to Herfried's response.

Should you be considering different classes?

Public Class BaseClass

Public Sub New()
' common construction
End Sub

End Class

Public Class Case1Class
Inherits BaseClass

Public Sub New()
MyBase.New()
' Case 1 specific construction
End Sub

End Class

Public Class Case2Class
Inherits BaseClass

Public Sub New()
MyBase.New()
' Case 2 specific construction
End Sub

End Class

Public Class Case1Class
Inherits BaseClass

Public Sub New()
MyBase.New()
' Case 3 specific construction
End Sub

End Class

With creating derived classes you can have methods & fields that are
specific to each derived class.

Hope this helps
Jay
 
Hi Jay,

I suppose that I should.

Thanks for your repsonse & that of Herfried

Best wishes

Paul Bromley
 
Back
Top