Setting Default Values in New Entities
Note: This topic demonstrates how to manually create default values for new entities in business objects using parameter objects. We highly recommend using the Business Layer Generator to generate default value objects and their associated code. For more information, see the topic Using the MM .NET Business Layer Generator.
Using Parameter Objects to Specify Default Values
The NewEntity method of mmBusinessObject is used to add a new entity to the business object. The following overloads of NewEntity accept a "default values" parameter object:
public virtual EntityType NewEntity(object defaultValues)
public virtual EntityType NewEntity(object defaultValues, bool addToEntityList)
Note: For an example of setting default values, check out the MM .NET Windows Forms sample applications' OrderDetailDefaults object, as well as the New button associated with the Customer Orders Form's Order Detail DataGridView.
Creating Default Value Parameter Objects
You can create a custom default value parameter object for each business object in your application. This parameter object should have public fields or properties that hold default values. For example, the following OrderDefaultValues class has two public fields, CustID and ProductID which can hold default values for an Orders business object.In C#:
public class OrderDefaultValues { public int CustID = 0; public int ProductID = 0; public OrderDefaultValues(int custID, int productID) { this.CustID = custID; this.ProductID = productID; } }
And in VB .NET:
Public Class OrderDefaultValues Public CustID As Integer = 0 Public ProductID As Integer = 0 Public Sub New(custID As Integer, productID As Integer) Me.CustID = custID Me.ProductID = productID End Sub 'New End Class
Notice the constructor method accepts parameters that can be used to pass default values when instantiating the class. The next section shows how you can pass these parameters during instantiation.
Note: If you have minimal requirements for default values (meaning a small memory footprint), you may want to consider using a structure rather than a class.
Passing a Default Value Parameter Object to the NewEntity Method
The following code shows how you can call one of the business object overloads of the NewEntity method, passing an instance of the OrderDefaultValues parameter object:In C#:
order.NewEntity(new OrderDefaultValues(1009, 111160));
And in VB .NET:
order.NewEntity(New OrderDefaultValues(1009, 111160))
Notice in this call to the NewEntity method that a new instance of OrderDefaultValues is passed as a parameter. In the constructor of OrderDefaultValues, the customer ID 1009 and product ID 111160 are passed as parameters. As shown in the code at the beginning of this topic, these values are then stored to the CustID and ProductID public fields of the OrderDefaultValues object.
Within the NewEntity method the default values parameter object is stored in the protected DefaultValues field of the business object and can be accessed from within other methods as described in the following section.
Setting Default Values within the HookSetDefaultValues Method
There's one final step required for setting default values in a new entity. You must override your business object's HookSetDefaultValues, and copy the default values into the new entity. For example:In C#:
protected override void HookSetDefaultValues(OrderDetailEntity entity) { if (this.DefaultValues != null) { // Store the dynamic default values via the entity object entity.OrderID = Defaults.OrderID; } }
And in VB .NET:
Protected Overrides Sub HookSetDefaultValues(entity As OrderDetailEntity) If Not (Me.DefaultValues Is Nothing) Then ' Store the dynamic default values via the entity object entity.OrderID = Defaults.OrderID End If End Sub
Notice the HookSetDefaultValues method receives an entity parameter. The business object's NewEntity() method automatically calls this hook method when a new entity is added, passing the new entity as a parameter.
Calling Form-Level NewEntity Methods with Default Value Objects
MM .NET business form classes contain overloads of the NewEntity methods that accept default value objects. You can call these methods from within your application forms.For example, in C#:
this.NewEntity(new OrderDefaultValues(custID, productID));
And in VB .NET:
Me.NewEntity(New OrderDefaultValues(custID, productID))
© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 07/28/18
Comment or report problem with topic
