Passing Structures as parameters in vb.net
Posted on 2004-08-08
Sometimes when programs become sufficiently large, I like to group my variables in Structures. I consider this a good practice, some of my peers find it odd.
On a code review, I was challanged with the following questions, which I am unsure of:
Here's a synopsis:
Private Function myVeryLongFunctionName() As myStruct
myVeryLongFunctionName.str1 = "123"
Private Function myFunction() As myStruct
dim myShortStruct as myStruct
myShortStructName.str1 = "123"
- When a Function is populating and returning a structure, is there a performance penalty to Dim a local variable 'myLocalStruct' of type "myStruct" in the function and then use the Return statement to "Return myLocalStruct" at the end of the procedure, or, should one simply stick with using the Function name (with an 'AS myStruct' at the end of the function definition) and reference the structure members via 'myVeryLongFunctionName.myStructureMemeber' syntax throughout the function? My function name is quite long, and thus it produces a bit more readable code to create a 'short name' and return it. However the strucuture is somewhat complex (containing XML Dom objects, other object pointers, and a variety of simple data types) and if there's overhead in the 'Return myLocalStructure' syntax, I'll forgo the added clarity of the local structure. Bluring the picture a bit is my lack of understanding exactly when VB.net is passing pointers around (when copying or returning structures) and when it actually copies the contents of variables.
- Bonus: I wanted to see the values in my structure, so I created a function to HTML format their values. When I did so, I passed the structure byVal into this function. Everywhere else in the code had been byRef. Suddenly, when debugging the program (a DLL business object for a web program), all the values in the "Locals" box where showing up as "error: cannot obtain value". As far as I know, this was the case anywhere in my code, not while stepping into the newly creating function with the ByVal structure parameters. Can anyone explain why "error: cannot obtain value" was showing up?
This is my first post. I intended to be generous with the points as I believe an answer that truely nails this one may prove challanging.