0

I want to build a class that parses a Road Number (RdNumber) code for sorting that overrides the default alphanumeric sort of the original values. Examples of the original RdNumber code and they way they would sort ascending are:

  • M1010
  • M10230
  • M10230A
  • M1234
  • M1234A
  • M1234B
  • M12456
  • M12456A
  • M1265
  • M1265A
  • S4056
  • S4056A

I would like to create a class with properties that gets the original RdNumber and parses it into three additional separate properties of a prefix, a 4 or 5 digit integer numeric value and an optional suffix so that I could implement an IComparer that would sort the values like this based on those additional properties:

  • M 1010 - M1010
  • M 1234 - M1234
  • M 1234 A - M1234A
  • M 1234 B - M1234B
  • M 1265 - M1265
  • M 1265 A - M1265A
  • M 10230 - M10230
  • M 10230 A - M10230A
  • M 12456 - M12456
  • M 12456 A - M12456A
  • S 4056 - S4056
  • S 4056 A - S4056A

I am unsure of what is the best way to set up the get and set implementation of these properties. It seems as if the only property that needs to have a public get and set method is the original RdNumber property. The setting of this property should trigger the setting of the other 3 properties internally within the class. Then other 3 properties would only have a public get method for implementing the sorting. Putting aside the implementation of the IComparer for the moment, how should these 4 properties be set up so that the RdNumber property is assigned and the parsing takes place so that the other 3 properties can be returned?

it can be assumed that the minimum characters for a valid RdNumber is 5 and the maximum number of characters is 7. For parsing, all valid RdNumbers begin with a single character prefix that is followed by a minimum of 4 integer digits. A RdNumber that is 5 characters only would always contain a 4 digit integer numeric value and would not have a suffix (use "" for the suffix). A valid RdNumber which is 7 characters long would always contain a 5 digit integer numeric value and a suffix (a single letter). A RdNumber with 6 characters could either have a 4 digit integer numeric value and a letter suffix value or it could have a 5 digit integer numeric value and no suffix value ("").

Thanks for any help.

I have tried reading examples of how to set internal properties up, but I am not sure that I understand how to set it up so that the scope of the property variables is consistent for the set and the get methods of the internally set properties. In any case, I fear I am looking at examples that possibly are overcomplicating what I actually need to accomplish.

1 Answers1

0

This is what I came up with for a class that parses the RdNumber into 3 other properties. I am still interested in knowing if there are ways to make this code more elegant, but the code works:

public class RdNumberM
{
    private string rdnumber;
    public string RdNumber
    {
        get { return rdnumber; }
        set
        {
            rdnumber = value;
            int RdNoLen = rdnumber.Length;
            string numString = String.Empty;
            int number1 = 0;
            bool canConvert = false;
            switch (RdNoLen)
            {
                case 5:
                    RdNoPrefix = rdnumber.Substring(0, 1);
                    numString = rdnumber.Substring(1, 4);
                    canConvert = int.TryParse(numString, out number1);
                    if (canConvert == true)
                    {
                        RdNumeric = number1;
                    }
                    RdNoSuffix = "";
                    return;
                case 6:
                    RdNoPrefix = rdnumber.Substring(0, 1);
                    char suf = rdnumber.Last();
                    if (Char.IsDigit(suf))
                    {
                        numString = rdnumber.Substring(1, 5);
                        canConvert = int.TryParse(numString, out number1);
                        if (canConvert == true)
                        {
                            RdNumeric = number1;
                        }
                        RdNoSuffix = "";
                    }
                    else
                    {
                        numString = rdnumber.Substring(1, 4);
                        canConvert = int.TryParse(numString, out number1);
                        if (canConvert == true)
                        {
                            RdNumeric = number1;
                        }
                        RdNoSuffix = Char.ToString(suf); ;

                    }
                    return;
                case 7:
                    RdNoPrefix = rdnumber.Substring(0, 1);
                    numString = rdnumber.Substring(1, 5);
                    canConvert = int.TryParse(numString, out number1);
                    if (canConvert == true)
                    {
                        RdNumeric = number1;
                    }
                    RdNoSuffix = rdnumber.Substring(6, 1);
                    return;
                default:
                    RdNoPrefix = rdnumber;
                    RdNumeric = number1;
                    RdNoSuffix = "";
                    return;
            }
        }
    }
    public string RdNoPrefix { get; private set; }
    public int RdNumeric { get; private set; }
    public string RdNoSuffix { get; private set; }
}

This is the IComparer class I am using:

public class RdNumberComparer : IComparer<RdNumberM>
{
    public int Compare(RdNumberM x, RdNumberM y)
    {
        var result = x.RdNoPrefix.CompareTo(y.RdNoPrefix);

        if (result == 0)
        {
            result = x.RdNumeric.CompareTo(y.RdNumeric);
        }
        if (result == 0)
        {
            result = x.RdNoSuffix.CompareTo(y.RdNoSuffix);
        }
        return result;
    }
}