dictionary

  • Thread starter Thread starter André Freitas
  • Start date Start date
A

André Freitas

Whats the best way to implement a dictionary like class who gets 3 columns.
Like:

"name", "regex", "error msg"

Or

"name", "reg" and "name", "error msg"

Regards,
André
 
Well, im trying to set default regex and default error message to all kinds
of inputs in the project, since today theres a mess, same fields validated
with diferent regex.

I can have 2 dictionarys... one for regular expressions, one for error
messages, but im not sure about the best way to implement it in a easy way
to use.. its why im asking the professional tips =D

Regards
 
[please don't top-post]

Assuming that the data is unlikely to change very often, I'd probably use
a DataTable which I'd create and populate in Application_Start and store
in Application memory. Then I'd just use standard ADO.NET functionality on
it. I do this for (almost) static data e.g. country codes, currency codes
etc...


U meant something like that:

public string returnregexp(string fieldname)
{
/*
declare the datatable here and populate the datatable here
return the correct field using probaly a datatable.select
or using a dictionary like functionality
*/
}

Regards,
 
Whats the best way to implement a dictionary like class who gets 3
U meant something like that:

public string returnregexp(string fieldname)
{
/*
declare the datatable here and populate the datatable here
return the correct field using probaly a datatable.select
or using a dictionary like functionality
*/
}

Regards,

Sorry about double post, theres no way to avoid that.

And about using a static method, that i can access without a instance of
then? Will it be expensive for the server to keep it in memory?
 
André Freitas said:
Whats the best way to implement a dictionary like class who gets 3 columns.
Like:

"name", "regex", "error msg"

Or

"name", "reg" and "name", "error msg"

Regards,
André

Make a custom class that contains a regular expression and an error
message, and use in a dictionary:

public class InputSettings {

public string Regex { get; private set; }
public string ErrorMessage { get; private set; }

public InputSettings(string regex, string errorMessage) {
Regex = regex;
ErrorMessage = errorMessage;
}

}

var settings = new Dictionary<string, InputSettings>();
settings.Add(
"name", new InputSettings(
".+", "Name has to be at least three characters."
)
);
settings.Add(
"email", new InputSettings(
"@", "The email address is incomplete."
)
);

Now you can efficiently look up the settings based on the control name.
 
Whats the best way to implement a dictionary like class who gets 3
Make a custom class that contains a regular expression and an error
message, and use in a dictionary:

public class InputSettings {

public string Regex { get; private set; }
public string ErrorMessage { get; private set; }

public InputSettings(string regex, string errorMessage) {
Regex = regex;
ErrorMessage = errorMessage;
}

}

var settings = new Dictionary<string, InputSettings>();
settings.Add(
"name", new InputSettings(
".+", "Name has to be at least three characters."
)
);
settings.Add(
"email", new InputSettings(
"@", "The email address is incomplete."
)
);

Now you can efficiently look up the settings based on the control name.


Can you show me how can I use it?

Thank you.
 
Back
Top