Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I've got a VB question which *should* be easy ... if you know how to solve it.

 

Currently, my code uses a multi dimentional array:

 

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:

 

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?

 

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:

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:

Private SD() as new SwitchClass()

Any thoughts or suggestions? Anyone?

~Joe

  • *Experts*
Posted (edited)

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 don't have to worry those errors:

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 don't know if im giving you the answer you are looking for because I don't know what is wrong.

Edited by mutant
Posted

Sorry for taking so long to get back with you. I've been trying other methods, including yours (which might work).

 

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

 

Private SD(12) As New SwitchClass() ' Intellisence says "Arrays cannot be declared with 'New'."

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:

Private SD(12) as SwitchClass

but it will not let me assign anything to it.

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.

Posted

PD:

I've tried that, but I think I am doing something wrong. I've renamed a few of the classes to make them more intuitive. Here is the way it looks right now:

    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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...