locked
Referencing Object Dynamically in Form RRS feed

  • Question

  • User1002026046 posted

    I built a form that dynamically builds controls based on a database.  I can assign fields to that form in data so that I can manage ONE form for all my admin tables.

    The controls are added dynamically.  

    I have a class file for each of my objects that could be a form (Customers, Orders, products).  They are all based on a Base Class and have overriding Insert, Update, Delete and Select methods.

    When the user clicks SAVE, I want to pass the form data to the appropriate class.  I guess I can do this with Reflection, but that sounds like a LOT of overhead.

    ANY IDEAS?

    Tuesday, February 5, 2019 2:47 PM

All replies

  • User475983607 posted

    In general,  browsers submit HTML Form's name/value pairs to a URL.  Usually the URL - the code that runs -  knows what to expect and performs a specific task.  If the URL is a generic invoke handler then you'll need to craft protocol using a standard container (the HTML Form) that contains information which tells the code what to do.  The action to perform, the input and out types, and resulting HTML.

    Tuesday, February 5, 2019 3:15 PM
  • User1002026046 posted

    Thanks!  Already done that!  BOOM

    My problem is that I take my keyvaluePair result and want to pass it to the appropriate object.  the base class has a property called LoadFromForm which accepts the KeyValuePair.

    Instead of passing it to the Base Class, I want to pass it to the Inherited Class.  But there could be an endless number of those inherited classes (Rooms, Products, CUstomers, orders) based on which data drives the form

    Dim myobject as thesubclassIwant

    myobject.LoadFromForm(MyKeyValuePair)

    myobject.ProcessRecord

    THanks!!

    Tuesday, February 5, 2019 3:26 PM
  • User475983607 posted

    Thanks!  Already done that!  BOOM

    My problem is that I take my keyvaluePair result and want to pass it to the appropriate object.  the base class has a property called LoadFromForm which accepts the KeyValuePair.

    Instead of passing it to the Base Class, I want to pass it to the Inherited Class.  But there could be an endless number of those inherited classes (Rooms, Products, CUstomers, orders) based on which data drives the form

    Dim myobject as thesubclassIwant

    myobject.LoadFromForm(MyKeyValuePair)

    myobject.ProcessRecord

    THanks!!

    Right and as stated above, you need a bit more information in the request.  The action and the input type if not already determined by the URL  These could be hidden fields injected in the HTML Form.  

    ...&action=save&inputType=namespace.classname

    If the URL determines the action and type then this information should be stored in a DB table.  

    Tuesday, February 5, 2019 3:44 PM
  • User1002026046 posted

    THanks!!  I get all that.  What I need to know is how I can Define an object by name.  My vision is to not have to hard code the forms.  I know reflection is costly, so I am trying to avoid that.

    instead of 

    Select Case frmtype

    Case "Product"

    Dim p as new clsproduct

    p.loadformdata = keyvaluepair<data>

    DIm c as new customer

    c.loadformdata = keyvaluepair<data>

    ...

    end select

    I would rather do it dynamically

    c = createbyname(msFormType)

    c.loadFormData = keyvaluepair<data>

    But i don't know how to create a new object by classtype dynamically without reflection.

    Tuesday, February 5, 2019 5:15 PM
  • User475983607 posted

    My vision is to not have to hard code the forms.  I know reflection is costly, so I am trying to avoid that.

    Can you quantify "costly" and how reflection impacts your project?

    Select Case frmtype

    Case "Product"

    Dim p as new clsproduct

    p.loadformdata = keyvaluepair<data>

    DIm c as new customer

    c.loadformdata = keyvaluepair<data>

    ...

    end select

    I would rather do it dynamically

    c = createbyname(msFormType)

    c.loadFormData = keyvaluepair<data>

    The URL generally uniquely defines the type and action. 

    https://www.mydomain.com/user/create

    Just new up the type and invoke the action within the code that executes when this URL is invoked.

    If your URLs are not defined then map the URL to an action and type within a database table.  Use reflection to instantiate the type which can be done through a generic method.

    But i don't know how to create a new object by classtype dynamically without reflection

    Me neither. 

    Anyway, it sounds like you are trying to build a framework that already exists; MVC/Web API.

    Tuesday, February 5, 2019 6:46 PM