CODECUBE VENTURES

Reference Type Folly

For the first time, I've had to deal with a bug caused by the whole reference type thing in .NET

For those of you who don't know, any class in .NET is going to be a reference type. Conversely, a struct is going to be a value type. This is apparently a big misconception as most of the explanations I hear when the question, "What's the difference between struct and class" is asked fall along the lines of: "structs default to public visibility", or "structs are allocated on the heap" or some other such nonsense.

In reality, the big thing you have to be aware of is that when you pass a class into another procedure, you're passing it ByRef, so anything you do to it in there will be reflected in the object once the procedure returns control to the calling method. I came across this because some records were being overwritten while they were in the ASP.NET cache. It was driving me batty until the whole reference type thing hit me. The reason the bug was happening is a bit too complex to get into here, but suffice to say that it's because the same object was being reused over and over in a loop, loading data, getting added to cache, then loading the next record's data. so at the end of the loop, everyone was the same!

Oh well ... you live and learn ... won't be happening to me again :-D

Latest post: Digging Up the First Version of CodeCube

See more in the archives