How to get the default Value (.defval) of Enumeration type?

Hi all,

maybe somebody can help me, I am pretty desperate...

I have an enumeration attribute type, let say  the colors: "white", "black", "red", "yellow" and so on , and the default enum value - "white".

I can manage all values of type no problems with the Type Property .strings [i].

My question is: how do I get here for such an "enumerated" attribute type the "default value" back, ie I need a property / method that returns me the string "white" in that case.

All recipes as assigning .defval to the string, for example, string s = ad.defval; print s - were bringing nothing at all. It returns always the Date "1970 01 January" . Thus the problem would be - how can I cast here the .defval type to my enumerated type "Colours"?

Any help will be extremely appreciated! Thank you in advance.


vjub58 - Fri May 22 14:41:33 EDT 2015

Re: How to get the default Value (.defval) of Enumeration type?
kourosh - Fri May 22 16:08:59 EDT 2015

I have an enumeration called "abc" with 'a', 'b', 'c' as enumerations and 'a' is the default value. This DXL correctly printed out 'a' as the default value:

 

AttrDef ad = find(current Module, "abc")
string s = ad.defval
print s

Re: How to get the default Value (.defval) of Enumeration type?
vjub58 - Sat May 23 11:08:03 EDT 2015

kourosh - Fri May 22 16:08:59 EDT 2015

I have an enumeration called "abc" with 'a', 'b', 'c' as enumerations and 'a' is the default value. This DXL correctly printed out 'a' as the default value:

 

AttrDef ad = find(current Module, "abc")
string s = ad.defval
print s

kourosh, thank you!

But I have the STRINGS "red", "white", "blue" etc as enumerations , and you have the LITERALS 'a', 'b', 'c'. Maybe it's the critical difference and it is exactly the point here.

Re: How to get the default Value (.defval) of Enumeration type?
ChrisHardy68 - Sun May 24 18:34:55 EDT 2015

vjub58 - Sat May 23 11:08:03 EDT 2015

kourosh, thank you!

But I have the STRINGS "red", "white", "blue" etc as enumerations , and you have the LITERALS 'a', 'b', 'c'. Maybe it's the critical difference and it is exactly the point here.

Hi

What Kourosh states is correct, however the problem I encountered is that the returned type string contained rich text so getting the actual value was an issue initially.

In the attached file see the function: getAllAttrs which should give you an idea of what to do. This is working code but will need modifying for your purposes.

Hope that helps

 

 

 


Attachments

attsandtypes.inc

Re: How to get the default Value (.defval) of Enumeration type?
Mathias Mamsch - Mon May 25 04:09:42 EDT 2015

If I understand you correctly you want to assign the "default" value to an object attribute?

In this case you just assign the null value to the attribute:

o."abc" = null

This will not assign empty, but the default value (or empty if there is no default value)

Note that the default value is an "attribute" property - i.e. all attributes, also int, date, etc. can be empty or have a default value.

Regards, Mathias

Re: How to get the default Value (.defval) of Enumeration type?
vjub58 - Wed May 27 02:07:02 EDT 2015

Mathias Mamsch - Mon May 25 04:09:42 EDT 2015

If I understand you correctly you want to assign the "default" value to an object attribute?

In this case you just assign the null value to the attribute:

o."abc" = null

This will not assign empty, but the default value (or empty if there is no default value)

Note that the default value is an "attribute" property - i.e. all attributes, also int, date, etc. can be empty or have a default value.

Regards, Mathias

No Matthias, I need to assign the default atribute value just TO VARIABLE , not to object attribute. And that is the point here.

My purpose is to export  the list of enumerations of the type to one CSV column, and the default value of the type  - to one another CSV column. I need both the enumerations list and the default value as a strings for that purpose.

Thank you in advance for any help! I read many your answers here in that forum, and these are always extremely professional and helpful.

Re: How to get the default Value (.defval) of Enumeration type?
Wolfgang Uhr - Wed May 27 04:28:05 EDT 2015

vjub58 - Wed May 27 02:07:02 EDT 2015

No Matthias, I need to assign the default atribute value just TO VARIABLE , not to object attribute. And that is the point here.

My purpose is to export  the list of enumerations of the type to one CSV column, and the default value of the type  - to one another CSV column. I need both the enumerations list and the default value as a strings for that purpose.

Thank you in advance for any help! I read many your answers here in that forum, and these are always extremely professional and helpful.

Hi

myObect.myAttributeName += myDefaultMember1;

myObect.myAttributeName += myDefaultMember2;

myObect.myAttributeName += myDefaultMember3;

// ...

myObect.myAttributeName -= myNonDefaultMember1;

myObect.myAttributeName -= myNonDefaultMember2;

myObect.myAttributeName -= myNonDefaultMember3;

 // ...

Best regards

Wolfgang

Re: How to get the default Value (.defval) of Enumeration type?
ChrisHardy68 - Wed May 27 19:31:49 EDT 2015

vjub58 - Wed May 27 02:07:02 EDT 2015

No Matthias, I need to assign the default atribute value just TO VARIABLE , not to object attribute. And that is the point here.

My purpose is to export  the list of enumerations of the type to one CSV column, and the default value of the type  - to one another CSV column. I need both the enumerations list and the default value as a strings for that purpose.

Thank you in advance for any help! I read many your answers here in that forum, and these are always extremely professional and helpful.

Hi

i think what i posted earlier should help.

However it might be easier if you attach your code and an example Module so we can see what the issue is.

Re: How to get the default Value (.defval) of Enumeration type?
llandale - Wed May 27 20:26:53 EDT 2015

Kourish and Chris are correct.  Below is the function I use to extract it.  here are some notes:

There is a Boolean "defval" property of an AttrDef as well as a string "defval" property.  My function below highlights how you are sure to get the correct property by using it in context (bool in the "if"; string in the Results assignment.

In your original code I'm guessing you have AutoDeclare on and failed to declare "s" as a string.  So for some reason "s" is a Date with a "null" value, which looks like 1970 01 January.

"AttType"s have "strings[I]".  "AttrDef"s are allowed to have default values.  There is no such thing as a "default" for your list of colors.  Once you define an attribute definition for it, such as "CarColors" of type "Colors", that can have a default value.  So you may need something like this:

AttrDef ad = find(mod, "CarColors")
AttrType at = ad.type
string Type = ad.typeName
get the at.strings[I]
get the default value for ad

-Louie


//**********************************************************
string fDefVal(AttrDef in_ad)
{ // Retrieve the default value, if any, for the attribute.
 // Return null if it has no default value.
 // Returns 'True' or 'False' for Booleans that have a Default.
 // Programs can determine IF an attr has a default value like this:
 // if (in_ad.defval){}

 if (null in_ad)  return("") // Bad Input
 if (!in_ad.defval) return("") // This attr has no default value

 string Results = in_ad.defval

 return(Results)
 // Note: Consider a Boolean attribute that true has a default value of False. 
 // Since 'in_ad.defval' can mean either [1] does it have a default value,
 // and [2] what that default value is, testing shows DXL should do this:
 // [1] >> Use [print (bool in_ad.defval)]   or [bool IsDef = in_ad.defval] or [if (in_ad.defval)]
 // [2] >> Use [print (string in_ad.defval)] or [string Value = in_ad.defval]

 // This also works, but not recommended:
 // AttrDef in_ad = find(current Module, "aBool")
 // string DefVal = in_ad.(ADADefault_ defval)
 // bool   DefHas = in_ad.(ADABool_    defval)
 // print DefVal "\t" DefHas "\n"

} // end fDefVal()

Re: How to get the default Value (.defval) of Enumeration type?
vjub58 - Fri May 29 10:27:10 EDT 2015

Thank you so much to all of you! Now I know at last the workaround.

The code like:

===========================================

AttrDef ad = find (current Module, "Attribute_Name")

bool hasdef = ad.defval

string defStr = "has no default value"

if (hasdef) defStr = ad.defval

print defStr

==========================================

DOES WORK.

The code like:

===========================================

AttrDef ad = find (current Module, "Attribute_Name")

string defStr = "has no default value"

if (ad.defval) defStr = ad.defval

print defStr

==========================================

DOES NOT WORK.

The tested bool value ad.defval (whether ad has default value) should be assigned to separate variable. Otherwise it has been interpreted as bool at assigning of af defval to string.

Re: How to get the default Value (.defval) of Enumeration type?
llandale - Mon Jun 01 18:59:46 EDT 2015

vjub58 - Fri May 29 10:27:10 EDT 2015

Thank you so much to all of you! Now I know at last the workaround.

The code like:

===========================================

AttrDef ad = find (current Module, "Attribute_Name")

bool hasdef = ad.defval

string defStr = "has no default value"

if (hasdef) defStr = ad.defval

print defStr

==========================================

DOES WORK.

The code like:

===========================================

AttrDef ad = find (current Module, "Attribute_Name")

string defStr = "has no default value"

if (ad.defval) defStr = ad.defval

print defStr

==========================================

DOES NOT WORK.

The tested bool value ad.defval (whether ad has default value) should be assigned to separate variable. Otherwise it has been interpreted as bool at assigning of af defval to string.

I may be wrong here and you will be doling me a favor to prove it; but I'm pretty sure this line will work:

  • if (ad.defval) defStr = ad.defval

The "if" statement requires a Boolean parameter and the interpreter should find the Boolean version of "ad.defval" to accommodate it.  If it found and returned the string version they you would end up with a run time error:

  • if ("This String") then "Doesn't make sense"

The second "ad.defal" should be interpreted as a string since it is being assigned to a string variable.  that presumes AutoDeclare is OFF and you have declared "string defStr" above.

Actually the following is technically perfectly save although overkill; which forces the issue directly:

  • if ((bool ad.defval)) defStr = (string ad.defval)

With AutoDeclare left ON and no declaration, the following is I believe unreliable:

  • defStr = ad.deval

Is "defStr" a string or bool?  IIRC, in DOORS v6 it was consistently a "bool" but in v7 it changed to a "string" ... most of the time.

Finally consider the following; for a bool attribute:

  • string defStr = (bool ad.defval) ""