Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Defaults: mmBusinessObject.NewRow(object defaultValues)
Message
General information
Forum:
ASP.NET
Category:
The Mere Mortals .NET Framework
Miscellaneous
Thread ID:
00839959
Message ID:
00842547
Views:
19
Patrick,

I spoke with Jason today (one of our highly valued MM .NET MVPs <s>), and here's an excerpt from our upcoming Dev Guide:

Setting Default Values in New Rows


The NewRow method of mmBusinessObject is used to add a new DataRow to the business object's current DataSet. The following overloads of NewRow accept a "default values" parameter object:

In C#:
public virtual DataRow NewRow(object defaultValues)
public virtual DataRow NewRow(string tableName, object defaultValues)
public virtual DataRow NewRow(DataSet ds, string tableName, object defaultValues)
public virtual DataRow NewRow(DataView dv, object defaultValues)
And in VB .NET:
Public Overridable Function NewRow(defaultValues As Object) As DataRow
Public Overridable Function NewRow(tableName As String, defaultValues As Object) As DataRow
Public Overridable Function NewRow(ds As DataSet, tableName As String, defaultValues As Object) As DataRow
Public Overridable Function NewRow(dv As DataView, defaultValues As Object) As DataRow

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.

Passing a Default Value Parameter Object to the NewRow Method


The following code shows how you can call one of the business object overloads of the NewRow method, passing an instance of the OrderDefaultValues parameter object:

In C#:
oOrder.NewRow(new OrderDefaultValues(1009, 111160));
And in VB .NET:
oOrder.NewRow(New OrderDefaultValues(1009, 111160))
Notice in this call to the NewRow 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 theOrderDefaultValues object.

Within the NewRow 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 DataRow. You must override your business object's HookSetDefaultValues, and copy the default values into the new DataRow. For example:

In C#:
public class Orders : ABusinessObject
{
	protected override void HookSetDefaultValues(System.Data.DataRow dataRow)
	{
		// Cast the DefaultValues object to the custom type
		OrderDefaultValues Defaults = (OrderDefaultValues)this.DefaultValues;

		// Store the default values in the new DataRow
		dataRow["CustomerID"] = Defaults.CustID;
		dataRow["ProductID"] = Defaults.ProductID;
	}
}
And in VB .NET:
Public Class Orders
   Inherits ABusinessObject
   
   Protected Overrides Sub HookSetDefaultValues(dataRow As System.Data.DataRow)

	' Cast the DefaultValues object to the custom type
      Dim Defaults As OrderDefaultValues = CType(Me.DefaultValues, OrderDefaultValues)
	
	' Store the default values in the new DataRow
      dataRow("CustomerID") = Defaults.CustID
      dataRow("ProductID") = Defaults.ProductID

   End Sub

End Class
Notice the HookSetDefaultValues method receives a DataRow parameter. The business object's NewRow method automatically calls this hook method when a new row is added, passing the new row as a parameter.

Regards,
Kevin McNeish
Eight-Time .NET MVP
VFP and iOS Author, Speaker & Trainer
Oak Leaf Enterprises, Inc.
Chief Architect, MM Framework
http://www.oakleafsd.com
Previous
Reply
Map
View

Click here to load this message in the networking platform