Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8074145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:33:35+00:00 2026-06-05T14:33:35+00:00

My project has classes which, unavoidably, contain hundreds upon hundreds of variables that I’m

  • 0

My project has classes which, unavoidably, contain hundreds upon hundreds of variables that I’m always having to keep straight. For example, I’m always having to keep track of specific kinds of variables for a recurring set of “items” that occur inside of a class, where placing those variables between multiple classes would cause a lot of confusion.

How do I better sort my variables to keep from going crazy, especially when it comes time to save my data?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:33:37+00:00Added an answer on June 5, 2026 at 2:33 pm

    While I’m certain there are numerous ways of keeping arrays straight, I have found a method that works well for me. Best of all, it collapses large amounts of information into a handful of arrays that I can parse to an XML file or other storage method. I call this method my “indexed array system”.

    There are actually multiple ways to do this: creating a handful of 1-dimensional arrays, or creating 2-dimensional (or higher) array(s). Both work equally well, so choose the one that works best for your code. I’m only going to show the 1-dimensional method here. Those of you who are familiar with arrays can probably figure out how to rewrite this to use higher dimensional arrays.

    I use Actionscript 3, but this approach should work with almost any programming or scripting language.

    In this example, I’m trying to keep various “properties” of different “activities” straight. In this case, we’ll say these properties are Level, High Score, and Play Count. We’ll call the activities Pinball, Word Search, Maze, and Memory.

    This method involves creating multiple arrays, one for each property, and creating constants that hold the integer “key” used for each activity.

    We’ll start by creating the constants, as integers. Constants work for this, because we never change them after compile. The value we put into each constant is the index the corresponding data will always be stored at in the arrays.

    const pinball:int = 0;
    const wordsearch:int = 1;
    const maze:int = 2;
    const memory:int = 3;
    

    Now, we create the arrays. Remember, arrays start counting from zero. Since we want to be able to modify the values, this should be a regular variable.

    Note, I am constructing the array to be the specific length we need, with the default value for the desired data type in each slot. I’ve used all integers here, but you can use just about any data type you need.

    var highscore:Array = [0, 0, 0, 0];
    var level:Array = [0, 0, 0, 0];
    var playcount:Array = [0, 0, 0, 0];
    

    So, we have a consistent “address” for each property, and we only had to create four constants, and three arrays, instead of 12 variables.

    Now we need to create the functions to read and write to the arrays using this system. This is where the real beauty of the system comes in. Be sure this function is written in public scope if you want to read/write the arrays from outside this class.

    To create the function that gets data from the arrays, we need two arguments: the name of the activity and the name of the property. We also want to set up this function to return a value of any type.

    GOTCHA WARNING: In Actionscript 3, this won’t work in static classes or functions, as it relies on the “this” keyword.

    public function fetchData(act:String, prop:String):*
    {
        var r:*;
        r = this[prop][this[act]];
        return r;
    }
    

    That queer bit of code, r = this[prop][this[act]], simply uses the provided strings “act” and “prop” as the names of the constant and array, and sets the resulting value to r. Thus, if you feed the function the parameters (“maze”, “highscore”), that code will essentially act like r = highscore[2] (remember, this[act] returns the integer value assigned to it.)

    The writing method works essentially the same way, except we need one additional argument, the data to be written. This argument needs to be able to accept any

    GOTCHA WARNING: One significant drawback to this system with strict typing languages is that you must remember the data type for the array you’re writing to. The compiler cannot catch these type errors, so your program will simply throw a fatal error if it tries to write the wrong value type.

    One clever way around this is to create different functions for different data types, so passing the wrong data type in an argument will trigger a compile-time error.

    public function writeData(act:String, prop:String, val:*):void
    {
        this[prop][this[act]] = val;
    }
    

    Now, we just have one additional problem. What happens if we pass an activity or property name that doesn’t exist? To protect against this, we just need one more function.

    This function will validate a provided constant or variable key by attempting to access it, and catching the resulting fatal error, returning false instead. If the key is valid, it will return true.

    function validateName(ID:String):Boolean
    {
        var checkthis:*
        var r:Boolean = true;
    
        try
        {
            checkthis = this[ID];
        }
        catch (error:ReferenceError)
        {
            r = false;
        }
    
        return r;
    }
    

    Now, we just need to adjust our other two functions to take advantage of this. We’ll wrap the function’s code inside an if statement.

    If one of the keys is invalid, the function will do nothing – it will fail silently. To get around this, just put a trace (a.k.a. print) statement or a non-fatal error in the else construct.

    public function fetchData(act:String, prop:String):*
    {
        var r:*;
    
        if(validateName(act) && validateName(prop))
        {
            r = this[prop][this[act]];
            return r;
        }
    }
    
    public function writeData(act:String, prop:String, val:*):void
    {
        if(validateName(act) && validateName(prop))
        {
            this[prop][this[act]] = val;
        }   
    }
    

    Now, to use these functions, you simply need to use one line of code each. For the example, we’ll say we have a text object in the GUI that shows the high score, called txtHighScore. I’ve omitted the necessary typecasting for the sake of the example.

    //Get the high score.
    txtHighScore.text = fetchData("maze", "highscore");
    
    //Write the new high score.
    writeData("maze", "highscore", txtHighScore.text);
    

    I hope ya’ll will find this tutorial useful in sorting and managing your variables.

    (Afternote: You can probably do something similar with dictionaries or databases, but I prefer the flexibility with this method.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have an android project which has a lot of classes I've built. i
I have an ntier solution which has a Domain project for my POCO classes.
I have created a project that uses scala that has some abstract classes definitions
Let's say I make a Java project in Eclipse that has 3-10 classes and
I have an xcode project which has 2 classes - Stem & Player, I'm
My GUI project in Qt has a lot of configuration pages classes which all
I have been given new project which has many classes and I have to
I have an application which has some classes that handle some specific functions, have
The project has a goal: use interface centric design. Basically we declare classes of
Right now, my project has two classes and a main. Since the two classes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.