Results 1 to 8 of 8

Thread: [2005] Multiple Instances of a Custom Class

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    90

    Question [2005] Multiple Instances of a Custom Class

    Hi all

    I have a sub that runs on a button click which I want to create a new instance of a custom class and add it to a list of already created instances of this class. I want the result to be a List Of collection of all of my class instances.

    My problem is that each time I run the sub, it simply 'overwrites' the first instance of the class and adds a copy of it to the list instead of writing a new instance. My code is along these lines :-

    Code:
    dim objFixture as new clsFixture
    
    For n As Integer = 1 To intNoOfChannels
         objFixture.SetChannelAddress(n, intCurrentChannelTotal + 1)
         intCurrentChannelTotal += 1
    next
    
    lstFixtures.Add(objFixture)
    Can anyone let me know what I'm obviously doing wrong?

    Hope that makes sense

    Andy

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: [2005] Multiple Instances of a Custom Class

    There's nothing obviously wrong with what you have posted. It does appear that two classes built in this fashion would be highly similar, based on my guess as to what SetChannelAddress does. Therefore, I would next pursue these two paths:

    1) How can you tell that the new class overwrites the first, and how can you tell that the other class is a copy?

    2) Are there any events or other things that might tie the two objects together such that changing one object can cause changes in the other object?

    Edit: Also, I assume that lstFixtures is defined as List (of clsFixture). If that's not the case, then what is it?
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    90

    Re: [2005] Multiple Instances of a Custom Class

    Sorry for not explaining fully

    Basically, the SetChannelAddress routine builds an array of Channel values. The first time the code is executed, the instance gets assigned values 1 - 5. The next time the code is executed, the new instance should get assigned values 6 - 10. I should end up with two instances, the first one holding values 1 - 5 and the second holding 6 - 10.

    What actually happens when I step through the code and examine the List is that I end up with two instances in the List, each one containing values 6 - 10. I can actually see the first instance (which is already in the List) getting overwritten as the loop is executed (the first loop iteration for the second instance shows that my Channel values are 6,2,3,4,5 on the existing instance in the List for example).

    There's no other code that accesses the List or class instance and nothing that could tie the instances together.

    lstFixtures is indeed defined as a List (of clsFixture)

    I'm stumped

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: [2005] Multiple Instances of a Custom Class

    Post the definition of SetChannelAddress, and the constructor (Sub New) for clsFixture, if there is one.

    This type of question always has the same answer, but in your case it is MUCH more difficult to find than it normally would be. For some reason, all the classes that are being created have the same array. Somewhere in the code there is a line that does this:

    arrayX = arrayY

    It may not look like that, but it is there, in some fashion. The most likely sources for this are in the constructor for clsFixture, and the SetChannelAddress method.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    90

    Re: [2005] Multiple Instances of a Custom Class

    It's a pretty simple class :-

    Code:
    Public Class clsFixture
    
        Private strName As String
        Private strManufacturer As String
        Private imgPicture As Image
        Private strDescription As String
        Private strFilename As String
        Private intType As Integer
        Private intNoOfChannels As Integer = 0
        Private arrChannelTypes() As Integer
        Private arrChannelDMXAddress() As Integer
    
        Public Sub New()
            arrChannelTypes = Nothing
            arrChannelDMXAddress = Nothing
        End Sub
    
        Public Property Name() As String
            Get
                Return strName
            End Get
            Set(ByVal value As String)
                strName = value
            End Set
        End Property
    
        Public Property Manufacturer() As String
            Get
                Return strManufacturer
            End Get
            Set(ByVal value As String)
                strManufacturer = value
            End Set
        End Property
    
        Public Property Picture() As Image
            Get
                Return imgPicture
            End Get
            Set(ByVal value As Image)
                imgPicture = value
            End Set
        End Property
    
        Public Property Description() As String
            Get
                Return strDescription
            End Get
            Set(ByVal value As String)
                strDescription = value
            End Set
        End Property
    
        Public ReadOnly Property StartAddress() As Integer
            Get
                Return arrChannelDMXAddress(0)
            End Get
        End Property
    
        Public Property Type() As Integer
            Get
                Return intType
            End Get
            Set(ByVal value As Integer)
                intType = value
            End Set
        End Property
    
        Public Property Filename() As String
            Get
                Return strFilename
            End Get
            Set(ByVal value As String)
                strFilename = value
            End Set
        End Property
    
        Public Property NoOfChannels() As Integer
            Get
                Return intNoOfChannels
            End Get
            Set(ByVal value As Integer)
                intNoOfChannels = value
    
                ReDim arrChannelDMXAddress(intNoOfChannels - 1)
                ReDim arrChannelTypes(intNoOfChannels - 1)
    
            End Set
        End Property
    
        Public Sub SetChannelAddress(ByVal Channel As Integer, ByVal Address As Integer)
            arrChannelDMXAddress(Channel - 1) = Address
        End Sub
    
        Public Sub SetChannelType(ByVal Channel As Integer, ByVal Type As String)
            arrChannelTypes(Channel - 1) = Type
        End Sub
    
    End Class

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: [2005] Multiple Instances of a Custom Class

    Hmmm, still see nothing. That IS a simple class. Very interesting.

    The next thing I would try would be to put a breakpoint on this line from your first post:

    For n As Integer = 1 To intNoOfChannels

    When the breakpoint is reached, I would examine the object in the list in detail (ok, I guess you'd have to reach that line the second time for anything to be in the list). Does it have the 1-5 that it should have? If so, I would then step through the code using F10 to skip over the functions, or else put a breakpoint on the .Add method, and at the point of the Add, I would look at the object that was just created to confirm that it holds 6-10, and look again at the object in the list to see whether it still holds 1-5.

    If it was 1-5 at the first check (before the loop is run), then changes to 6-10 by the time of the Add, then I would repeat those steps and watch for when the existing object changes values. If it was 1-5 at the first check, then is STILL 1-5 at the Add, then I would check immediately AFTER the add, at which point it should not have changed at all.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    90

    Thumbs up Re: [2005] Multiple Instances of a Custom Class

    Ok, so I've found the problem!

    It's been down to how the initial object was created and properties set (which I omitted from the example code as I considered it irrelevant to the problem )

    The actual application is for controlling light fixtures (as in for stage shows etc). These lights have multiple channels to control different properties such as brightness, red, green, blue, strobe etc, each one having a specific address assigned to it (using what's called the DMX512 protocol for lighting). The class only has to be for holding the light's settings for later use, hence the simplicity.

    I already had a List of available fixtures set up (without any values for the channels or channel addresses, but apart from theat exactly the same as lstFixtures) using the same class. I was then using the available fixture list and picking what fixtures I actually wanted to control. The problem came when I created the new fixture from the available fixture list. I was assigning the new fixture to be equal to the specific one in the available fixture list.

    Code:
    Dim objFixture As New clsFixture
    
    objFixture=lstAvailableFixtures(index)
    
    For n As Integer = 1 To intNoOfChannels
         objFixture.SetChannelAddress(n, intCurrentChannelTotal + 1)
         intCurrentChannelTotal += 1
    Next
    This obviously tied any other instances of this same fixture together, so whenever I changed a value, all other corresponding values in the other clone fixtures changed

    I've fixed it by assigning the properties of the new fixture to be equal to the properties of the available fixture instead of making it directly equal to the actual object.

    Code:
    Dim objFixture As New clsFixture
    
    With objFixture
         .NoOfChannels=lstAvailableFixtures(strFixtureTag).NoOfChannels
         .Manufacturer = lstAvailableFixtures(strFixtureTag).Manufacturer
         .Description = lstAvailableFixtures(strFixtureTag).Description
         .Picture = lstAvailableFixtures(strFixtureTag).Picture
    Next
    
    For n As Integer = 1 To intNoOfChannels
         objFixture.SetChannelAddress(n, intCurrentChannelTotal + 1)
         intCurrentChannelTotal += 1
    Next
    Schoolboy error which I should have noticed (well, never done it in the first place would have been preferable )

    Thanks for your help

    Andy

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: [2005] Multiple Instances of a Custom Class

    Glad to hear that. As I said before, the answer to this type of problem is always the same...you just have to find it.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width