StrataFrame Forum

Enum data type

http://forum.strataframe.net/Topic5670.aspx

By kkchan - 1/8/2007

Hi,

May I know what is the data type used by enum? I have a combobox that use enum as population. It hits error if I bind it to a integer field.



System.InvalidCastException was unhandled by user code

Message="Specified cast is not valid."

Source="InventoryBusinessObject"

StackTrace:

at JK.Inventory.BusinessObject.CompanyAddressBO.get_AddressTypeID() in F:\Projects\Inventory\InventoryBusinessObject\CompanyAddressBO.Designer.cs:line 321

at JK.Inventory.BusinessObject.CompanyAddressBO.Field_AddressTypeID_Descriptor.GetValue(Object component) in F:\Projects\Inventory\InventoryBusinessObject\CompanyAddressBO.Designer.cs:line 751

at System.Windows.Forms.BindToObject.GetValue()

at System.Windows.Forms.Binding.PushData(Boolean force)

By StrataFrame Team - 1/8/2007

When you populate a combo box from an enum, you have to bind the combo box to a field that is typed as the same enum (through the field customization in the business object mapper).  When the combo box is populated, it does not use an integer type as the value for each item within the combo box, but rather the actual enum value.  So, you have to bind to a field that uses that same enum value or the data-types do not match.
By kkchan - 1/9/2007

Hi,

Thank you for reply.

However, I still unable to make it.



I declared enum, able to populate combobox by refer to enum, set it as custom data field type in BO mapper (able to search from data type browse dialog). However, when I click new button, I still hit error.



CompanyAddressBO.cs

namespace JK.Inventory.BusinessObject

{

public enum AddressType { Corresponding, Shipping, Billing };



[Serializable()]

public partial class CompanyAddressBO : MicroFour.StrataFrame.Business.BusinessLayer

{

}

}



CompanyAddressBO.designer.cs



namespace JK.Inventory.BusinessObject

{

public enum AddressType { Corresponding, Shipping, Billing };



[Serializable()]

public partial class CompanyAddressBO : MicroFour.StrataFrame.Business.BusinessLayer

{



[Browsable(false),

BusinessFieldDisplayInEditor(),

Description("This field was imported."),

DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

public JK.Inventory.BusinessObject.AddressType AddressTypeID

{

get

{

// ERROR HERE!!!!!!

return (JK.Inventory.BusinessObject.AddressType)this.CurrentRow["AddressTypeID"];

}

set

{

this.CurrentRow["AddressTypeID"] = value;

}

}

}

}





Error:

System.InvalidCastException was unhandled by user code

Message="Specified cast is not valid."

Source="InventoryBusinessObject"

StackTrace:

at JK.Inventory.BusinessObject.CompanyAddressBO.get_AddressTypeID() in F:\Projects\Inventory\InventoryBusinessObject\CompanyAddressBO.Designer.cs:line 321

at JK.Inventory.BusinessObject.CompanyAddressBO.Field_AddressTypeID_Descriptor.GetValue(Object component) in F:\Projects\Inventory\InventoryBusinessObject\CompanyAddressBO.Designer.cs:line 751

at System.Windows.Forms.BindToObject.GetValue()

at System.Windows.Forms.Binding.PushData(Boolean force)





Thank you
By StrataFrame Team - 1/9/2007

Do you have the enum defined in 2 different locations?  Within both the CompanyAddressBO.cs and the CompanyAddressBO.designer.cs files?  It is strange that the compiler would let you compile it with the same type defined in 2 different locations.  Try deleting one of them and running it again... most likely, the combo box is being populated from one of them while the business object is casting it's enum as the other type causing your cast exception.
By kkchan - 1/9/2007

Hi,

I moved enum to enumeration.cs and try again.. still the same.

FYI, I typed the code below in editor and VS.NET able to show me the enum list. But when I search enum from BO mapper custom data type search, it doesn't list anything. It just return what I type back to the column.



this.companyaddressbo.addresstypeID =



Really strange



BTW, my field is INT field type. Any impact?


By kkchan - 1/10/2007

Hi,

Sorry to trouble you again.

After some debugging, when I call myBO.Add(), VS.NET shown my field value as {} which I guest is NULL. Therefore, .NET complaint when read the property which also cast field value to enum data type.



I got set value to "NULL Replacement Value" under business object mapper but it doesn't seem to work. Any ideas?



At last, what I did is explicit set default value to myField in myBO_SetDefaultValues event and it solves the problem.



Please advice.



Thank you
By kkchan - 1/10/2007

Hi,

Sorry, one more question here. I found that only integer field shown as {} but string is "" when add new record. Is it .NET "feature" or ...?



Thank you
By StrataFrame Team - 1/10/2007

When you add a new record, SF initializes all of the fields within the record so that there are no DBNull.Value fields.  This functionality can be turned off on the business object by setting the AllowNullValuesOnNewRow property to True on the business object.  Most likely, your enum did not have a value for 0, which is what the integer fields are initialized to.  So, but, explicitly setting the value for the field within SetDefaultValues() will always work.
By kkchan - 1/10/2007

Hi,

My enum do have 0 value



public enum myEnum {Billing, Corresponding, Home};
By StrataFrame Team - 1/11/2007

Hi,
My enum do have 0 value

public enum myEnum {Billing, Corresponding, Home};

I'm not sure then... I'd have to see it at runtime and test several things within the Watch window.  I'm glad you got it working, though, with your explicit default value.