Creating classes|properties|methods programatically

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrzej Lipski

    #1

    Creating classes|properties|methods programatically

    I am new to dotnet and I'll tried searching Google for a solution to my
    problem. I am hoping that it is possible to do, or am I going down a dead
    end?

    I have a User class that has known properties from a DB table (EmployeeID,
    FirstName, LastName) and a set of unknown properties that need to be
    appended to it.

    Is it possible to create a function that I can pass a class object and
    string containing the unknown property names into so that it appends the
    class with the new properties and then instantiates it?


  • Armin Zingler

    #2
    Re: Creating classes|propert ies|methods programatically

    "Andrzej Lipski" <andrzej.lipski @bhs.orgschrieb
    I am new to dotnet and I'll tried searching Google for a solution to
    my problem. I am hoping that it is possible to do, or am I going
    down a dead end?
    >
    I have a User class that has known properties from a DB table
    (EmployeeID, FirstName, LastName) and a set of unknown properties
    that need to be appended to it.
    >
    Is it possible to create a function that I can pass a class object
    and string containing the unknown property names into so that it
    appends the class with the new properties and then instantiates it?
    How do you intend to handle the properties later? How do you set and get
    their values in code? You don't know their names during programming.


    Armin

    Comment

    • Armin Zingler

      #3
      Re: Creating classes|propert ies|methods programatically

      "Andrzej Lipski" <andrzej.lipski @bhs.orgschrieb
      The class that will contain the properties will be part of a
      collectionof class that will be bound to a datagridview. I do know
      where the value are coming from but not the names or how many will
      be coming in. These values are 'roles' that can be given to a user.
      Its a many-to-many relationship so the user need to be assigned
      multiple roles. But each group will have its own control over the
      names of and number of roles that in their group. That is a
      requirement.
      >
      I have something programmaticall y creates a dataset, populates it
      with the users collection, appends each role as a column and then
      sets a value for that user role if it listed in the lookup table. That
      datatable is then bound to the datagridview. However I was
      told that I need make bindable object for the UI guy to attach too
      so they can configure it all through the designed and not write it
      all in the code behind page.
      >
      I was doing some research into Reflection but I have to admit that I
      have no experience in using it.

      I know nothing about the datagridview because I'm not a friend of data
      binding and late binding at all, so unfortunatelly I can not help you with
      this.

      I would add the values to a Hashtable or any kind of name/value collection
      maintained by the User class. Maybe somebody else knows a way to dynamically
      bind these values as additional columns to the datagridview. IMO better than
      dynamically creating extended classes using Reflection. Though, I don't know
      what's the best way, sorry.


      Armin

      Comment

      • Andrzej Lipski

        #4
        Re: Creating classes|propert ies|methods programatically

        The class that will contain the properties will be part of a collectionof
        class that will be bound to a datagridview. I do know where the value are
        coming from but not the names or how many will be coming in. These values
        are 'roles' that can be given to a user. Its a many-to-many relationship so
        the user need to be assigned multiple roles. But each group will have its
        own control over the names of and number of roles that in their group. That
        is a requirement.

        I have something programmaticall y creates a dataset, populates it with the
        users collection, appends each role as a column and then sets a value for
        that user role if it listed in the lookup table. That datatable is then
        bound to the datagridview. However I was told that I need make bindable
        object for the UI guy to attach too so they can configure it all through the
        designed and not write it all in the code behind page.

        I was doing some research into Reflection but I have to admit that I have no
        experience in using it.

        "Armin Zingler" <az.nospam@free net.dewrote in message
        news:%2318SmYhA IHA.968@TK2MSFT NGP03.phx.gbl.. .
        "Andrzej Lipski" <andrzej.lipski @bhs.orgschrieb
        >I am new to dotnet and I'll tried searching Google for a solution to
        >my problem. I am hoping that it is possible to do, or am I going
        >down a dead end?
        >>
        >I have a User class that has known properties from a DB table
        >(EmployeeID, FirstName, LastName) and a set of unknown properties
        >that need to be appended to it.
        >>
        >Is it possible to create a function that I can pass a class object
        >and string containing the unknown property names into so that it
        >appends the class with the new properties and then instantiates it?
        >
        How do you intend to handle the properties later? How do you set and get
        their values in code? You don't know their names during programming.
        >
        >
        Armin

        Comment

        • =?Utf-8?B?RGVubmlz?=

          #5
          Re: Creating classes|propert ies|methods programatically

          You can use reflection to both get and set properties of a class based on the
          property name.
          --
          Dennis in Houston


          "Andrzej Lipski" wrote:
          The class that will contain the properties will be part of a collectionof
          class that will be bound to a datagridview. I do know where the value are
          coming from but not the names or how many will be coming in. These values
          are 'roles' that can be given to a user. Its a many-to-many relationship so
          the user need to be assigned multiple roles. But each group will have its
          own control over the names of and number of roles that in their group. That
          is a requirement.
          >
          I have something programmaticall y creates a dataset, populates it with the
          users collection, appends each role as a column and then sets a value for
          that user role if it listed in the lookup table. That datatable is then
          bound to the datagridview. However I was told that I need make bindable
          object for the UI guy to attach too so they can configure it all through the
          designed and not write it all in the code behind page.
          >
          I was doing some research into Reflection but I have to admit that I have no
          experience in using it.
          >
          "Armin Zingler" <az.nospam@free net.dewrote in message
          news:%2318SmYhA IHA.968@TK2MSFT NGP03.phx.gbl.. .
          "Andrzej Lipski" <andrzej.lipski @bhs.orgschrieb
          I am new to dotnet and I'll tried searching Google for a solution to
          my problem. I am hoping that it is possible to do, or am I going
          down a dead end?
          >
          I have a User class that has known properties from a DB table
          (EmployeeID, FirstName, LastName) and a set of unknown properties
          that need to be appended to it.
          >
          Is it possible to create a function that I can pass a class object
          and string containing the unknown property names into so that it
          appends the class with the new properties and then instantiates it?
          How do you intend to handle the properties later? How do you set and get
          their values in code? You don't know their names during programming.


          Armin
          >
          >
          >

          Comment

          • Armin Zingler

            #6
            Re: Creating classes|propert ies|methods programatically

            "Dennis" <Dennis@discuss ions.microsoft. comschrieb
            You can use reflection to both get and set properties of a class
            based on the property name.
            I think the problem is that the property does not exist yet and is to be
            added at runtime.


            Armin

            Comment

            • =?Utf-8?B?RGVubmlz?=

              #7
              Re: Creating classes|propert ies|methods programatically

              Guess I didn't understand the question. Kind of hard to add a property at
              run time but guess is possible with some inline runtime coding but way beyond
              my expertese.
              --
              Dennis in Houston


              "Armin Zingler" wrote:
              "Dennis" <Dennis@discuss ions.microsoft. comschrieb
              You can use reflection to both get and set properties of a class
              based on the property name.
              >
              I think the problem is that the property does not exist yet and is to be
              added at runtime.
              >
              >
              Armin
              >
              >

              Comment

              • Cor Ligthert[MVP]

                #8
                Re: Creating classes|propert ies|methods programatically

                Andrzej,

                In a kind of way you can, this is called recursive programming, be aware
                that you by instance don't know at the start how much memory you will need.

                Cor

                Comment

                • Andrzej Lipski

                  #9
                  Re: Creating classes|propert ies|methods programatically

                  This is what I originally wrote in the load event. The Users are passed
                  into a datatable and it is bound to the datagridview. I then create a
                  datagridviewche ckboxcolumn for each row in the roles table and add them to
                  the same datagridview. I then flag each datagridviewche ckboxcolumn with a
                  true state if there is a userroles row for that particular user and role.

                  The intent was to be able to bring this code down to the business object
                  layer so that the datagridview could be bound to a single object. The
                  business object layer could then be reused by other winform applications or
                  could even be used by web applications the future.

                  mUsers.FillAll( )
                  mRoles.FillAll( )
                  mUserRoles.Fill All()

                  Dim dt As New DataTable
                  Dim dr As DataRow
                  dt.Columns.Add( "USRPK", GetType(Integer ))
                  dt.Columns.Add( "USREmpId", GetType(String) )
                  dt.Columns.Add( "USRLastNam e", GetType(String) )
                  dt.Columns.Add( "USRFirstNa me", GetType(String) )

                  For Each ur As User In mUsers
                  dr = dt.NewRow
                  dr.Item(0) = ur.USRPK
                  dr.Item(1) = ur.USREmpId
                  dr.Item(2) = ur.USRLastName
                  dr.Item(3) = ur.USRFirstName
                  dt.Rows.Add(dr)
                  Next
                  dgvUsers.DataSo urce = dt
                  FormatDgvUsers( )
                  For Each rl As Role In mRoles
                  Dim chk As New DataGridViewChe ckBoxColumn
                  chk.HeaderText = rl.RLName
                  chk.Name = "chk" & rl.RLName
                  FormatChk(chk)
                  If chk.HeaderText = "_Access" Then
                  chk.Visible = False
                  End If
                  dgvUsers.Column s.Add(chk)
                  Next
                  For Each ur As UserRole In mUserRoles
                  Dim i1 As Integer = 0
                  While Not i1 = mUsers.Count
                  If ur.URUPPK = mUsers.Item(i1) .USRPK Then
                  Dim i2 As Integer = 0
                  While Not i2 = mRoles.Count
                  If ur.URRLPK = mRoles(i2).RLPK Then
                  dgvUsers.Rows(i 1).Cells("chk" &
                  mRoles(i2).RLNa me).Value = 1
                  End If
                  i2 = i2 + 1
                  End While
                  End If
                  i1 = i1 + 1
                  End While
                  Next

                  "Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
                  news:15127509-4C9E-4C3F-A8B8-0E0D573CE7A9@mi crosoft.com...
                  Andrzej,
                  >
                  In a kind of way you can, this is called recursive programming, be aware
                  that you by instance don't know at the start how much memory you will
                  need.
                  >
                  Cor

                  Comment

                  Working...