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

0 Members and 1 Guest are viewing this topic.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • 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

Find me on https://parler.com @kblackie

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
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 »
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

Hanauer

  • Mosquito
  • Posts: 10
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: 16899
  • 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

Find me on https://parler.com @kblackie