+ Reply to Thread
Results 1 to 5 of 5

Deliver UserForm Array to a class

  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

    Please Login or Register  to view this content.

    Please Login or Register  to view this content.
    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.

    Please Login or Register  to view this content.
    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:
    Please Login or Register  to view this content.
    Class:
    Please Login or Register  to view this content.
    Userform:
    Please Login or Register  to view this content.
    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
    Please Login or Register  to view this content.
    ( 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. [SOLVED] 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