Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2007

Tip: Looking for answers? Try searching our database.

Creating classes|properties|methods programatically

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andrzej Lipski - 28 Sep 2007 21:34 GMT
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 - 28 Sep 2007 22:24 GMT
> 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
[quoted text clipped - 7 lines]
> 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
Andrzej Lipski - 28 Sep 2007 23:44 GMT
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 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
[quoted text clipped - 12 lines]
>
> Armin
Armin Zingler - 29 Sep 2007 00:09 GMT
> The class that will contain the properties will be part of a
> collectionof class that will be bound to a datagridview.  I do know
[quoted text clipped - 15 lines]
> 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
Dennis - 29 Sep 2007 01:10 GMT
You can use reflection to both get and set properties of a class based on the
property name.
Signature

Dennis in Houston

> 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
[quoted text clipped - 30 lines]
> >
> > Armin
Armin Zingler - 29 Sep 2007 02:25 GMT
> 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
Dennis - 29 Sep 2007 02:48 GMT
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.
Signature

Dennis in Houston

> > You can use reflection to both get and set properties of a class
> > based on the property name.
[quoted text clipped - 3 lines]
>
> Armin
Cor Ligthert[MVP] - 29 Sep 2007 04:32 GMT
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
Andrzej Lipski - 02 Oct 2007 21:23 GMT
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

> Andrzej,
>
[quoted text clipped - 3 lines]
>
> Cor

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.