Solved

Smart inheritence and typecasting

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create XML 5 47
code conversion from excel vba to visual basic for visual studio 5 41
Web Form VB.Net  import CSV 4 27
Get Client IP on RDS - VB.NET 15 30
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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

18 Experts available now in Live!

Get 1:1 Help Now