StrataFrame Forum

Field sizes

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

By Ben Kim - 3/1/2007

This is more out of curiosity but also lends itself to efficiency.  When creating the BO's using the Business Object Mapper, shouldn't there be some way of creating a property with MaximumSize?  This way when creating the UI, I do not have to keep referring to our SQL Schema to find out the size of each field.  We have over 540 tables in our schema, more tables and fields being added by the DBA frequently so I cannot keep track of the sizes in my head.

So whether the field should accept 1 character or 5,000, it would be easier to type:

MyTextField.MaximumSize = Me.MyBO1.Field.MaximumSize - or if you look at my enhancement idea for an automatic screen builder wizard, this metadata could be used to set the property automagically.

You have the data in the DDT and even if you directly import into the BOM, all you need is an additional MaxSize field listed here to support this feature. 

So, out of curiosity, is this something that "might" be supported in the future?

Ben

By StrataFrame Team - 3/2/2007

We do not currently have plans to implement a "MaxSize" property on the business object... however, we already have a FieldLengths property... For string fields, it will contain the length of characters that the field can contain (i.e. for VarChar(150), the value will be 150).  For other data types, it will be 0 (and it's part of the partial class, so it gets updated when you rebuild the partial class).  As I said, though, we don't have plans to automatically set the MaximumLength property on a textbox to the specified value.
By Ben Kim - 3/2/2007

Ben,

Thank you.  That worked like a charm.  Hopefully one of these days I will be as well versed in .NET as you Cool

Now to make it generic so I can call it whenever initializing a form Wink

Ben

By Ben Kim - 3/2/2007

Ben,

I think I am close to a generic solution that I can call from any StrataFrame form.  However, I cannot for the life of me figure out what the proper AS "Control" to use from SF's object library.

Public Sub SetMaxLengths(ByVal MyForm As MicroFour.StrataFrame.UI.Windows.Forms.BaseForm, ByVal BO As MicroFour.StrataFrame.Business.BusinessLayer)

 For Each MyControl As Control In MyForm.Controls 'HERE the AS CONTROL needs to be   
   
replaced with a more specific type of control from SF library since .BindingField
    and .MaxLength are none standard

  If (TypeOf MyControl Is MicroFour.StrataFrame.UI.Windows.Forms.Textbox) Then
   
If MyControl.BindingField Then
           
MyControl.MaxLength = BO.FieldLengths(MyControl.BindingField)
   
End If
    End If
 Next
End Sub

Or is there a better way with reflection?

Ben

By StrataFrame Team - 3/2/2007

No, reflection is going to slow you down tons... your best bet is probably to do a TryCast... it's much faster than catching any InvalidCastExceptions and a little faster than testing the TypeOf or GetType() of something.  And you'll also need to call it recursively (because not all of the textboxes are going to be in the root Controls collection).

Public Shared Sub SetAllMaxLengths(ByVal container As ContainerControl)
    Dim box As MicroFour.StrataFrame.UI.Windows.Forms.TextBox
    Dim container2 As ContainerControl

    For Each ctrl As Control In container.Controls
        box = TryCast(ctrl, MicroFour.StrataFrame.UI.Windows.Forms.TextBox)
        If box IsNot Nothing Then
            box.MaxLength = box.BusinessObject.FieldLengths(box.BindingField)
        Else
            container2 = TryCast(ctrl, ContainerControl)
            If container2 IsNot Nothing Then
                SetAllMaxLengths(container2)
            End If
        End If        
    Next

End Sub

You can then call this method from within any form by just passing Me as a reference to the parameter (because a form is a ContainerControl).

By Ben Kim - 3/5/2007

Thanks Ben.  However, when I copy this code into my tools module, I get two errors:

1. Shared is not allowed in a module <--- I removed the Shared

2. box.BusinessObject.FieldLengths(box.BindingField) - FieldLengths is not a member of MicroFour.StrataFrame.Business.BusinessLayerBase <-- isn't FieldLengths a property of the IBusinessBindable interface?  If so, how do I cast that into your code example?

Thanks!

Ben

By StrataFrame Team - 3/5/2007

I assumed that your code was going to be in a class... therefore Shared (in fact, when you put code in a module, .NET compiles the module as a class and then adds all of the methods to it as Shared Smile).  As for the FieldLengths not being a property, you will need to change that line to:

box.MaxLength = CType(box.BusinessObject, BusinessLayer).FieldLengths(box.BindingField)