Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 23rd, 2008, 01:00 AM
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to dynamically add propery in a Class in C#?

Hi All,

I need to create property in a class dynamically.
Property names will come from database and also the total number of properies can be changed according to database value.
I have tried with CodeDome,but unable to solve.

Please suggest me.
  #2 (permalink)  
Old December 23rd, 2008, 01:09 AM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

If you mean a simple data member of a class, much as you would do by coding
Code:
class Foo 
{
    int someProperty;
}
the answer is simple: You can't. You can't. You can't.

C#--along with C++ and VB.NET and C and Java and Pascal and many other languages--is a *COMPILED* language. Generally speaking, only INTERPRETED languages allow adding members to classes dynamically.

However...

However, if you want to plan ahead, you could clearly add a data member to your class that is, say, some kind of collection. And then you can add elements to that collection.

Depending on the rest of your code, this might be exactly what you need.

In fact, if your class includes, say, a DataTable member (or DataSet member), then you have accomplished what you are after already.

But *compiled*? No.
  #3 (permalink)  
Old December 23rd, 2008, 02:23 AM
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot for your quick reply.

But could u plz explian this line
"However, if you want to plan ahead, you could clearly add a data member to your class that is, say, some kind of collection. And then you can add elements to that collection."

Means i need some example code.
  #4 (permalink)  
Old December 23rd, 2008, 03:43 AM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

If you have *ANY* kind of collection object as a member of your class, then you could add to it as needed.

Example: Say you have a class that describes these "properties" that you want to dynamically add. Maybe a property has a name, a type, and a value. For simplicity, we'll say that the value is held as just an Object and you will write code to cast the various values to their correct types.
Code:
class MyProperty
{
    public String name;
    public DataTypeDescriptor type;
    public Object value;
}
(I'm keeping this simple, just making everything public. In "real life" you'd probably write get/set methods and keep the data members private. But that's really just syntactic sugar.)

Okay, so now you write your class that needs to be able to hold an arbitrary number of these dynamically added MyProperty objects.
Code:
class MyHolder
{
    // I'm just making up some other members of the class...
    public DateTime whenCreated; 
    public int timeToLive;
    public String password; // silly stuff

    // but then here is your collection:
    List<MyProperty> properties;

    // and you could have methods such as
    void AddProperty( MyProperty toAdd )
    {
        properties.Add( toAdd );
    }
    // or maybe:
    void AddProperty( String nm, DataTypeDescriptor dtd, Object val );
    {
        // really should have a constructor for MyProperty, but even without one
        // we can do it the brute force way:
        MyProperty prop = new MyProperty( );
        prop.Name = nm;
        prop.Type = dtd;
        prop.Value = val;
        properties.Add( prop );
    }
    // and much much more...
}
If you think about it, the .NET framework is just chock full of collections such as this. Just as one for-instance, DataReader and DataTable and DataRow objects are all implemented, under the covers, with some kind of collection. And of course when you use a method such as FindControl( ), you are really just looking for a particular object in the collection of objects that represent the HTML display. And and and...

At this point, maybe the best thing I can suggest is that you think about buying and reading this book:
http://www.wrox.com/WileyCDA/WroxTit...470261293.html

It's probably the best way to approach C#, much better than starting from building websites, I think. And it's going to cover these kinds of subjects in a lot more depth than anyone can, just posting messages in these silly little windows.
  #5 (permalink)  
Old December 23rd, 2008, 05:20 AM
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks a lot for your help....
I'll try this.
  #6 (permalink)  
Old December 23rd, 2008, 07:57 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

It can be done using reflection and code emission, or possibly with extension methods to some xtent. You'd have to describe the scenario and it's complicated. Probably better off having a DynamicProperties property as the others have suggested.
__________________
Joe
http://joe.fawcett.name/
  #7 (permalink)  
Old December 23rd, 2008, 11:28 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

coolcoder2007 -

Could you explain what you are trying to achieve, versus how you are trying to achieve it? Perhaps there is a more logical solution to what you are trying to do instead of us trying to help you implement how you think you should solve the original problem.

There are solutions for building code on the fly and compiling then consuming it, however, without a clearly stated problem, that may or may not be the appropriate solution.
__________________
-Peter
compiledthoughts.com
twitter/peterlanoie
  #8 (permalink)  
Old December 28th, 2008, 05:55 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Peter wrote: "There are solutions for building code on the fly and compiling then consuming it..."

I hadn't thought about it enough, I guess, but I can see that you could easily enough do that with ASP.NET. You just spit out ".aspx" files and then transfer to your dynamically created pages and presto.

But man oh man, I'd sure hate to try to maintain and debug such a system. Ugh, to say the least.
  #9 (permalink)  
Old January 5th, 2009, 09:29 PM
Registered User
 
Join Date: Dec 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default DaoliteMappingTool can do it for you

DaoMappingTool will connect your database and generate fully working extendable source code for DAO classes.

Google it and download to have a try.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamically Parameters.Add for storedProc havey C# 2 November 27th, 2007 02:41 PM
Dynamically Class Loading form jar saiyedriyaz J2EE 1 February 28th, 2005 05:41 AM
Dynamically Add input tags Jstmehr4u3 Javascript How-To 2 July 29th, 2003 04:03 AM



All times are GMT -4. The time now is 09:20 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.