[Webinar] Streamline your web hosting managementRegister Today


Passing Structures as parameters in vb.net

Posted on 2004-08-08
Medium Priority
Last Modified: 2008-03-10

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"
end function


Private Function myFunction() As myStruct
  dim myShortStruct as myStruct
  myShortStructName.str1 = "123"
  return myShortStructName
end function

- 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.
Question by:Yelnoc
  • 3
  • 2
LVL 37

Expert Comment

ID: 11748218

ok a few things ...

1) why create a struct instead of a class ? its a reference type so when you pass it byval it won't need to be copied ...
2) Using a class/struct as a return generally is a great idea !

I believe your error is due to the fact that it would actually need to be expanded i.e. it doesn't have a true "value" but is instead a container, what do you get when you place it in the watch window ?
LVL 27

Accepted Solution

Dabas earned 1000 total points
ID: 11748402
Hi Yelnoc:
I would have agreed with Greg's remark about structures vs classes, but just yesterday I stumbled on the following paragraph in Rockford Lhotka's "Expert On on On - Visual Basic .NET Business Objects" (recommended reading!)

"The reason why Rule is a Structure instead of a Class is down to memory management. As our application runs, we can expect that business rules will be constantly broken and 'un-broken', resulting in the creation and destruction of Rule entities each time. There's a higher cost to creating and destroying an object that there is for a Structure, so we're minimizing this effect as far as we can"

Now do notice here that the author is talking about a process where he KNOWS there is going to be substantial activity of creation and destruction. I would assume you need hundreds if not thousands of these for the "minimizing effect" that he is talking about to be noticeable. In a situation where the passing of structures occurs once or twice at a time, I would not bother, and would use classes. You never know when you would want to encapsulate the data in myStruct with its own functionality!


Author Comment

ID: 11749371
>> 1) why create a struct instead of a class ?

Unfortunately, I could not dig up an article that demonstrated UDT's superior performance over classes in VB6.  Got me hooked on UDTs, and then saved me when it came to in-house discussions on UDTs.  In my experience, UDTs/Structures often seem to be "guilty until proven innocient".

So, I dug up this on Structures: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchUseClassStruct.asp

Good stuff. Answered many pieces of my question(s).  Here's a snip:

A structure can be preferable when:

1)You have a small amount of data and simply want the equivalent of the UDT (user-defined type) of previous versions of Visual Basic
2)You perform a large number of operations on each instance and would incur performance degradation with heap management
3)You have no need to inherit the structure or to specialize functionality among its instances
4)You do not box and unbox the structure
5)You are passing blittable data across a managed/unmanaged boundary

Item 3 says it all for me.

What I was getting at on the return value is this...  You set the return value of a function as a Structure.  If you then dim a variable to the structure type and return it (instead of using the function value itself) you've done yourself a disservice. The values or pointers from your DIMmed structure variable are copied into the return.

I'm still fairly concerned about  the "error: cannot obtain value".  What sin has one commited when passing a structure byVal to cause such pain?

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 37

Expert Comment

ID: 11749388
as for #3 using sealed classes (not overridable) provides nearly identical functionality.

passing a structure byval causes the structure to be box/unboxed which violates #4 ...

what does your struct look like ?

in this code I can view members of the struct in the locals window...

Module Module1

    Structure test
        Public i As Integer
        Public j As Integer
    End Structure
    Sub bar(ByVal i As test)
    End Sub

    Sub Main()
        Dim foo As test
        foo.i = 12
        foo.j = 1
    End Sub

End Module

Author Comment

ID: 11749584
From http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchUseClassStruct.asp

Boxed Value Types
If you define a value type but subsequently treat it as an object, it must be boxed, or converted to type Object. The common language runtime boxes a value type instance by making a copy of it, embedding it in a newly allocated object, and storing pointers to its type information in the metadata.

You treat a structure as an object if you assign it to an Object variable or pass it to a procedure that takes an Object argument. If you do this, the common language runtime must box it and often unbox it, as well as manage it on the heap. This kind of treatment can make a structure considerably less efficient than if you had created it as a class in the first place


"passing a structure byval causes the structure to be box/unboxed which violates #4" ????  I bet you're right, but I'm not seeing it based on what I read above.  

I could not reproduce with a quick code sample. Sending actual code would take some work as it is so dependent on abstracted XML objects and other interal framework code.
LVL 37

Assisted Solution

gregoryyoung earned 1000 total points
ID: 11749604
sorry I typed too quickly the struct is COPIED to pass byval ... this can be an expensive operation.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Get the source code for a fully functional Access application shell with several popular security features that Access VBA application developers desire, but find difficult or impossible to figure out how to code. You get the source code for managi…
Suggested Courses

608 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question