Custom Classes question (again)

ARC

Well-known member
Joined
Sep 9, 2006
Messages
63
Location
Minnesota
Programming Experience
Beginner
Alright so i've been trying to understand classes, and how to use them recently... and i still have some trouble with understanding the concept of using the objects that you add/create using the class.

My understanding as of now, is that you create a Class and create it's Properties -- each of which must have a Get/Set code in order for anything outside of the class file to access the values of said Properties. At this point, you dont have an object or instance yet. You just have the format in which future objects that are created using this class file must constrain to.

Then, you create an object/instance and set it's Properties... then you can use that object as a representation of the Class.

VB.NET:
Public Class Widget

    Private _ID As Integer

    Public Property ID() As Integer
        Get
            Return Me._ID
        End Get

        Set(ByVal value As Integer)
            Me._ID = value
        End Set

    End Property

End Class

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click

        Dim myWidget As New Widget
        myWidget.ID = 1

        Me.Label1.Text = myWidget.ID

    End Sub

Am I to understand then, that this object, myWidget, is now an available object with the ID Property of 1, and I can do with it as I wish? Like for example, stick it in an array with other Widget Objects? Or maybe use the array to hold newly created Widgets at run time when a button OnClick is fired?

I'm not sure if that's even the correct usage of a class object, but that's why I'm asking :) Thanks in advance.
 
When you need to create a user defined type (a template so to speak) you only need to choose between Class and Structure. In short structures are commonly used to keep a stack of simple values, classes for more complex types that may implement inheritance and faster moving around in memory between other classes. Both can define type methods and implement interfaces. See also Structures and Classes in help.
 
Let's say that you are a designer for a car manufacturer. You create this fantastic design for a new car with diagrams and specifications describing every aspect of what features this car has a what it can do. Can you get in that design and drive it? Of course not. The design is just a template or description of what actual cars of that type have and do. You have just defined a class of Car. On the production line, cars are built to that specification. Each one of those cars is an instance of that class. Each one is stamped with a chassis number. They just had their ChassisNumber property set. Some may be sprayed blue while others may be sprayed red. They just had their Color property set. Are you starting to get the idea that OOP might be something like the real world?
 
Ok that makes a little more sense in terms of when to use Classes or Structures. One of my biggest confusions is whether or not Classes 'should' be used if you plan on making lots of instances of said class at the same time.

When you create a new instance of your class, how do you tell it apart from other instances you've already created, and is that appropriate use of a Class object?

VB.NET:
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim userInput As String = InputBox("Enter New Widget ID #", "Create Widget")
        Dim NewWidget As New Widget

        NewWidget.ID = CInt(userInput)

        Me.Label1.Text = NewWidget.ID

    End Sub

This creates an object of the Class in the NewWidget variable correct? But then this doesnt make sense in the onclick event right? As wont this be saving the NewWidget to the same variable every time the user clicks on the button? Is there a 'key' of some sorts as in structures? or perhaps just a different assigning method to organize a group of class objects.
 
You're correct that doesn't makes sense, because you are creating a new instance for no purpose and that instance is destroyed when the sub method is finished.
 
If you want to be able to refer to an object, i.e. an instance of a class, then you need to keep a reference to it. A reference is basically a variable that contains its memory address, which is how reference type objects, which includes class instances, are tracked.

To maintain a reference to class instance between method calls you need to use a class-level variable. If you want to keep track of a single Widget object then you'd use a Widget variable. If you wanted to keep track of multiple Widget objects then you might use a Widget array or a collection, like a List(Of Widget).
 
If you wanted to keep track of multiple Widget objects then you might use a Widget array or a collection, like a List(Of Widget).

I thought I'd understood everything up to here, but I'm confused as to how to add new widgets to the list.

Assuming
VB.NET:
    Public manyWidgets As List(Of WIDGET)
    Public intQuantityOfWidgets As Int32 = 0

why does

VB.NET:
    Sub AddWidget()
        intQuantityOfWidgets += 1
        Dim singleWidget As New WIDGET
        singleWidget.ID = intQuantityOfWidgets
        manyWidgets.Add(singleWidget)
    End Sub

throw a NullReferenceException on the Add method :confused:
 
I think I've answered my own question :eek:

VB.NET:
    Public manyWidgets As New List(Of WIDGET)
 
and then to call that specific widget from the list from lets say the ID the user inputs to a textbox...

VB.NET:
For counter = 0 To intQuantityOfWidgets Step 1

    If CInt(me.textbox1.text) = counter Then
             'code to use or modify widget properties here...
    End If

Next counter
 
Last edited:
If you need a lookup list you can use a Dictionary if you have a unique identifier. But you can also For-Next or For-Each iterate a plain collection to find an item.

The For-Next loop you posted doesn't really make any sense, why would you count from 1-10 to find 5 when 5 is already at hand? If id 5 was what you were looking for you would query the collection for it. If this was supposed to mean a widget ID in a list you create a function to find widget by id:
VB.NET:
function FindWidget(id as integer) as widget
  for each w as widget in widgets
    if w.id=id then return w
  next
  return nothing
end function
 
Back
Top