Denis Cekic
Denis Cekic

Reputation: 155

asp.net MVC and validating a selection box

I am creating a profile page wizard so the user would have quite a few select boxes to validate against.

The way I have it set up the value of my select boxes are keys to an string array.

For example:

string[] HairColor = {"blonde", "amber", "black", "platinum"}.

Would there be a threading issue if I create a helper static class, and uses a method such as:

public static string GetHairColor(int key)
{
 string[] HairColor = {"blonde", "amber", "black", "platinum"}.
 return HairColor[key];
}

The reason why I want to return a string, is because I would rather limit the amount I would have to validate against, if the number returns a value then, I call a db or cache save. So instead of the user inserting there string values, I am inserting it for them based on the number they pass in.

My question is if users were concurrently checking against this method, would there be an issue with threading?

*note I am not passing the array into the view, I am just trying to quickly check the user isn't passing in a number that doesn't exist, if it does grab it and persist it. Thanks for your help everyone.

Upvotes: 1

Views: 58

Answers (2)

Steve Holdorf
Steve Holdorf

Reputation: 131

Because you are using a static method you won't have any threading issues because all of the code accessing the static value are operating in there own sessions. The static value is part of the main application thread an has no effect on any other thread or session.

Upvotes: 0

Erik Philips
Erik Philips

Reputation: 54638

note I am not passing the array into the view, I am just trying to quickly check the user isn't passing in a number that doesn't exist, if it does grab it and persist it.

Then don't let the user pass an invalid number, that is the best user design. Make it a drop down.

As a side note:

public static string GetHairColor(int key)
{
  string[] HairColor = {"blonde", "amber", "black", "platinum"}.
  return HairColor[key];
}

Will throw an IndexOutOfRangeException if that is all your validation does.

Upvotes: 1

Related Questions