Return to Snippet

Revision: 66411
at May 1, 2014 04:13 by JordanRowles


Updated Code
public class DictionaryToDB {
    public Dictionary<string, dynamic> MyDictionary = new Dictionary<string, dynamic>{ };

    public void AddToDictionary(string key, dynamic value) {
        if (
            (key == "hs_tag" || key == "host_pc" && value.Length <= 10) ||
            (key == "mac" && value.Length <= 17 || key == "ip" && value.Length <= 25 || key == "wireless_mac" && value.Length <= 20) ||
            (key == "disks" || key == "cpu" || key == "memory" || key == "graphics" && value.Length <= 25) ||
            (key == "name" || key == "manufacturer" || key == "model" || key == "serial_no" || key == "oem_os_key" || key == "allocated_to" || key == "bios" && value.Length <= 50) ||
            (key == "comment" && value.Length <= 255)) {
            MyDictionary.Add(key, value);
            MessageBox.Show("It worked!", "Success!");
        }
        else {
            MessageBox.Show("It Failed!", ":(");
        }

    }
}

Revision: 66410
at May 1, 2014 03:50 by JordanRowles


Initial Code
public class DictionaryToDB {
    public Dictionary<string, dynamic> MyDictionary = new Dictionary<string, dynamic>{ };

    public void AddToDictionary(string key, dynamic value) {
        if (
            (key == "hs_tag" || key == "host_pc" && value.Length <= 10) ||
            (key == "mac" && value.Length <= 17 || key == "ip" && value.Length <= 25 || key == "wireless_mac" && value.Length <= 20) ||
            (key == "disks" || key == "cpu" || key == "memory" || key == "graphics" && value.Length <= 25) ||
(key == "name" || key == "manufacturer" || key == "model" || key == "serial_no" || key == "oem_os_key" || key == "allocated_to" || key == "bios" && value.Length <= 50) ||
            (key == "comment" && value.Length <= 255)) {
            MyDictionary.Add(key, value);
            MessageBox.Show("It worked!", "Success!");
        }
        else {
            MessageBox.Show("It Failed!", ":(");
        }

    }
}

Initial URL


Initial Description
Logic to make sure that the input through window controls (text boxes, drop down selections ...) were the correct length for the database table. 

I.e.
If the user entered 'abcdefghijk' into the 'HS Asset Tag' (hs_tag) text box, it will reject it because the table column hs_tag is set to VARCHAR(10) - a string with a length of ten.

======================================================
LINE   |   EXPLAINATION
---------------------------------------------------------------------------------------------------
  01          Creates a public class (container object) called DictionaryToDB.
                This is because it is taking the contents of a dictionary (key-value
                pair) and putting it into a database table.
---------------------------------------------------------------------------------------------------
  02         Create the dictionary object (another container type). 'string' means
                that the key in the dictionary must be a string (a sentence or word
                encapsulated by " "). 'dynamic' means that the datatypes can be
               different, you can have integers, strings, lists and objects in the
               same dictionary. This is useful because database table rows have a
              specific datatype attributed to them.
---------------------------------------------------------------------------------------------------
  04       I create a method/function that's public and doesn't return a value
              (void) called AddToDictionary, because it will perform the logic and
             add the keys and values (which we get from the windows form). It
             takes two arguments. Which is called whenever we want to use that
             method on some data. It becomes an object (method) inside another
             object (Class) - encapsulation.
            Used like:
                                      AddToDictionary("hs_tag", "02535")
---------------------------------------------------------------------------------------------------
05-13 
            05 - It starts as an if statement, it does something if the condition given
                    is either true or false. For example:
                              if (MyName == "Jordan") {
      06-10 - This is the basic layout of what you are seeing
                      == is equal to      || is 'or'      && is 'and'     <= is less than or equal to

                    if (
                              (columns with 10 or less characters)  or
                              (columns with mixed length) or
                              (columns with 25 or less characters) or
                              (columns with 50 or less characters) or
                              (columns with 255 or less characters)
                              = The users input   then
                                    Add the key and value to the dictionary


That's the basics of it. Still needs a little more work and need to add the other columns, with different datatypes like DATETIME and INT.

Initial Title
Logic used in internal Licensing program

Initial Tags


Initial Language
C#