• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

Smart inheritence and typecasting

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
Unimatrix_001
Asked:
Unimatrix_001
2 Solutions
 
CodeCruiserCommented:
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
 
Bob LearnedCommented:
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
 
Unimatrix_001Author Commented:
Thank you. :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now