StrataFrame Forum

Seting Default Values

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

By Bradley Marston - 3/1/2007

I have a BO with a lot of fields that need to set with defaults, I also have a datatable with the field name and value. Is there a way to set these defaults looping thru the data table without having to use a select case where i must type Case datatable.fieldname = me.fieldname. For example in vfp one could use macro

substitution , but not in vb.net or c sharp.net

By Trent L. Taylor - 3/1/2007

Yeah...as you already know, macros were a very dangerous thing since it was not strong-typed, but also a useful tool when you just want to "strong-arm" something.  In .NET, you will use reflection.  However, in this example you can just use the CurrentRow exposed on the BO.

Dim loRow As DataRow

For Each loRow IN MyTempDataTable.Rows
    MyBO.CurrentRow.Item(loRow.Item("FieldName")) = loRow.Item("FieldValue")
Next

By Bradley Marston - 3/2/2007

After I posted the question I thought there would be a way to do it using current row, but did not have the time to investigate it. Your reponse is very much apprecited and will save me a lot of time.
By Bradley Marston - 3/14/2007

On a related note is there a way to similarly add BrokenRules without having to hardcode the first parameter and make

it dynamic?

Me.AddBrokenRule(MyBusinessObjectFieldNames.cust_lname, "Last name must be at least two characters.")

becomes

dim myvalue as string = "cust_lname"

Me.AddBrokenRule(myvalue , "Last name must be at least two characters.")


 


 

By StrataFrame Team - 3/14/2007

Yes, there is an overload to accept the field name as a string.  The overload to accept the parameter as an enum (which is defined within the partial class of the business object) just .ToString()s the enum value and calls the other overload.  So, you can certainly make it dynamic, and also add broken rules for custom properties that do not have a value in the enum.
By Bradley Marston - 3/20/2007

For one last question on the subject , how do I add a broken rule to a created business object. for example

BOCustomers.BrokenRules.Add()  takes a parameter of type MicroFour.StrataFrame.Business.BrokenRule 

When I try to create an object of MicroFour.StrataFrame.Business.BrokenRule I cannot use the new keyword, and all the relevent properties seem to be Read Only.

By StrataFrame Team - 3/20/2007

Yes, the constructor for the BrokenRule class is internal (Friend in VB), to prevent broken rules from being added outside of the business objects themselves.  If you want to be able to add a broken rule from outside of a business object, then you'll need to create a public method that will turn around and call the BusinessLayer.AddBrokenRule method internally.