Author Topic: casting xData.value to enum head scratcher  (Read 287 times)

0 Members and 1 Guest are viewing this topic.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16750
  • Superior Stupidity at its best
casting xData.value to enum head scratcher
« 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. }

Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

MickD

  • Gator
  • Posts: 3357
  • (x-in)->[process]->(y-out)
Re: casting xData.value to enum head scratcher
« Reply #1 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
« Last Edit: March 23, 2020, 05:12:19 PM by MickD »
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

Lao Tzu: “To attain knowledge, add things
every day; to obtain wisdom, remove things every day.”

Hanauer

  • Mosquito
  • Posts: 2
Re: casting xData.value to enum head scratcher
« Reply #2 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
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);
 }
« Last Edit: March 23, 2020, 07:05:50 PM by Hanauer »

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16750
  • Superior Stupidity at its best
Re: casting xData.value to enum head scratcher
« Reply #3 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.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal