dictionary

Discussion in 'ASP .Net' started by André Freitas, Jan 18, 2010.

  1. 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é
     
    André Freitas, Jan 18, 2010
    #1
    1. Advertising

  2. 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


    "Mark Rae [MVP]" <> escreveu na mensagem
    news:...
    > "André Freitas" <andrefreitas> wrote in message
    > news:%...
    >
    >> 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"

    >
    > A Dictionary is composed of two parts - a key and a value - so neither of
    > the above would fit into a Dictionary directly
    >
    > It would be helpful if you could explain a little more about what you are
    > trying to achieve...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
     
    André Freitas, Jan 18, 2010
    #2
    1. Advertising

  3. > [please don't top-post]


    >>>> 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"



    >>> A Dictionary is composed of two parts - a key and a value - so neither
    >>> of the above would fit into a Dictionary directly
    >>>
    >>> It would be helpful if you could explain a little more about what you
    >>> are trying to achieve...



    >> Well, I'm trying to set default rage and default error message to all
    >> kinds of inputs in the project, since today there's a mess, same fields
    >> validated with different rage.
    >>
    >> I can have 2 Dictionaries... one for regular expressions, one for error
    >> messages, but I'm not sure about the best way to implement it in a easy
    >> way to use..


    > 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,
     
    André Freitas, Jan 18, 2010
    #3
  4. >>>>> 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"

    >
    >
    >>>> A Dictionary is composed of two parts - a key and a value - so neither
    >>>> of the above would fit into a Dictionary directly
    >>>>
    >>>> It would be helpful if you could explain a little more about what you
    >>>> are trying to achieve...

    >
    >
    >>> Well, I'm trying to set default rage and default error message to all
    >>> kinds of inputs in the project, since today there's a mess, same fields
    >>> validated with different rage.
    >>>
    >>> I can have 2 Dictionaries... one for regular expressions, one for error
    >>> messages, but I'm not sure about the best way to implement it in a easy
    >>> way to use..

    >
    >> 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,
    >


    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, Jan 18, 2010
    #4
  5. André Freitas wrote:
    > 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.

    --
    Göran Andersson
    _____
    http://www.guffa.com
     
    Göran Andersson, Jan 19, 2010
    #5
  6. >> 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.
    >
    > --
    > Göran Andersson
    > _____
    > http://www.guffa.com



    Can you show me how can I use it?

    Thank you.
     
    André Freitas, Jan 19, 2010
    #6
  7. André Freitas wrote:
    >>> 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.
    >>
    >> --
    >> Göran Andersson
    >> _____
    >> http://www.guffa.com

    >
    >
    > Can you show me how can I use it?
    >
    > Thank you.
    >


    To get the settings for a name:

    InputSettings emailSettings = settings["email"];

    --
    Göran Andersson
    _____
    http://www.guffa.com
     
    Göran Andersson, Jan 25, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    6
    Views:
    485
    Ilias Lazaridis
    Feb 21, 2006
  2. james_027
    Replies:
    1
    Views:
    364
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  3. Navkirat Singh
    Replies:
    6
    Views:
    3,346
    Navkirat Singh
    Jul 29, 2010
  4. Chris Rebert
    Replies:
    0
    Views:
    573
    Chris Rebert
    Jul 29, 2010
  5. Fox

    dictionary within dictionary

    Fox, Mar 8, 2005, in forum: ASP General
    Replies:
    5
    Views:
    215
    Michael D. Kersey
    Mar 13, 2005
Loading...

Share This Page