Get UserVariableType from code

Hello,

Is it possible to get to know which kind of type (Boolean, Date,…) is a UserVariable direct from code?

So far I have tried it with Dom and ListLabel but I could not find it.

Thank you

Yes, it is possible. In short: Just pass the data source to List & Label and evaluate the UserVariables with the List & Label ExpressionEvaluator. The result will be the value and the type.

For example: the shown report uses a UserVariable of type String and another of type integer (see the icon of each UserVariable):

In your code you can open the report file using the DOM-API and and evaluate the content of the UserVariables. Have a look at the object named result. The type is “object {int}”.

Edit: I’ll add the code down below. It is much easier to copy than writing off the screenshot

        ProjectList proj = new ProjectList(LL);
        proj.Open(@"C:\temp\list.lst", LlDomFileMode.Open, LlDomAccessMode.ReadOnly, false);

        string formula1 = proj.UserVariables[1].Contents;

        object result = LL.EvaluateExpression(formula1, true);

Hallo Chistian!,

I think I did not explain my problem quiet well. What I want to get from the uservariablecontents is its LlFieldType (see picture)
grafik

Thank you again!

Once you have the type (see @crauchfuss’ reply) it’s quite easy to get the LlFieldType using a static helper. A user variable will not have the type “Date_MS” (as an example) anyway, it’s always a Date. Internally, a helper function that does this job looks like below, although you could make it easier if you don’t need all exotic edge cases:

protected static readonly System.Type ByteType = System.Type.GetType("System.Byte");
protected static readonly System.Type DecimalType = System.Type.GetType("System.Decimal");
protected static readonly System.Type DoubleType = System.Type.GetType("System.Double");
protected static readonly System.Type Int16Type = System.Type.GetType("System.Int16");
protected static readonly System.Type Int32Type = System.Type.GetType("System.Int32");
protected static readonly System.Type Int64Type = System.Type.GetType("System.Int64");
protected static readonly System.Type SByteType = System.Type.GetType("System.SByte");
protected static readonly System.Type SingleType = System.Type.GetType("System.Single");
protected static readonly System.Type UInt16Type = System.Type.GetType("System.UInt16");
protected static readonly System.Type UInt32Type = System.Type.GetType("System.UInt32");
protected static readonly System.Type UInt64Type = System.Type.GetType("System.UInt64");
protected static readonly System.Type StringType = System.Type.GetType("System.String");
protected static readonly System.Type CharType = System.Type.GetType("System.Char");
protected static readonly System.Type DateTimeType = System.Type.GetType("System.DateTime");
protected static readonly System.Type DateTimeOffsetType = System.Type.GetType("System.DateTimeOffset");
protected static readonly System.Type TimeSpanType = System.Type.GetType("System.TimeSpan");
protected static readonly System.Type BooleanType = System.Type.GetType("System.Boolean");

internal static LlFieldType GetFieldTypeFromDataType(Type dataType)
{
    if (dataType == null)
        return LlFieldType.Unknown;

    LlFieldType result = LlFieldType.Unknown;
    Type originalType = dataType;
    lock (_lockObject)
    {
        if (!_fieldTypeCache.TryGetValue(dataType, out result))
        {
#if !NETCORE_BUILD
            if (dataType.IsGenericType && dataType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
#else
            if (dataType.GetTypeInfo().IsGenericType && dataType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
#endif
            {
                NullableConverter nc = new NullableConverter(dataType);
                dataType = nc.UnderlyingType;
            }

            if ((dataType == StringType) ||
                (dataType == CharType))
                result = LlFieldType.Text;
            else if ((dataType == DateTimeType) ||
                        (dataType == TimeSpanType) ||
                        (dataType == DateTimeOffsetType))
                result = LlFieldType.Date;
            else if (dataType == BooleanType)
                result = LlFieldType.Boolean;
            else if ((dataType == ByteType) ||
                        (dataType == Int16Type) ||
                        (dataType == Int32Type) ||
                        (dataType == Int64Type) ||
                        (dataType == SByteType) ||
                        (dataType == UInt16Type) ||
                        (dataType == UInt32Type) ||
                        (dataType == UInt64Type))
                result = LlFieldType.Numeric_Integer;

            else if ((dataType == DecimalType) ||
                        (dataType == SingleType) ||
                        (dataType == DoubleType))
                result = LlFieldType.Numeric;
            else if (dataType.IsSubclassOf(typeof(Image)) || dataType == typeof(Image) || dataType == typeof(Byte[])) // || typeName == "System.Windows.Media.ImageSource")
                result = LlFieldType.Drawing;
            else if (dataType == typeof(Byte[]))
                result = LlFieldType.Drawing_hBitmap;
            else
                result = LlFieldType.Text;

            _fieldTypeCache.Add(originalType, result);
        }
    }
    return result;
}

Hello Jochen,

Thank you so much. Of course that was my plan B but I assumed ListLabel or Dom would already have a function like that one implemented. So before typing it by my own I rather ask.

I think It would be a good idea to have it already in LL. :slight_smile:

Thanks for your support

1 Like

You’re welcome - also to suggest such a function over at Idea Place.