Solved

Instanciating Objects on the fly

Posted on 2006-06-12
11
223 Views
Last Modified: 2010-04-16
I have created a method :

public void newQueue()
{
   Queue myQueue = new Queue();
}

This basically creates a new queue. What I want to be able to do is create as many queues as I need by simply calling newQueue();

Most likely I would need to pass in a String and an Id Number so I can Name and Number the queues. Any ideas on how to do this? Basically I dont want to be restricted to reusing the same queue over and over, as I would potntially like 6 - 12 queues running at the same time. So overlapping them wouldnt be good.

Cheers

0
Comment
Question by:directxBOB
  • 5
  • 4
  • 2
11 Comments
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 16891874
you could pass a number to the function and return an array of new queues like:
0
 

Author Comment

by:directxBOB
ID: 16891891
So basically I would be creating a list? and each element in the list would be a Queue which I pull back out using an Element number....
0
 

Author Comment

by:directxBOB
ID: 16891907
eg:

Queue myQueue = new Queue();
ArrayList myList = new ArrayList();
myList.Add(myQueue);

but how do I add the id, and more importantly how do I make it more dynamic, I was thinking doing a Factory style system but it would need to be far more lightweight.

0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 16891965
You could do something like...

    public partial class frmMain : Form
    {

        Hashtable queues = new Hashtable();

        public Queue myQueues(string ID)
        {
            Queue retQueue;
            if (!queues.ContainsKey(ID))
            {
                retQueue = new Queue();
                queues.Add(ID, retQueue);
            }
            else
            {
                retQueue = (Queue)queues[ID];
            }
            return retQueue;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            myQueues("Q1").Enqueue("Hello...");
            myQueues("Q1").Enqueue("Experts");

            myQueues("Q2").Enqueue("...Goodbye");
            myQueues("Q2").Enqueue("Exchange");

            Debug.WriteLine(myQueues("Q1").Dequeue().ToString());
            Debug.WriteLine(myQueues("Q2").Dequeue().ToString());

            Debug.WriteLine(myQueues("Q1").Dequeue().ToString() + myQueues("Q2").Dequeue().ToString());
        }

    }
0
 

Author Comment

by:directxBOB
ID: 16892110
and to remove a Queue from the Hashtable:

queues.remove(ID);

is this correct?
0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 16892448
yes that is correct
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 500 total points
ID: 16893548
Right...

        public void myQueuesRemove(string ID)
        {
            if (queues.ContainsKey(ID))
            {
                queues.Remove(ID);
            }
        }

...then:

    myQueuesRemove("Q1");

You also need to either check the Queues Count before Dequeuing or enclose the calls in a Try...Catch block to keep your app from crashing if you Dequeue and empty Queue.
0
 

Author Comment

by:directxBOB
ID: 16898573
If I call this:

Queue myQueue = new Queue();

10 times am I creating 10 new Queues or am I re-instanciating the same queue 10 times over?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16898640
You are creating ten new queues.

Additionally, if you did that inside a sub/function then those queues will go out of scope once the sub/function has exited and then they will be garbage collected if they are not referenced anywhere else.
0
 

Author Comment

by:directxBOB
ID: 16898755
Well basically what I have is:

    /**
      * Create a new Queue
      */
    public Queue createQueue(string ID)
    {
      Queue myQueue;
      if (!queues.ContainsKey(ID))
      {
        myQueue= new Queue();
        queues.Add(ID, myQueue);
      }
      else
      {
        myQueue= (Queue)queues[ID];
      }
      return myQueue;
    }

Where basically I am looking to create X amount of Queues and put them into a Hashtable, then call other get and set methods to handle and maintain the queues.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16899046
That'll work...
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Icons and Colors for Terms 3 32
Replace &lt; with < 14 59
Angular - break the dataset in chunks? 7 50
compare between entities in entity framework 8 70
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

776 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