They say you learn something new every day.

Posts tagged ‘objects’

A Touch of Class (17/02/2012)

I think I suddenly understood classes and objects today.

I get this quite a bit with coding concepts. Or maybe concepts in general. I leave them mulling over in my head, and then suddenly, one day, I get them,

I think no one has ever really explained objects very well, and I think a key part of that is that I never got when I would use one. I think the fact that you don’t really need them makes it even harder.

But classes are basically like multiple functions in one. Here’s a demo one I built to explain them to myself:

Class TVProgram

Public StartTime
Public Test
Public ProgramTitle

Public Property Get ProgramDate
ProgramDate = Day(Test) & ” ” & MonthName(Month(Test)) & ” ” & Year(Test)
End Property

End Class

Set objTVShow = New TVProgram
objTVShow.StartTime = CDate(“17:30”)
objTVShow.Test = DateSerial(1999,9,17)
objTVShow.ProgramTitle = “TV Show”

wscript.echo objTVShow.ProgramDate & ” – ” & objTVShow.Test

So the first few lines:

Public StartTime
Public Test
Public ProgramTitle

Are like the Function variables. You assign values to these.

Public Property Get ProgramDate

Is like the Function output. You can have several of these in one class, and this is the real value to them and the thing I don’t think I got until now: classes are like functions with lots of outputs. This means you can define different outputs with the same data, or different parts of output for the same idea.

It gives me a nice warm feeling when I suddenly understand something like this. And it makes me wonder why I never understood it before. However, I think part of this comes from the fact no one ever really says what classes actually are:

You can use classes to describe complex data structures. For example, if your application tracks customers and orders, you can define two classes for them, each with a unique set of internal data (typically called properties) and functions (typically called methods). You can then manage customers and orders as if they were native VBScript subtypes. More important, because you assign a class its properties and methods (i.e., its programming interface), you have an object-oriented tool to improve VBScript applications.

Does that really help in any way? Or does that leave you even more confused by what they’re actually talking about?

Tag Cloud