Results 1 to 8 of 8

Thread: Can't pass a Structure to a Sub Procedure???

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Can't pass a Structure to a Sub Procedure???

    I am trying to pass a Public Data Structure to a SubProcedure. Problem is, the SubProcedure signature won't allow me to declare a structure, ex;
    Code:
    Private Sub WriteRecord(ByVal stc as Structure)
    
    End Sub
    It doesn't like "Structure". How can I pass a Data Structure to a SubProcedure?

    Thanks,
    Blake

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Can't pass a Structure to a Sub Procedure???

    You have to specify the type of the argument in a method declaration and Structure is not a type. Would you ever try to do this:
    vb Code:
    1. Private Sub WriteRecord(ByVal cls as Class)
    2.  
    3. End Sub
    I hope not.

    If you really want a method that can accept a parameter that is a structure of any type then you can do this:
    vb Code:
    1. Private Sub WriteRecord(ByVal stc as ValueType)
    because all structures inherit the ValueType class. If what you actually want is a method that can take a parameter of the type of a structure that you have declared then it's that type you must specify. For instance, if you have declared a structure like this:
    vb Code:
    1. Public Structure MyType
    2.     '...
    3. End Structure
    then you method would be:
    vb Code:
    1. Private Sub WriteRecord(ByVal stc as MyType)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Can't pass a Structure to a Sub Procedure???

    Well whats the error? You shouldnt name your structure "structure".
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Can't pass a Structure to a Sub Procedure???

    I was using "Structure" as an illustration!

    JMC...I have several Structures defined but I want to create a Generic Sub that will accept all the defined structures...
    Blake

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Can't pass a Structure to a Sub Procedure???

    Quote Originally Posted by blakemckenna
    ...I was using "Structure" as an illustration! ...
    Shouldve said so in the first place
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Can't pass a Structure to a Sub Procedure???

    Quote Originally Posted by blakemckenna
    I was using "Structure" as an illustration!

    JMC...I have several Structures defined but I want to create a Generic Sub that will accept all the defined structures...
    You can do as I have already suggested and declare the argument as type ValueType, but then the argument will accept any value type. I don't really see how that benefits you though. These types cannot have common functionality because they cannot inherit a common type (other than ValueType). What do you get by providing a single method for all types?

    You could also overload your method, providing one signature for each type. That is probably a more reasonable path because then you can call the same method regardless of the type and the system will choose the correct overload. Because each overload is specific to a type you can provide specific functionality. If you had a single method signature you'd only have to test types and cast within anyway, which doesn't make any sense.

    A further option is to declare an Interface and have all your structures implement it. Then you can declare your argument as that Interface type. That would also be a more sensible option if your structures actually do have common functionality.

    Finally, you could declare your types as classes instead of structures and have them all inherit a common base class.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Can't pass a Structure to a Sub Procedure???

    They don't make is easy do they?
    Blake

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Can't pass a Structure to a Sub Procedure???

    Yes they do. What you are asking for doesn't actually make sense. Creating a single method to handle multiple types that don't share functionality is poor design. The only way a single method makes sense is if the types share functionality. For that to be the case they would already have to either implement a common interface or inherit a common type. Those are not workarounds for a problem you've encountered. They're things that you should have already done to make what you're now asking for reasonable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width