Creating classes|properties|methods programatically

  • Thread starter Thread starter Andrzej Lipski
  • Start date Start date
A

Andrzej Lipski

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?
 
Andrzej Lipski said:
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
 
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 programmatically 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.
 
Andrzej Lipski said:
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 programmatically 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
 
You can use reflection to both get and set properties of a class based on the
property name.
 
Dennis said:
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
 
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.
 
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
 
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
datagridviewcheckboxcolumn for each row in the roles table and add them to
the same datagridview. I then flag each datagridviewcheckboxcolumn 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.FillAll()

Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add("USRPK", GetType(Integer))
dt.Columns.Add("USREmpId", GetType(String))
dt.Columns.Add("USRLastName", GetType(String))
dt.Columns.Add("USRFirstName", 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.DataSource = dt
FormatDgvUsers()
For Each rl As Role In mRoles
Dim chk As New DataGridViewCheckBoxColumn
chk.HeaderText = rl.RLName
chk.Name = "chk" & rl.RLName
FormatChk(chk)
If chk.HeaderText = "_Access" Then
chk.Visible = False
End If
dgvUsers.Columns.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(i1).Cells("chk" &
mRoles(i2).RLName).Value = 1
End If
i2 = i2 + 1
End While
End If
i1 = i1 + 1
End While
Next
 
Back
Top