Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. call a Sub with a dynamic type parameter.

call a Sub with a dynamic type parameter.

Scheduled Pinned Locked Moved Visual Basic
questioncsharphelp
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    desanti
    wrote on last edited by
    #1

    Hello ! On vb.net , I have a sub like this : ... myobj.num=1 Myobj.vl=2 ..... I want to pass Myobj as a parameter when I call the sub. But the problem is that I want to call this same sub , one time when myobj is Type1 , and one time when myobj is Type2 ( Both type1 and Type2 are custom types that both have the fields .num and .vl). How can I do this ? Thank you !

    L 2 Replies Last reply
    0
    • D desanti

      Hello ! On vb.net , I have a sub like this : ... myobj.num=1 Myobj.vl=2 ..... I want to pass Myobj as a parameter when I call the sub. But the problem is that I want to call this same sub , one time when myobj is Type1 , and one time when myobj is Type2 ( Both type1 and Type2 are custom types that both have the fields .num and .vl). How can I do this ? Thank you !

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Assuming both types are descended from the same parent class, you can use the TypeOf Operator (Visual Basic) | Microsoft Docs[^] to decide which is which. If they are totally different types then you should use different subs.

      D 1 Reply Last reply
      0
      • D desanti

        Hello ! On vb.net , I have a sub like this : ... myobj.num=1 Myobj.vl=2 ..... I want to pass Myobj as a parameter when I call the sub. But the problem is that I want to call this same sub , one time when myobj is Type1 , and one time when myobj is Type2 ( Both type1 and Type2 are custom types that both have the fields .num and .vl). How can I do this ? Thank you !

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Easy way is to put those details in their own class, and make that new class part of type1 and type2.

        Public Class McBoatFace
            Property Num As Integer
            Property vl As String
        End Class
        Public Class Type1
            Property Boat As McBoatFace
        End Class
        Public Class Type2
            Property Boat As McBoatFace
        End Class
        
        Sub Marine(Boat As McBoatFace)
            ' access to Boat.Num and Boat.vl
        End Sub
        

        A less easy, but cleaner way would be implementing the McBoatface-class as an interface, and to pass the interface to the sub. Another way to make it cleaner is by putting the Boat-property into a new Type0-class, and inherit Type1 and Type2 from it; makes the property appear in both classes.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

        D 1 Reply Last reply
        0
        • L Lost User

          Easy way is to put those details in their own class, and make that new class part of type1 and type2.

          Public Class McBoatFace
              Property Num As Integer
              Property vl As String
          End Class
          Public Class Type1
              Property Boat As McBoatFace
          End Class
          Public Class Type2
              Property Boat As McBoatFace
          End Class
          
          Sub Marine(Boat As McBoatFace)
              ' access to Boat.Num and Boat.vl
          End Sub
          

          A less easy, but cleaner way would be implementing the McBoatface-class as an interface, and to pass the interface to the sub. Another way to make it cleaner is by putting the Boat-property into a new Type0-class, and inherit Type1 and Type2 from it; makes the property appear in both classes.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

          D Offline
          D Offline
          desanti
          wrote on last edited by
          #4

          The types type1 and type2 are totally different types , but both have the fields that I use on the sub.

          R 1 Reply Last reply
          0
          • L Lost User

            Assuming both types are descended from the same parent class, you can use the TypeOf Operator (Visual Basic) | Microsoft Docs[^] to decide which is which. If they are totally different types then you should use different subs.

            D Offline
            D Offline
            desanti
            wrote on last edited by
            #5

            The types type1 and type2 are totally different types , but both have the fields that I use on the sub.

            L D 2 Replies Last reply
            0
            • D desanti

              The types type1 and type2 are totally different types , but both have the fields that I use on the sub.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              In that case I would suggest you should have different subs. Or better still the types should expose their data content as Properties, which you can then set without needing to check which type it is.

              1 Reply Last reply
              0
              • D desanti

                The types type1 and type2 are totally different types , but both have the fields that I use on the sub.

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                Both of your "type1" and "type2" classes have fields in common. Ok, so those have to be broken out into something that is common between those two types, either an Interface implementation or a base class that they both inherit from. You will then be able to pass your "type1" and "type2" class instances as either that interface or base class for the method to use. For example, an interface version would look something like:

                Public Interface SomeInterface
                Property SomeProperty As String
                Property AnotherProperty As Int
                End Interface

                And both of your "types" would have to implement the interface:

                Public Class Type1 Implements SomeInterface
                ...
                End Class

                Public Class Type2 Implements SomeInterface
                ...
                End Class

                When you define your method that has to accept instances of these two classes, you use the interface to define the parameter expected:

                Public Sub MyMethod(ByVal parameter As SomeInterface)
                

                End Sub

                The method is saying "I need something that implements this interface". It's a contract where the method expects the object passed in as "parameter" to implement a know interface, guaranteeing the properties defined in the interface will be there in the object that is passed in. When you go to call this method, you're saying this is an instance of Type1, but since it implements this interface, treat this object as this interface type, not as the Type1 class.

                Dim myInstance As New Type1
                

                ...
                MyMethod(myInstance)

                Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                Dave Kreskowiak

                1 Reply Last reply
                0
                • D desanti

                  The types type1 and type2 are totally different types , but both have the fields that I use on the sub.

                  R Offline
                  R Offline
                  Ralf Meier
                  wrote on last edited by
                  #8

                  I suppose that there is a possible solution - but for this you should give more information about your issue ... Perhaps you write something more about those 2 types and what is common to them. In my opinion each of your types derive from Object and so you should be able to identify each type in your method ... But ... as I wrote before ... more Info required ...

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups