Solved

Smart inheritence and typecasting

Posted on 2009-04-14
3
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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