+ Reply to Thread
Results 1 to 5 of 5

Deliver UserForm Array to a class

Hybrid View

  1. #1
    Registered User
    Join Date
    05-23-2013
    Location
    switzerland
    MS-Off Ver
    Excel 2010
    Posts
    9

    Deliver UserForm Array to a class

    Hey Guys

    I try to program a genetic Algo. Generating the Population and mutate/breed them already works fine, as long as i define the class properties as constant Integers. My goal is now to deliver some of these properties by a Userform, and that's where i fail, because the Array i try to deliver is always empty, when the userform finishes.

    That's where the 50 classes of the population are initiated

    Const PopulationSize As Integer = 50
    Public CurrentGen(1 To PopulationSize) As clsLabAssignement
    Public NextGen(1 To PopulationSize) As clsLabAssignement
    Public MutationRate As Double
    Public MaxGenerations As Double
    Public SumFitnessScore As Double
    
    Sub EvolveButton_Click()
    Set ws1 = Sheets(1)
    Set ws2 = Sheets(2)
    Set ws3 = Sheets(3)
    Set ws4 = Sheets(4)
    Set ws5 = Sheets(5)
    Set ws6 = Sheets(6)
    
    
    MaxGenerations = 50
    MutationRate = 0.01
    
    For i = 1 To PopulationSize
        Set CurrentGen(i) = New clsLabAssignement
        Set NextGen(i) = New clsLabAssignement
        
        CurrentGen(i).iMutationRate = MutationRate
        CurrentGen(i).iDeviceAmount = ws2.Cells(1, 1)
    Next
    
    End Sub

    Public iDeviceAmount As Double
    Public aDeviceLimits As Variant
    
    Private Sub Class_Initialize()
    
    
    iDeviceAmount = ws2.Cells(1, 4).End(xlToRight).Column - 3
        
        ReDim aDeviceLimits(1 To iDeviceAmount, 1 To 2)
        If CurrentGen(1) Is Nothing Then
            ModuleTypes.Show
        Else
            aDeviceLimits = CurrentGen(1).aDeviceLimits
        End If
    This is the start of the class_initialize, where i try to assign the limits over the Module Types userform, in which the user can put in the different kind of Modules for each of the Devices.
    logically the DeviceLimits are the same for every LabAssignement in the same population.

    Private Sub OKButton4_Click()
    Dim ComboControl As Control
    ReDim aDeviceLimits(1 To ws2.Cells(1, 1), 1 To 2)
    
    i = 1
    For Each ComboControl In ModuleTypes.Controls
        If TypeName(ComboControl) = "ComboBox" Then
            Module = ComboControl.Text
                
                Select Case Module
                Case "a1", "a2"
                    aDeviceLimits(i, 1) = aDeviceLimits(i, 1) + 50
                Case "b1"
                    aDeviceLimits(i, 2) = aDeviceLimits(i, 2) + 30
                Case "a3", "a4"
                    aDeviceLimits(i, 1) = aDeviceLimits(i, 1) + 30
                End Select
            p = p + 1
        End If
        If p = ws2.Cells(2, i + 3) Then
            i = i + 1
            p = 0
        End If
    Next
    
    Unload Me
    
    End Sub
    So there are 2 different types of modules (a and b), and for the type a there are again 4 versions. Every different module/version the SpaceLimit in the Device increases.
    How is it now possible to deliver this aDeviceLimit array for the First class back to the class_Initialize sub after the Unload Me of the Userform to work with it afterwards? The Calculation in The Userform is correct, but after unloading it, the array is empty again.

    I hope you can help me.

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Deliver UserForm Array to a class

    As far as I can see that's because you've created and dimmed the class in the userform, then the userform terminates all it's variables are released - killing your classes. You need to declare the classes elsewhere, though without seeing the structure of your workbook; I can't really comment where.

    P.S what's with all the public variables - they need killing with fire

  3. #3
    Registered User
    Join Date
    05-23-2013
    Location
    switzerland
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Deliver UserForm Array to a class

    Hey Thanks for your answer

    No the Classes are genereated in a proper Module(First Snippet). The only code from the Userform is the last snippet in my former post. The second one is in the classModule.
    The Evolve Button is on a Worksheet, and the OK_Button4 is the Button on the UserForm.
    What do you mean with killing the Public Variables? Could that be the Problem? I actually built it all up like a simple example in a book about genetic Algos in VBA, where they defined the class properties as pulbic variables, thus my problem is not as simple as the one in the book.
    Last edited by Guesty; 08-12-2013 at 08:12 AM.

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Deliver UserForm Array to a class

    Ok, your problem is one of scope. aDeviceLimits is out of scope in the userform - you need to explicitly pass it into the form/read it from the form from the class when you initialize it. Consider:

    Module:
    Sub test()
    
    Dim g As Class1
    Set g = New Class1
    
    End Sub
    Class:
    Option Explicit
    Dim p_MyClassArray As Variant
    
    Private Sub Class_Initialize()
        Dim oUserForm As UserForm1
        Set oUserForm = New UserForm1
        
        oUserForm.MyArray = p_MyClassArray
        
        oUserForm.Show
        
        p_MyClassArray = oUserForm.MyArray
    
        Debug.Print p_MyClassArray(0)
        Debug.Print p_MyClassArray(1)
    End Sub
    Userform:
    Option Explicit
    Dim p_MyArray As Variant
    
    Public Property Let MyArray(value As Variant)
        p_MyArray = value
    End Property
    Public Property Get MyArray() As Variant
        MyArray = p_MyArray
    End Property
    Private Sub CommandButton1_Click()
        p_MyArray = Array("My", "Array")
        Me.Hide
    End Sub
    In the above passing in the array is pointless since it is just overwritten, but it's possible you might want to in certain circumstances - where you want the userform to act on the existing array for example. If you comment out passing the array to the userform you'll see things work as expected.

    RE The public variables: All variables should have as smaller scope as possible, especially in a case like this where things get very complicated very quickly. Public variables can be changed from anywhere in your code - this is bad practice and makes bug hunting extremely difficult, if you consider the above, you'll notice that there are no public variables.
    Last edited by Kyle123; 08-12-2013 at 08:28 AM.

  5. #5
    Registered User
    Join Date
    05-23-2013
    Location
    switzerland
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Deliver UserForm Array to a class

    OK Thank you very much. It Worked

    But i still had to declare the
    Dim p_MyClassArray As Variant
    ( in my Case aDeviceLimits)as a public Variable, otherwise he said me, that he could not find this method or Object later in the Code.

    But Anyway i think this should be not such a big issue. Thanks again=)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Call a class sub from userform
    By excel-yes in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-10-2011, 04:11 PM
  2. search and deliver
    By damorrison in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-05-2006, 05:30 PM
  3. Generic Userform class
    By RB Smissaert in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-22-2005, 12:05 PM
  4. class modules and array
    By Martin Bauer in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-05-2005, 09:40 PM
  5. refedits in a userform control class
    By Doug Glancy in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-03-2005, 06:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1