none
Mapping table, best way. RRS feed

  • Question

  • Hello,
    I need to read out a mapping table and create a new string.
    Sample:
      26.02.2020
     
    Should be generating this   L2S
    Which way is the best for the problem?
      a) only by code
      b) maybe by file, so the user can change without need a new version.
      C) maybe a map ? or a datatable
    // -------------
    month	                print				
    1			1			
    2			2		 **	
    3			3			
    4			4			
    5			5			
    6			6			
    7			7			
    8			8			
    9			9			
    10			X			
    11			Y			
    12			Z			
    						
    // -------------
    	
    year/date	    print	
    1				1		
    2				2		
    3				3		
    4				4		
    5				5		
    6				6		
    7				7		
    8				8		
    9				9		
    10				A		
    11				B		
    12				C		
    13				D		
    14				E		
    15				F		
    16				G					
    17				H	
    18				J	
    19				K	
    20				L	**						
    21				M							
    22				N							
    23				P	
    24				Q	
    25				R	
    26				S	
    27				T	
    28				U	
    29				V	
    30				W	
    31				X

    Greetings Markus


    Wednesday, February 26, 2020 5:27 PM

Answers

  • Hi Markus Freitag,
    You can use Dictionary as a map and access a value in the map from a key.
    Here is a code example you can refer to.

    static void Main(string[] args)
    {  
        // Use Dictionary as a map.
        var monthMap = new Dictionary<string, string>();
        var yearMap = new Dictionary<string, string>();
        string[] month = { "1", "2", "3", "4", "5", "6","7","8","9","X","Y","Z"};
        string[] year= { "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X"};
        //  Add some keys and values.
        for (int i=0; i< month.Length; i++) {
            monthMap.Add((i+1).ToString(), month[i]);
        }
        for (int i = 0; i < year.Length; i++)
        {
            yearMap.Add((i + 1).ToString(), year[i]);
        }
        string input = "26.02.2020";
        char[] charSeparators = new char[] { '.' };
        string[] result = input.Split(charSeparators, StringSplitOptions.None);
        string monthResult;
                if (result[1].StartsWith("0"))
                {
                    monthResult = monthMap[result[1].Substring(1,1)];
                }
                else {
                     monthResult = monthMap[result[1]];
                }
        string dateResult = yearMap[result[0]];
        string yearResult = yearMap[result[2].Substring(0,2)];
        Console.WriteLine(yearResult+""+ monthResult+""+ dateResult);
        Console.ReadKey();
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 27, 2020 3:30 AM
  • Honestly, Daniel.  A dictionary is gross overkill for this task.

    using System;
    class Program
    {
        static string GetDateCode( int year, int month, int day )
        {
            const string mocode = "0123456789XYZ";
            const string yrcode = "0123456789ABCDEFGHJKLMNPQRSTUVWX";
            return yrcode[year % 100].ToString() + mocode[month].ToString() + yrcode[day].ToString();
        }
    
        static public void Main()
        {
            Console.WriteLine(GetDateCode(2020,02,26));
        }
    }
    
    


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by Markus Freitag Thursday, February 27, 2020 6:33 AM
    Thursday, February 27, 2020 5:34 AM

All replies

  • Come on, Markus.  This is a function with something like three lines of code.  You don't need someone to write this for you.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Wednesday, February 26, 2020 8:52 PM
  • Hi Markus Freitag,
    You can use Dictionary as a map and access a value in the map from a key.
    Here is a code example you can refer to.

    static void Main(string[] args)
    {  
        // Use Dictionary as a map.
        var monthMap = new Dictionary<string, string>();
        var yearMap = new Dictionary<string, string>();
        string[] month = { "1", "2", "3", "4", "5", "6","7","8","9","X","Y","Z"};
        string[] year= { "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X"};
        //  Add some keys and values.
        for (int i=0; i< month.Length; i++) {
            monthMap.Add((i+1).ToString(), month[i]);
        }
        for (int i = 0; i < year.Length; i++)
        {
            yearMap.Add((i + 1).ToString(), year[i]);
        }
        string input = "26.02.2020";
        char[] charSeparators = new char[] { '.' };
        string[] result = input.Split(charSeparators, StringSplitOptions.None);
        string monthResult;
                if (result[1].StartsWith("0"))
                {
                    monthResult = monthMap[result[1].Substring(1,1)];
                }
                else {
                     monthResult = monthMap[result[1]];
                }
        string dateResult = yearMap[result[0]];
        string yearResult = yearMap[result[2].Substring(0,2)];
        Console.WriteLine(yearResult+""+ monthResult+""+ dateResult);
        Console.ReadKey();
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 27, 2020 3:30 AM
  • I would say create simple file where user can define mappings, if you expect they might change based on application user. Or if you already have database for your application, then you can as well save mappings to database table. Of course to be more user friendly, your application might then require UI to allow user to edit mappings.

    Then when you have read mappings from data source you can use dictionary or hashtable to do mapping in-memory like based on Daniel's example.

    Thursday, February 27, 2020 5:32 AM
  • Honestly, Daniel.  A dictionary is gross overkill for this task.

    using System;
    class Program
    {
        static string GetDateCode( int year, int month, int day )
        {
            const string mocode = "0123456789XYZ";
            const string yrcode = "0123456789ABCDEFGHJKLMNPQRSTUVWX";
            return yrcode[year % 100].ToString() + mocode[month].ToString() + yrcode[day].ToString();
        }
    
        static public void Main()
        {
            Console.WriteLine(GetDateCode(2020,02,26));
        }
    }
    
    


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by Markus Freitag Thursday, February 27, 2020 6:33 AM
    Thursday, February 27, 2020 5:34 AM
  • Perfect Tim, three lines.

    Thanks!

    Thursday, February 27, 2020 6:33 AM
  • Hello Daniel,

    I would have solved it something like that. So I thought I'd ask and Tim came up with the perfect solution.

    Good to know, something like that will come back.

    Greetings Markus

    Thursday, February 27, 2020 6:38 AM