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,
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:
Private Sub WriteRecord(ByVal cls as Class)
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:
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:
Public Structure MyType
'...
End Structure
then you method would be:
vb Code:
Private Sub WriteRecord(ByVal stc as MyType)
Re: Can't pass a Structure to a Sub Procedure???
Well whats the error? You shouldnt name your structure "structure".
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...
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:rolleyes:
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.
Re: Can't pass a Structure to a Sub Procedure???
They don't make is easy do they?
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.