The requested page is not available for the requested platform. You are viewing the content for Default platform.

Initialize a Property After Creating an Object (EF)

  • 3 min to read

In this lesson, you will learn how to set the default value for a particular property of a business class. For this purpose, the Priority property will be added to the DemoTask class created in the Set a Many-to-Many Relationship (EF) lesson. To initialize it, assign a value to this property in the constructor.

Note

Before proceeding, take a moment to review the following lessons:

  • Add the Priority property to the DemoTask class and declare the Priority enumeration, as shown below:

    namespace MySolution.Module.BusinessObjects {
        //...
        public class DemoTask : Task {
            //...
            public Priority Priority { get; set; }
        }
    
        public enum Priority {
            Low = 0,
            Normal = 1,
            High = 2
        }
    }
    
  • In the Module.cs (Module.vb) file, register the Priority enumeration type in the constructor of your ModuleBase descendant using the EnumProcessingHelper.RegisterEnum method as follows.

    using DevExpress.Data.Filtering;
    //...
    public sealed partial class MySolutionModule : ModuleBase {
        public MySolutionModule() {
            InitializeComponent();
            EnumProcessingHelper.RegisterEnum(typeof(MySolution.Module.BusinessObjects.DemoTask.Priority));
            //...
        }
        //...
    }
    
  • Use the code below to initialize the newly added Priority property when a DemoTask object is created.

    public class DemoTask : Task {
        public DemoTask() : base() {
            //...
            Priority = Priority.Normal; 
        }
        //...
    }
    

    The constructor will be executed when the new DemoTask object is created. As a result, the Priority property will be initialized with the specified value.

  • Run the WinForms or ASP.NET application. Create a new DemoTask object by selecting DemoTask in the drop-down list of the New (new_dropdown_btn) button. (In the Detail View that represents the newly created DemoTask object, note that the Priority property is set to Normal, as declared in the code above.) Notice that the enumeration property is automatically displayed by the combo box editor.

    Tutorial_BMD_Lesson12_1

You can see the code demonstrated in this lesson in the MySolution.Module | Business Objects | DemoTask.cs (DemoTask.vb) file of the EF Demo (Code First) installed with XAF. By default, the EF Demo (Code First) application is installed in %PUBLIC%\Documents\DevExpress Demos 19.1\Components\eXpressApp Framework\EFDemoCodeFirst.

 

Next Lesson: Implement Dependent Reference Properties (EF)