1

Consider I have some models as follow: black, green, blue, red.

I have a function which receive an object and I would like to trycast the object to appropriate model.

Now I have the code like this:

Public Sub SetData(ByVal obj As Object)
    If TryCast(obj, black) IsNot Nothing Then
        Dim m As Black = TryCast(obj, black)
        ......
    ElseIf TryCast(obj, green) IsNot Nothing Then
        Dim m As green = TryCast(obj, green)
        ......
    ElseIf TryCast(obj, blue) IsNot Nothing Then
        Dim m As blue = TryCast(obj, blue)
        ......
    ElseIf TryCast(obj, red) IsNot Nothing Then
        Dim m As red = TryCast(obj, red)
        ......
    Else
        ......
    End If
End Sub

It seems the code is not efficient and consume more memory and use more CPU for its double trycast. How to simplify this code such as checking trycast but get the result model at the same time without double trycast process?

Buzz
  • 321
  • 2
  • 3
  • 20
  • Ideally, all of your models should inherit from the same base and have a [`MustOverride`](https://msdn.microsoft.com/en-us/library/hyb29zk8.aspx) (or, at least, `Overridable`) method that you could call that would then apply the correct logic - rather than making this method try to work out what type of object its dealing with, and then having type-specific code inside as well. – Damien_The_Unbeliever May 20 '15 at 06:58
  • Yeah...good point..I think I need to think the design again.... – Buzz May 20 '15 at 08:53

2 Answers2

1

Well I would say that there is no point in TryCast and then TryCasting again so this would be more efficient:

Public Sub SetData(ByVal obj As Object)

    Dim blk As black = TryCast(obj, black)
    Dim grn As green = TryCast(obj, green)

    If blk IsNot Nothing Then

    ElseIf grn IsNot Nothing Then
        ......
    ElseIf 

    End If
End Sub

But I suspect you should be using a base class or interface here and then you wouldn't need to cast it at all.

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • Matt, Your point about the use of an interface in this sort of situation is as aspect of programming that still throws me.. although I know you're correct..could I be cheeky and ask you to expand your answer with an illustration applying that. – Dom Sinclair May 20 '15 at 07:15
1

You can use TypeOf so as to know what Class is your object. Example

If TypeOf obj Is black Then
    Dim m As Black = TryCast(obj, black)
ElseIf TypeOf obj Is green Then
    Dim m As green = TryCast(obj, green)
ElseIf TypeOf obj Is blue Then
    Dim m As blue = TryCast(obj, blue)
ElseIf TypeOf obj Is red Then
    Dim m As red = TryCast(obj, red)
    ......
Else
    ......
End If
  • This should be better..! Thanks. – Buzz May 20 '15 at 08:54
  • Shouldn't it performs better with DirectCast instead of TryCast when you already know it's a matching type (you can DirectCast from ChildType to BaseType) See this question : http://stackoverflow.com/a/385727/2410892 – Karl Stephen Sep 22 '15 at 07:14