Solved

Smart inheritence and typecasting

Posted on 2009-04-14
3
171 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now