Better way other than hardcode in VS RRS feed

  • Question

  • Hi All,

    Is it any better solution other than hardcode as below?

    string _pn = "";
    string pn = sline[1].Replace("'", "");
    if (pn.ToUpper().Contains("AAAA1")) { _pn = "A_A"; }
    else if (pn.ToUpper().Contains("BBBB2")) { _pn = "B_B"; }
    else if (pn.ToUpper().Contains("CCCC3")) { _pn = "C_C"; }
    else if (pn.ToUpper().Contains("DDDD4")) { _pn = "D_D"; }
    else if (pn.ToUpper().EndsWith("S")) { _pn = "S_S"; }
    else { _pn = "NULL"; }

    Maybe can get from XML/Text file?

    Please help and advise.

    Thanks in advance.

    Tuesday, April 24, 2018 2:09 AM

All replies

  • Each type could be represent as class which implements interface with method bool IsMatch(string) and string Evaluate(string). 

    Then you create list of instances and loop it. If IsMatch is true then you call Evaluate method which return result into _pn variable. 

    But solution is depend on project. In small project I think your solution could be better.

    Tuesday, April 24, 2018 2:41 AM
  • If it's not broke, then don't fix it.

    However, you may want to use a Case statement for cleaner looking code.

    Tuesday, April 24, 2018 4:19 AM
  • Check this approach too:

    string _pn = "";
    string pn = . . .
    var defs = new string[,]
            {"AAAA1", "A_A" },
            {"BBBB2", "B_B" },
            {"CCCC3", "C_C" },
            {"DDDD4", "D_D" },
            { "S$", "S_S" },
    int ? j = Enumerable.Range( 0, defs.GetLength( 0 ) ).Cast<int?>().FirstOrDefault( i => Regex.IsMatch( pn, defs[i.Value, 0], RegexOptions.IgnoreCase ) );
    _pn = j == null ? "NULL" : defs[j.Value, 1];

    • Edited by Viorel_MVP Tuesday, April 24, 2018 5:08 AM
    Tuesday, April 24, 2018 5:05 AM
  • In the Solution Explorer expand "Properties" and then double-click "Settings.setings".

    For console programs you might need to go to the project properties and click on the Settings there.

    You can put strings there. If you set the Scope to Application then that means the string is meant to be constant and not changed during execution. The settings are saved as an XML file for the project. To get the string, in the source code start typing "properties" and IntelliSense will guide you to "Properties.Settings.Default.someSetting1" where someSetting1 is the name you provide.

    Sam Hobbs

    Tuesday, April 24, 2018 7:46 PM
  • If it is only a couple of values then I'd leave it alone as DA924x mentioned.

    If you want this to be expandable over time then I'd move it outside the code. If you want to be able to change things without recompiling then a database or file would be appropriate. To make it a little flexible you could store in a simple file (maybe JSON) the pattern and result expected. Depending upon how complex you want to get you could do something simple like just wildcard support.

    - *AAAA1* = A_A

    - *S = S_S

    For more complex patterns then maybe using a regular expression would be easier. Irrelevant your code would have to load the patterns into memory and then enumerate them. For each one you'd do the appropriate match check. If matched then return the value otherwise keep going.

    Michael Taylor

    Wednesday, April 25, 2018 2:48 PM