David Seruyange's Radio Weblog
Tidbits for developers and the interested...

David-ism
Watu
Vicariously
Photo Blogs
Form, Function
Write, Think
Web People
Coders
Feel Good


Subscribe to "David Seruyange's Radio Weblog" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

Home (all entries)  | Technie  | Prattle (personal stuff)  | Books  | Snippets  | WhiteBox


Monday, October 14, 2002
 

Finally back on the blog after spending a week at a company whose no less than draconian internet policy was to lock employees out of all internet mail based sites (hotmail, yahoo, etc...) and whose proxy locked me out of a lot of the software I use.

Last week was spent teaching ASP.NET to developers in Simi Valley.  Not too much interesting stuff to report there.

Today I encountered strange behavior in VB.NET.  Assinine behavior, IMHO but I'll just say strange to keep other people happy.  Here is a brief overview:

Object Oriented languages support classes as a method of creating "templates" for things one might model in an application.  For example, in an application dealing with simple mathematical calculations I may create a class called Circle like this:

public class Circle{

}

Every circle has a radius.  We can therefore define a radius as instance data related to classes; each instance of the class circle has it's own measure of radius.  That means if I drew a circle in the sand, it would have its own radius.  If my friend Karen drew another circle in the sand, it too would have its own radius.  Even if the two radiuses were identical in value, they would each belong to their own instance of the circle.  In programming we do that like this:

public class Circle{

public double radius;

}

Fair enough.  We might even add a constructor, an initialization method for the class circle that would take a measure of a radius:

public class Circle{

public double radius;

public Circle(double r){
radius = r;
}

}

So it's easy to conceive that each instance of a circle has its own radius but there is also another type of data a class may have, static data.  This is data that is shared by every instance of the class. 

To make this easy to grasp, just think back to circles.  Is there anything related to circles that is always the same, no matter what circle one references?

[dramatic pause]

How about PI?  PI is a value needed by all circles to calculate circumference and area but it is a value that does not change with each instance of the circle.  To create a static member, simply write:

public class Circle{

public static double PI = 3.1415927353;

public double radius;

public Circle(double r){

radius = r;

}

}

When referencing static data, since it belongs to the class, not the instance, you can just type:

x = Circle.PI

It makes sense that you couldn't use an instance to refer to it because an instance doesn't own the value PI, it belongs to the class:

Circle karensCircle = new Circle(4.2);
x = karensCircle.PI; // wrong, will give us an error.

So here is the point of my contention: in VB.NET you can refer to static data via an instance of the class.   Here is how I found out:

dim x as String = new DateTime().Now().ToString()  'this works in VB.NET

I had been writing the above for a while and needed to do the same thing in C# but kept getting errors.

Wierd, but definitely worth noting for the future.

 


6:13:11 PM    comment []


Click here to visit the Radio UserLand website. © Copyright 2006 David Seruyange.
Last update: 5/23/2006; 8:20:01 PM.
October 2002
Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
Sep   Nov