How can I loop with a Class?

joe_pool_is

Well-known member
Joined
Jan 18, 2004
Messages
451
Location
Texas
Ive got a VB question which *should* be easy ... if you know how to solve it.

Currently, my code uses a multi dimentional array:

Code:
dim Position as short = 0
dim IsOn as short = 1
for i = 0 to (whatever)
  switcher(i, Position) = this
  switcher(i, IsOn) = that
next

This is short and easy, but every element of the array has to be the same datatype (all integers, all strings, etc.).

To fix this, I want to use a class:

Code:
class SwitchClass
  public Position as String
  public IsOn as Boolean
end Class
Private SD01 as New SwitchClass()
Private SD02 as New SwitchClass()
etc.

How can I use this in my loop now?

Code:
for i = 0 to (whatever)
  SDxx.Position = This
  SDxx.IsOn = False
next

See my problem? How can I replace xx with i? I tried this:
Code:
SD{Convert.ToString(i).PadLeft(2, 0)}.Position = This
but that did not work. Also, I tried to declare an array of classes, but that did not work either:
Code:
Private SD() as new SwitchClass()
Any thoughts or suggestions? Anyone?
~Joe
 
How did declaring an array of classes did not work? DId it give you errors about members not being initialized or out of bounds excpetions? To get this done you could easily use an arraylist because you dont have to worry those errors:
Code:
Dim vars As New ArrayList
add something to the array list
vars.Add(New SwitchClass)
vars.Add(New SwitchClass)
Loop through each object in the array list
For each sc as SwitchClass in vars
   sc.Position = this
   sc.IsOn = false
next
When you say that something doesnt work with the array, please explain more as it will be MUCH easier to come up with an answer. For example now, I dont know if im giving you the answer you are looking for because I dont know what is wrong.
 
Last edited by a moderator:
Sorry for taking so long to get back with you. Ive been trying other methods, including yours (which might work).

Regarding your question, I have been trying to initialize my arrays using these:

Code:
Private SD(12) As New SwitchClass()  Intellisence says "Arrays cannot be declared with New."
Code:
Private SD(12) As SwitchClass()  Intellisence says "Array modifiers cannot be spedified on both a variable and its type."
The example below allows me to declare the variable:
Code:
Private SD(12) as SwitchClass
but it will not let me assign anything to it.
Code:
SD(1).Address = 1  Intellisence says "Object not set to an instance of a variable"
I am still working with your example, trying to find a way to implement it. If you (or anyone else) has any other input, I am all ears.
 
PD:
Ive tried that, but I think I am doing something wrong. Ive renamed a few of the classes to make them more intuitive. Here is the way it looks right now:
Code:
    Class SwitchValues
        Class Addresses
            Public Num As Short = 0
            Public Name As String = ""
            Public Last As String = ""
        End Class
        Class Positions
            Public Num As Short = 0
            Public Name As String = ""
            Public Last As String = ""
        End Class
        Public Addy As New Addresses()
        Public Pos As New Positions()
        Public TallyOn As Boolean = False
        Public Exists As Boolean = False
        Public Button() As String = {"N/A", "RT1", "RT2", "LD1", "LD2", "AX1", "AX2", "AX3", "AX4", "AX5", "SAFE"}
    End Class
    Class SwitcherBox
        Public SD01 As New SwitchValues()
        Public SD02 As New SwitchValues()
        Public SD12 As New SwitchValues()
        Public SD03 As New SwitchValues()
        Public SD13 As New SwitchValues()
        Public SD04 As New SwitchValues()
        Public SD05 As New SwitchValues()
        Public SD06 As New SwitchValues()
        Public SD07 As New SwitchValues()
        Public SD08 As New SwitchValues()
        Public SD09 As New SwitchValues()
        Public SD10 As New SwitchValues()
        Public SD11 As New SwitchValues()
    End Class
    Private Studio(12) As New SwitchValues()  Arrays cannot be declared with New.
    Private objStudio As Studio(12)  Type Studio is not defined, and Array bounds cannot appear in type specifiers.
 
Back
Top