[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Dynamic Object Names C#

Posted on 2009-04-17
6
Medium Priority
?
747 Views
Last Modified: 2013-12-17
I have a class foo. I want to create multiple object of this class with multiple object names derived from a string or database.
Dont need to to be from database just anything diffrent each time so that i can  fill my items array with it.
public class foo
{
  
}
 
foreach(row r in ds.Tables[0].row)
{
  foo dr.["id"] =new foo();
}
items[] items={ dr.["id"]}

Open in new window

0
Comment
Question by:WannabeNerd
  • 3
  • 3
6 Comments
 
LVL 6

Expert Comment

by:HarryNS
ID: 24166573

private void Test()
        {
            DataSet ds = new DataSet();
            //Fill ds
            List<foo> myObj = new List<foo>();
            int idx = 0;
            foreach(DataRow r in ds.Tables[0].Rows)
            {
                myObj[idx] = new foo();
                myObj[idx].Name = r[0].ToString();
            }
            
            //To access the Filled object 
            MessageBox.Show(myObj[0].Name);
        }
       public class foo
    {
        private string _name;
 
        public string Name
        {
          get { return _name; }
          set { _name = value; }
        }
    }

Open in new window

0
 

Author Comment

by:WannabeNerd
ID: 24166912
I am getting this error when i use your code.


Message      "Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"      string
0
 
LVL 6

Expert Comment

by:HarryNS
ID: 24166967
Where do you get this error? While retreiving the value from object or when assigning?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:WannabeNerd
ID: 24166980
Actually it happnes when ever i assign this object to new foo();

 myObj[idx] = new foo();


0
 
LVL 6

Accepted Solution

by:
HarryNS earned 2000 total points
ID: 24167531
Sorry use this...
private void Test()
        {
            DataSet ds = new DataSet();
            //Fill ds
            List<foo> myObj = new List<foo>();
            foo obj = null;
            foreach(DataRow r in ds.Tables[0].Rows)
            {
                obj = new foo();
                obj.Name = r[0].ToString();
                myObj.Add(obj);
            }
            
            //To access the Filled object 
            MessageBox.Show(myObj[0].Name);
        }
       public class foo
    {
        private string _name;
 
        public string Name
        {
          get { return _name; }
          set { _name = value; }
        }
    }

Open in new window

0
 

Author Comment

by:WannabeNerd
ID: 24167612
for (int i = 0; i < 5; i++)
                {
                    myObj.Add(new foo());
                    myObj[i].Name = "test";
             
                }
                MessageBox.Show(myObj[3].Name.ToString());


This is what i tried. Let me try yours and hopefully it will work
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Integration Management Part 2
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question