From Variables to Objects


In this chapter, we've learned a lot about variables, but a term you will probably hear more frequently as a .NET developer is object. If you've heard much about programming before, you've probably heard about objects. You might even have heard that Visual Basic .NET is 'object oriented'. Being 'object oriented' may sound a bit daunting, but using objects is easy – you've already done quite a lot of it.

Very simply, an object is a thing. You can't necessarily see it, but you know its there. People sometimes say "In Visual Basic .NET every thing is an object." Here's what they mean. A textbox is a thing, and so in Visual Basic .NET it is an object. A piece of text is a thing, and so in Visual Basic .NET is an object. A number is a thing, and so in Visual Basic .NET is an object. Every thing in VB.NET is an object of one sort or another.

Look at this line of code:

Dim numberOfChildrenInClass As Integer = 34

Variables, like this one, allow us to give names in our code to things that we need to make use of. In other words, variables are names that we attach to objects. When we create an integer variable like this, we're creating a name for an integer object. If we create a variable called numberOfChildrenInClass, and assign it a value of 34, what we're doing is creating an integer object with the integer value of 34, and giving it the name numberOfChildrenInClass. When we use the variable, we call it by name in order to get at the value that the integer object holds. For these simple objects, our variables only hold a value, for example, an integer, a string, a date, and so on.

Likewise, when we work with a textbox control, we are actually using a textbox object, and referring to it by its name. The ID property is the name for our textbox, so in a lot of our examples, we have textbox objects called Textbox1, Textbox2, etc. as these are the system-generated names when a new textbox object instance is created. It is important for code maintenance that these name are made meaningful, otherwise in a few weeks anyone, including yourself, will have difficulty understanding how the code works.

Note

The technical definition that we'll explore in the next chapter is that we're using an instance of the TextBox class. Creating a new object that we can use in our code means that we are creating a new instance of the object, the definition (or template) for the object is held in a class. Creating an instance of an object is a process known as instantiation.

The TextBox object is a bit more complicated than a simple Integer or String object, as an instance of a TextBox object will contain all of the properties we assign to the TextBox (so, the ID property, the Text property, and many more will all be stored in our instance of the object).

That's as far into object land as we're likely to go for now, but we'll be using some interesting objects in the next chapter, as well as learning more about classes and namespaces, which will help you to better understand what's actually going on when we use objects.




Beginning Dynamic Websites with ASP. NET Web Matrix
Beginning Dynamic Websites: with ASP.NET Web Matrix (Programmer to Programmer)
ISBN: 0764543741
EAN: 2147483647
Year: 2003
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net