Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Attributes and Choices

Vote:
 

I'm using various attributes for my IUser object to record extra information for each user. And I need to set a boolean value for each attribute, as to decide at runtime wether this attribute shoud be displayed or not. And I do not understand the point of Choice on the attributes, and the programmers guide does not mention this.  What is Choice supposed to do for an attribute? Can I make for example a string attribute and define several strings as "valid" choices? And how do I access these choices?

I was kind of hoping that I could have mye attribute sting, and an additional choice attached to this attribute, in stead of making a string and bool attribute for each attribute value I need. example: if(user.GetAttributeValue<string>("attribute").GetChoice<bool>("attribute_choice")... Kind of thing

Any help, examples etc are most welcome!

#24753
Oct 07, 2008 13:05
Vote:
 

Hi Marius!

I think you have misunderstood the use of choices on an attribute. A choice is a predefined alternative for the attribute, and will always be of the same type as the attribute, the choice will also have a name that is always of type String. With that said, it is also important to point out the value for an attribute is not limited to its choices.

I will use an example to illustrate. Say that you have an attribute named "Car Make" of type String. Now you may want to attach three choices to this attribute (these will also be of type String according to above):

  1. Name: Saab, Value: Saab
  2. Name: Volvo, Value: Volvo
  3. Name: Other, Value: Other 

When a user is supposed to input which car make he/she drives, you present a drop down list with the choices. In the case where the user selects "Other", maybe you want to show a free text field to input another car make. Say that the user inputs "Audi", then it will be OK to store the value "Audi" in the "Car Make" attribute, even though it is not among the choices.

So in you can use the choices in the manner you want to. As I see it you have two ways to solve your problem:

  1. (More elegant, but more work) Create your own custom attribute that wraps the type you want and that also have a boolean show/hide property on it
  2. (Less work, slightly less elegant but still ok) Create two attributes, one for the actual type you would like to store, and one boolean to indicate if the former attribute should be shown or not

I hope this helps you to a solution!

//Tom

ps. It is worth mentioning that the administrative interface at the moment only handles choices of primitive types.

#24844
Oct 08, 2008 10:29
Vote:
 

Ok, thanks for the enlightenment Tom! Smile This gave me an opportunity to refactor some less good code, and I want to use the attribute choices in a dropdownlist, but the GetAttributes method returns nothing. My code looks like this:

DropDownList1.DataSource = user.GetAttributeValues<string>("UserListOfAttributeChoises");
DropDownList1.DataBind();

The attribute is UserListOfAttributes, an attribute attached to the IUser object, it is a list of string choices.

What am I missing?

#25114
Oct 14, 2008 11:20
Vote:
 

Hi again,

The code that you have done actually gets all the values for a specific attribute. This method is used when you have an attribute that a user can enter several values for, for instance a multiselect listbox.

The method you are looking for to get hold of the choices is:

user.GetAttribute("yourAttributeName").Choices;

The "Choices"  property will give you a AttributeValueChoiceCollection, where the "Text" property is the name of the choice.

//Tom

#25124
Oct 14, 2008 13:49
Vote:
 

Ah, thanks a lot, this did clear things up a bit :)

Made myself a little method that gets all my choices as a list of strings:

public static List GetAttributes(StarCommunity.Core.Modules.Security.IUser user, string attributeName) {     	List list = new List(); 	IAttribute attr = CurrentUser.GetAttribute(attributeName); 	foreach (AttributeValueChoice choice in attr.Choices) 	{ 		list.Add(choice.Text); 	} 	return list; }
#25202
Oct 15, 2008 9:43
Vote:
 

Great! Laughing

//Tom 

#25213
Oct 15, 2008 11:05
This thread is locked and should be used for reference only. Please use the Legacy add-ons forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.