Solved

Smart inheritence and typecasting

Posted on 2009-04-14
3
175 Views
Last Modified: 2013-11-05
Hi,

Assume the code below, in the someSeperateMethod is there a more intelligent way of typecasting to the derived classes without all the if's - someway to automatically typecast using something to do with Enum?

Thanks,
Uni
Public MustInherit Class myBaseClass
    Enum ENUMTYPES
        Type1
        Type2
    End Enum
    Public MustOverride Function getClassType() as ENUMTYPES
End Class
 
Public Class CType1
    Public Overrides Function getClassType() as ENUMTYPES
        getClassType=Type1
    End Function
End Class
 
Public Class CType2
    Public Overrides Function getClassType() as ENUMTYPES
        getClassType=Type2
    End Function
End Class
 
Public Sub someSeperateMethod()
    'Some list of CType1's and CType2's.
    if(myList.Item(1).getClassType()==Type1)then
        'Typecast to CType1
    else
        'Typecast to CType2
    end if
End Sub

Open in new window

0
Comment
Question by:Unimatrix_001
3 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 300 total points
ID: 24146376
I can not think of any way other than if statements (may be because i am sleepy!). But one thing could be suggested. instead of

getClassType=Type1

use

return Type1
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 200 total points
ID: 24148355
It sounds like you need to use a base class with common code, or an interface that defines the contract that each sub-class should implement.
0
 
LVL 3

Author Closing Comment

by:Unimatrix_001
ID: 31570041
Thank you. :)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question