TheSwamp

Code Red => .NET => Topic started by: Keith™ on March 23, 2020, 04:41:56 PM

Title: casting xData.value to enum head scratcher
Post by: Keith™ on March 23, 2020, 04:41:56 PM
It compiles
It seems 100% legit
It crashes without raising an exception

Documentation says TypedValue 1070 is Int32
I can Convert.ToInt32(tv.Value) without issue and then cast to an enum

enum is Int32

It's a head scratcher for me for sure

(code abbreviated for clarity)


Code - C#: [Select]
  1. //get the stored enum status data from the object
  2. statusEnum st = statusEnum.none; //=0
  3. foreach (TypedValue tv in obj.ResultBuffer)
  4. {
  5.     if (tv.TypeCode == 1070)
  6.         st = (statusEnum)tv.Value;
  7. }

Title: Re: casting xData.value to enum head scratcher
Post by: MickD on March 23, 2020, 05:09:17 PM
have you looked into DxfCodes? This is the enumeration for TypedCodes

maybe you can try ((DxfCode)tv.TypedCode== 1070) as your condition?

btw the doc's show all codes being a short or Int16  therefore DxfCode.ExtendedDataInteger16 = 1070 (or 0x42e)

hth
Title: Re: casting xData.value to enum head scratcher
Post by: Hanauer on March 23, 2020, 06:08:22 PM
Have you looked here: https://adndevblog.typepad.com/autocad/2012/12/store-and-retrieve-enum-values-in-a-resbuf-using-net.html (https://adndevblog.typepad.com/autocad/2012/12/store-and-retrieve-enum-values-in-a-resbuf-using-net.html)
Try change:
Code: [Select]
st = (statusEnum)tv.Value;
with
Code: [Select]
st = (statusEnum)Enum.ToObject(typeof(statusEnum), tv.Value);
or with
Code: [Select]
st = (statusEnum)tv.Value;
use DxfCode.ExtendedDataInteger32 (1071).
Here's what I use:
Code: [Select]
GhAcStatusEnum st = GhAcStatusEnum.Existente;
if (tv.TypeCode == (int)DxfCode.ExtendedDataInteger16)
{
    // Only works if the enum value in
    // the Xdata was stored as ExtendedDataInteger32 (1071)   
    // GhAcStatusEnum st = (GhAcStatusEnum)tv.Value;
                                   
    // Works in both the cases
    // if the enum value in the Xdata was
    // stored as ExtendedDataInteger16 (1070)
    // or as ExtendedDataInteger32 (1071)                                     
    st = (GhAcStatusEnum)Enum.ToObject(typeof(GhAcStatusEnum), tv.Value);
 }
Title: Re: casting xData.value to enum head scratcher
Post by: Keith™ on March 23, 2020, 07:33:58 PM
Ah .. so perhaps I'll give the 1071 group a try. Essentially I need to store an enum in xdata and retrieve it at a later date. enum is Int32 so that could possibly be the issue. I'll give it a shot and see what happens.