casting from TypedValue with Type = (int)DxfCode.Bool

casting from TypedValue with Type = (int)DxfCode.Bool

Anonymous
Not applicable
1,288 Views
6 Replies
Message 1 of 7

casting from TypedValue with Type = (int)DxfCode.Bool

Anonymous
Not applicable
Hello,
I have successfully stored a boolean value into an Xrec using a TypedValue with Type of (int)DxfCode.Bool.
When I retrieve it the value is a 1 or 0 according to whether I had input True of False. So far, so good. The problem is that I am so far unable to cast this TypedValue.Value of 1 or 0 to either a bool or a Boolean or even an int or a long. How do I get that TypedValue.Value that is returned into another variable?
In the attached file what I am referring to is in the method
public bool getBoolVar(string VarName)

Thanks for your advice.
0 Likes
1,289 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable
You can't cast from an object to an int.

You can use Convert.ToInt32(), etc.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5768864@discussion.autodesk.com...
Hello,
I have successfully stored a boolean value into an Xrec using a TypedValue with Type of (int)DxfCode.Bool.
When I retrieve it the value is a 1 or 0 according to whether I had input True of False. So far, so good. The problem is that I am so far unable to cast this TypedValue.Value of 1 or 0 to either a bool or a Boolean or even an int or a long. How do I get that TypedValue.Value that is returned into another variable?
In the attached file what I am referring to is in
the method
public bool getBoolVar(string VarName)

Thanks for your advice.
0 Likes
Message 3 of 7

Anonymous
Not applicable
Hello, thanks for your reply. Well, I did figure out another way in the meantime. I am not sure why it works, but here it is:

I take the TypedValue.Value (which is an object), and set it equal to an Object variable. Then I did an explicit cast from there to Int16. Then I did an if-else to see if it was a 1 or 0 and set it True of False accordingly. Seems like a long way around, but I only have to write the code once, and I have.
0 Likes
Message 4 of 7

jbooth
Advocate
Advocate
If result is a boolean, and TypedValue.Value is an object:

result = Convert.ToInt32(TypedValue.Value) <> 0

Note that the method will throw an exception if the object can't be converted to an integer (it needs to implement IConvertible.ToInt32).
0 Likes
Message 5 of 7

joantopo
Mentor
Mentor

Hi.

I am in the same issue with DxfCode.Bool, so finally what is the best solution for this?

 

I am surfing on internet and I don´t find any thread related to this.

 

Thanks!

Autocad C3D 2019 SP3, 2020 & 2021
Intel I9 9900K with frontal watercooler alphacool eisbaer 360 (original fans mounted in pull)- 3 fans Corsair 120 ML PRO in push.
MOBO Gygabyte Z390 Aorus Master- Corsair RGB Vengeance 64GB RAM (4x16) CL16
Nvidia Quadro RTX 4000
Samsung 970 EVO PLUS 1TB (unit C). Samsung 970 PRO 512GB (for data)
Power Supply: Corsair TX850M PLUS


Descubre mi programa VisorNET para Civil 3D:
https://apps.autodesk.com/CIV3D/es/Detail/Index?id=appstore.exchange.autodesk.com%3avisornet_windows32and64%3aes
0 Likes
Message 6 of 7

Jeff_M
Consultant
Consultant

TypedValue[] tv = xrec.Data.AsArray();
returnValue = Convert.ToBoolean(tv[0].Value);

Jeff_M, also a frequent Swamper
EESignature
Message 7 of 7

joantopo
Mentor
Mentor

OK.

Although I have a bidimensional array:

 

TypedValue tdv2 = variables_globales.matriz[registro, 8]; //bool_BySubentity
int Bool_BySubentity =Convert.ToInt32(tdv2.Value);

 

So, I would have to do at least something to convert it to a single array.

 

With "Convert.ToInt32( )" it seems that it works..

Autocad C3D 2019 SP3, 2020 & 2021
Intel I9 9900K with frontal watercooler alphacool eisbaer 360 (original fans mounted in pull)- 3 fans Corsair 120 ML PRO in push.
MOBO Gygabyte Z390 Aorus Master- Corsair RGB Vengeance 64GB RAM (4x16) CL16
Nvidia Quadro RTX 4000
Samsung 970 EVO PLUS 1TB (unit C). Samsung 970 PRO 512GB (for data)
Power Supply: Corsair TX850M PLUS


Descubre mi programa VisorNET para Civil 3D:
https://apps.autodesk.com/CIV3D/es/Detail/Index?id=appstore.exchange.autodesk.com%3avisornet_windows32and64%3aes
0 Likes