top of page

4_RA

  • hrafnulf13
  • Oct 23, 2020
  • 2 min read

Updated: Oct 28, 2020

While searching the ways to infer a suitable data type, I found only one suitable way to do it my application 7_A. The solution is presented in [1] and looks as follows:


private dataType ParseString(string str)
    {

        bool boolValue;
        Int32 intValue;
        Int64 bigintValue;
        double doubleValue;
        DateTime dateValue;

        // Place checks higher in if-else statement to give higher priority to type.

        if (bool.TryParse(str, out boolValue))
            return dataType.System_Boolean;
        else if (Int32.TryParse(str, out intValue))
            return dataType.System_Int32;
        else if (Int64.TryParse(str, out bigintValue))
            return dataType.System_Int64;
        else if (double.TryParse(str, out doubleValue))
            return dataType.System_Double;
        else if (DateTime.TryParse(str, out dateValue))
            return dataType.System_DateTime;
        else return dataType.System_String;

    }

The advantage of this approach is that it uses TryParse function that will not invoke any exceptions during the conversions to infer the data type of the variable.


Based on that function I created 2 classes: Variable and Value, and one enum type Variable_Type (Integer, Double, Boolean, String, DateTime)


Variable stores the general information of the values, such as:

  • m_name of the variable

  • m_default_type of the variable, that was defined during reading of the file

  • m_type, which is set to default type from the beginning but can be modified by user, so that all values of the variable will be changed upon changing it.

  • List of Values (objects)

  • So Variable class can be considered as a column with values

Value stores the general information of value itself, such as:

  • m_generic_value, which is initial value that was read from the file (used for restoring values, for example when casting from double to int and back).

  • m_value of the dynamic type. After reading the file is set to m_generic_value and converted to type specified by the m_type. So the type of the m_value changes when user changes m_type value during runtime.

  • m_default_type of the variable, that was defined during reading of the file and usually retrieved from the Variable object that stores that Value.

  • m_type, which is set to default type from the beginning but can be modified by user, so that all values of the variable will be changed upon changing it. Usually retrieved from the Variable object that stores that Value.

So by modifying [1] function, I created 2 functions for Variable and Value classes. After completing reading of the file, application invokes check_types() and calls check_type() for every Value within the list and sets m_value to infered data type.


public void check_types()
        {
            bool bool_value;
            int int_value;
            double double_value;
            DateTime date_value;

            if (bool.TryParse(values[0].m_value, out bool_value))
                set_default_type(Variable_Type.Boolean);
            else if (Int32.TryParse(values[0].m_value, out int_value))
            {
                bool check_double = false;
                foreach (Value val in values)
                {
                    check_double = val.check_type() == Variable_Type.Double;
                    if (check_double) break;
                }
                if(check_double) set_default_type(Variable_Type.Double);
                else set_default_type(Variable_Type.Integer);
            }           
            else if (double.TryParse(values[0].m_value, out double_value))
                set_default_type(Variable_Type.Double);
            else if (DateTime.TryParse(values[0].m_value, out date_value))
                set_default_type(Variable_Type.DateTime);
            else
            {
                bool check_double = false;
                bool check_int = false;
                foreach (Value val in values)
                {
                    check_double = val.check_type() == Variable_Type.Double;
                    check_int = val.check_type() == Variable_Type.Integer;
                    if (check_double) break;
                }
                if (check_double) set_default_type(Variable_Type.Double);
                else if(check_int) set_default_type(Variable_Type.Integer);
                else set_default_type(Variable_Type.String);
            }              
        }

public Variable_Type check_type()
        {
            bool bool_value;
            int int_value;
            double double_value;
            DateTime date_value;

       
            if (bool.TryParse(m_value, out bool_value))
                return Variable_Type.Boolean;
            else if (Int32.TryParse(m_value, out int_value))
                return Variable_Type.Integer;
            else if (double.TryParse(m_value, out double_value))
                return Variable_Type.Double;
            else if (DateTime.TryParse(m_value, out date_value))
                return Variable_Type.DateTime;
            return Variable_Type.String;
        }

 

References


  1. https://stackoverflow.com/questions/5311699/get-datatype-from-values-passed-as-string/5325687

Recent Posts

See All
14_RA

The Euler–Maruyama method (also called the Euler method) is a method for the approximate numerical solution of a stochastic differential...

 
 
 

Comments


bottom of page