Android Question Dependent Dim Declaration

Discussion in 'Android Questions' started by DawningTruth, Apr 24, 2019.

  1. DawningTruth

    DawningTruth Active Member Licensed User

    I have a scenario where I need to Dim a variable to 2 different classes dependent on an option. When I do this, it gives an error saying that myItem is already declared when option = 2. It should not do this as option 1 is never executed. There is clearly a gap in my understanding. Any suggestions?

    Code:
    If option = 1 Then
            
    Dim myItem As Class1
    Else if option = 2 Then
            
    Dim myItem As Class2
    End If
     
  2. Sandman

    Sandman Well-Known Member Licensed User

    DawningTruth and Cableguy like this.
  3. techknight

    techknight Well-Known Member Licensed User

    Welcome to the world of statically typed languages.
     
    DawningTruth likes this.
  4. Erel

    Erel Administrator Staff Member Licensed User

    You can create a factory method:
    Code:
    Sub CreateObject(Option As Int) As Object
     
    Select Option
      
    Case 1
       
    Dim square1 As Square
       square1.Initialize(
    20)
       
    Return square1
     
    Case 2
      
    Dim rectangle1 As Rectangle
      rectangle1.Initialize(
    2040)
      
    Return rectangle1
     
    End Select
    End Sub

    'usage
    Dim shape As Object = CreateObject(option)
    CallSub(shape, "draw")
     
  5. DawningTruth

    DawningTruth Active Member Licensed User

    Thx Erel, I will see how my sub can be adapted to this approach.
     
  6. MarkusR

    MarkusR Well-Known Member Licensed User

    DawningTruth likes this.
  7. DawningTruth

    DawningTruth Active Member Licensed User

  8. advansis

    advansis Member Licensed User

    Why not declare the variable as object and let the called-function to choose the right behaviour? In this case you will use the select-case statement, after the calling and not before (as Erel suggests). The magic word to distinguish between classes is "is"
     
    DawningTruth likes this.
  9. MarkusR

    MarkusR Well-Known Member Licensed User

    that i meant with a third class, as object u not see the methods.
     
    DawningTruth likes this.
  10. DawningTruth

    DawningTruth Active Member Licensed User

    Advansis, please share a code stub, so I can understand what you mean. My current approach was to pass the object to the Sub, and then use it in what ever form is required in the sub. But that is not working for the reason above.
     
  11. advansis

    advansis Member Licensed User

    The skeleton could be this...

    First approach: each class is independent
    Code:
    Sub CreateObject (option As Int)
        
    Dim X As Object
       
    If option = 1 Then
           
    Dim M1 As Class1
           M1.initialize
           X=M1
       
    Else if option = 2 Then
           
    Dim M2 As Class2
           M2.initialize
           X=M2
       
    Else
           
    Return
       
    End If
       UseTheObject(X)
    End Sub


    ' The dispatcher

    Sub UseTheObject (X As Object)
       
    If X Is Class1 Then
           
    Dim M1 As Class1
           M1=X          
           
    ' Use of M1
       else if X Is Class2 Then
           
    Dim M2 As Class1
           M2=X
           
    ' Use of M2
       Else
           
    Return
       
    End If
    End Sub
    Second approach: using something like polimorphism:

    Code:
    ' The object creation

    Sub CreateObject (option As Int)
        
    Dim X As Object
        
    If option = 1 Then
            
    Dim M1 As Class1
            M1.initialize
            X=M1
        
    Else if option = 2 Then
            
    Dim M2 As Class2
            M2.initialize
            X=M2
        
    Else
            
    Return
        
    End If
       
        UseTheObject(X)      
    End Sub


    ' The dispatcher

    Sub UseTheObject (X As Object)
        
    ' Calling some class methods...
        If SubExists(X,"Foo1"Then CallSub(X,"Foo1")
        
    If SubExists(X,"Foo2"Then CallSub2(X,"Foo2","xyz")
        
    If SubExists(X,"Foo3"Then CallSub3(X,"Foo3","abc","123")

        
    ' Calling with multiple parameters
        Dim Args As Map=CreateMap("Param1":1,"Param2","xyz","Param3","abc123")
        
    If SubExists(X,"FooM"Then CallSub2(X,"FooM",Args)
        
    Dim FooRes As String=Args.GetDefault("RetParam","")
           
    End Sub
     
    DawningTruth likes this.
  12. DawningTruth

    DawningTruth Active Member Licensed User

    Thx, I will study in more depth.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice