Windows Develop Bookmark and Share   
 index > Windows Forms General > Performance question: Arrays vs. Public Variables
 

Performance question: Arrays vs. Public Variables

I have come across a few situations where I face the option of either (1) creating a public shared variable and passing this to another called procedure or (2) iterating through an array of records within the called procedure to retrieve the piece of data. Which would be the more efficient approach in terms of speed and memory?

(Assume (1) the arrays in question usually contain less than 100 records each and (2) the variables would need to remain public and shared as long as the application remains open.)

As a follow up question, how significant is the performance difference between the two options if the application has 100 arrays? 1000 arrays? (Or does it matter--one option is always more efficient over the other?)

I would appreciate any thoughts on this.

Regards,

Chuck
MigrationUser 1  Friday, February 07, 2003 12:46 AM
Ok, then combine the best of both worlds. Assuming you have your UserSettings class, simply when the user logs in find the settings and keep that reference ... for example

Excuse the pseudo-code/c# nature of my code, only for clarity

class ...


private static UserSetting mCurrentUserSettings = null;

public static UserSetting CurrentUserSetting
{
get
{
return mCurrentUserSettings;
}
}

void ProcessLogin(string userName)
{
foreach(UserSetting _us in AllUserSettingsArray)
if(_us.Username == userName)
mCurrentUserSettings = _us;
}

void Logout()
{
mCurrentUserSettings = null;
}

...

Textbox1.ForeColor = ThatClassAbove.CurrentUserSettings.ForeColorPreference;

...

 


I hope this gives you an idea. You only have one public shared variable and you still have the array intact. Let me know if I misread your request and need to answer again.
MigrationUser 1  Friday, February 07, 2003 2:16 AM
Well, without knowing exactly what the specs are on the type of data you are storing, or how exactly these "records" are stored in the array, I would say go with storing them in public shared variables.

However, I would like to know more about these types of situations. I'm sure another and better method exists, so please do detail your problem.
MigrationUser 1  Friday, February 07, 2003 1:11 AM
yah, please give us more details, because i'm a bit fuzzy on what you're trying to do too :~

also, just thought i'd throw out that if you're using a shared variable, no need to pass it around from function to function, just access it directly if that's applicable (again i might not be understanding what you're going for).
MigrationUser 1  Friday, February 07, 2003 1:13 AM
A good example (on a much smaller scale) is an application that is designed to run on a single computer that is accessed by multiple users. (One computer in a room with many users--4 to 8 users with the situation I came across.) Because multiple users access the application on the same computer, the application has to be designed to have a "quick switch/logon" feature between users while the application is still running. When the current user changes, the user preference settings must also change. I have considered building a "User Preference" structure to store the 25 to 30 different preference settings (font color, # of drop down items in a combobox, show tooltips yes/no, etc) and then creating an array to store the user preference structure for all users. Since I have a global variable set to store the current user's ID, I now have the option to either (1) pull the current user's preference settings out of the array and store in 25 to 30 public variables or (2) simply iterate through the array containing all "user preference" structures to find and return the preference setting currently required by my code.

True, we could have accessed the database directly to get the user preferences, but for reasons I am fuzzy about, the development team decided to dump the "user preference" info into a public array and reduce accessing the database for this info. (???)

Hope this sheds some more light on my original question.
MigrationUser 1  Friday, February 07, 2003 2:05 AM
Ok, then combine the best of both worlds. Assuming you have your UserSettings class, simply when the user logs in find the settings and keep that reference ... for example

Excuse the pseudo-code/c# nature of my code, only for clarity

class ...


private static UserSetting mCurrentUserSettings = null;

public static UserSetting CurrentUserSetting
{
get
{
return mCurrentUserSettings;
}
}

void ProcessLogin(string userName)
{
foreach(UserSetting _us in AllUserSettingsArray)
if(_us.Username == userName)
mCurrentUserSettings = _us;
}

void Logout()
{
mCurrentUserSettings = null;
}

...

Textbox1.ForeColor = ThatClassAbove.CurrentUserSettings.ForeColorPreference;

...

 


I hope this gives you an idea. You only have one public shared variable and you still have the array intact. Let me know if I misread your request and need to answer again.
MigrationUser 1  Friday, February 07, 2003 2:16 AM
Actually, that is just what I was looking for.  Thanks for the help.
MigrationUser 1  Friday, February 07, 2003 2:29 AM

You can use google to search for other answers

Custom Search

More Threads

• ListView, SubItems, ForeColor
• how to make a control display things outside its boundaries
• GDI+ (Displaying Sine Wave in C#)
• Problem with the Scrollbar corner painting
• dateTimePicker Values
• TargetInvocationException - Exception Handling Application Block
• Get number of UserObjects
• Acessing a control on a form from a seperate class
• Display Windows form with call to dll?
• Web Browser Favorites List