Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing numeric Value stored in object >= to another object
    primarykey
    data
    text
    <p>In my program, i'm reading SchemData from a table. I need to determine the column size and decide at runtime if a given value matches the column size and could be written to that column. In case of alpha-numeric Types like char, nvarchar,... this is no problem.</p> <p>But in case of numeric values i cannot compare the value with the column size, because column size will give me the amount of bytes to store inside that column, if my understanding here is correct.</p> <p>So i want to check, if my numeric values are inside the MaxValue range of that particular data type stored inside a System.Type variable of that column.</p> <p>I started with determining the MaxValue using reflection and also recognizing nullable types like that:</p> <pre><code>public static Object GetMaxValue(this Type type) { var t = GetNullableType(type); var f = t.GetField("MaxValue"); if (f == null) return null; else return f.GetValue(null); } public static Type GetNullableType(Type type) { Type retType = type; if (type.IsGenericType &amp;&amp; type.GetGenericTypeDefinition() == typeof(Nullable&lt;&gt;)) { var nullableConverter = new System.ComponentModel.NullableConverter(type); retType = nullableConverter.UnderlyingType; } return retType; } </code></pre> <p>Now i get an object, storing the MaxValue information.</p> <p>How can i compare the MaxValue stored inside an object with another value, stored inside another object (or maybe a string). The value inside the second object (or string, as mentioned before) are read from a xml file, therefor this is not a specific type like int. It needs to be from type object.</p> <p>The only thing to solve the comparison thing i thought of was implementing a method and checking for every single numeric type inside a switch and performing a try parse and return true/false.</p> <p>First example method looks like this:</p> <pre><code> public static bool TestMaxValue(this Type type, object compare) { var t = GetNullableType(type); var mv = t.GetMaxValue(); bool ret = false; switch (Type.GetTypeCode(t)) { case TypeCode.Byte: { Byte b; if (Byte.TryParse(compare.ToString(), out b)) ret =(Convert.ToByte(mv) &gt;= b); break; } case TypeCode.Decimal: { Decimal b; if (Decimal.TryParse(compare.ToString(), out b)) ret = (Convert.ToDecimal(mv) &gt;= b); break; } case TypeCode.Double: { Double b; if (Double.TryParse(compare.ToString(), out b)) ret = (Convert.ToDouble(mv) &gt;= b); break; } case TypeCode.Int16: { Int16 b; if (Int16.TryParse(compare.ToString(), out b)) ret = (Convert.ToInt16(mv) &gt;= b); break; } case TypeCode.Int32: { Int32 b; if (Int32.TryParse(compare.ToString(), out b)) ret = (Convert.ToInt32(mv) &gt;= b); break; } } return ret; } </code></pre> <p>Does anyone have a better idea than implementing such a method? Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COI am not sure about the kind of storage you are relying on (and the kind of widths you are intending to measure) but, in most of the cases (all what I know), the max./min. values are defined as function of the given type. That is, if the type is a string, by its length (from length 1 to length 100), if it is an integer by integers (1 to 1000), etc. If, for example, you want to store a numeric value into a string column, you would be forced to convert it into string and thus the string rules (for length) will be applied anyway. Did I miss something?
      singulars
    2. COI want to store a numeric value inside a numeric column. The data from xml File i read is unknown to me. I will get a path to a xml file and read it. The data i get there needs to be stored inside a table and every single value needs first to be checked, if it could be stored inside the matching column. Therefore i need to determine the data type of that column (i get it from GetSchemaTable()) and test the MaxValue of this Type against the incoming value from xml file. I hope i could describe as precise as possible, otherwise just ask again :).
      singulars
    3. COIntending to perform any kind of operation (storage or any other thing) without knowing the type you are dealing with is not something recommendable at all (to say it "softly"). You shouldn't do that in any case. What you can do always is relying on strings (and on their lengths for size checkings). But still it does not seem to make too much sense to store something you don't know its type, this is a basic defining feature of the given bit of information. You should either store the types together with the info or "guess them"; knowing numeric types, for example, is straightforward.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload