Thought for the Dazed

I've had to give up that Distance Learning course as I was having trouble seeing the teacher.

Flickr
www.flickr.com
RobMiles' items Go to RobMiles' photostream
Twitter
C# Yellow Book

Search entire site
« Cameras and Lumps | Main | When is a number not a number? »
Friday
Feb032012

Tutorials, Objects and References

Snowy Library.jpg

We are doing objects and references in the First Year tutorial today. Great fun. Well, at least I thought so. I asked the class whether big objects in memory have larger references than small ones. They don’t – the size of the tag is always the same – no matter what it is connected to. I went on to explain that a reference tag contains a bunch of information about the thing it is referring to, including the type that it has, for example string, BankAccount, AlienSprite or whatever class you have created.

Then someone asked a great question: “What happens if the type has a very long name? Does the reference tag get bigger?”. Aha! What a great question. The answer is no. This is because the type of a reference is managed in terms of a reference from the tag to the type object that describes that reference. In other words, a reference to a BankAccount will also contain a reference to an object that describes the BankAccount class.

I was very pleased with this question, because it let me start to explain how, by using objects, you can build up structures of data that are genuinely useful. I’ve been explaining structures and objects all week and several times I’ve had the sensible question “What’s the point of objects and references? They just seem to make life harder for us.”  This little allows me to show how easy it is to use references to allow the system running the program to track and mange the type of the objects it is using.

I reckon that a good tutorial is when the students learn something. A great tutorial is when the tutor learns something as well. I’ve now got a lovely new example to use next year…

Reader Comments (2)

Did you also cover strong and weak references? I think these are a very important distinction, especially when discussing Garbage Collection. The .net framework has the WeakReference class, but I find many .Net developer's have never heard of it. I do a lot of Silverlght programming usng MVVM and I was shocked to see that it was causing a memory leak. My viewmodels were subscribing to events in my model and never unsubscribing, so were never being dereferenced and garbage collected. I notice that Objective-C has the notion of strong and weak references built into the language.
February 7, 2012 | Unregistered CommenterMartin Randall
No we don't mention the distinction at this point. At the moment we are more concerned with making sure that the difference between value and reference types is made clear. We do that kind of thing a little bit further down the tracks.

Very good thought though, I'll put some more slides in for week 9.....
February 7, 2012 | Registered CommenterRob

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.