Solved

Instanciating Objects on the fly

Posted on 2006-06-12
11
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 
LVL 86

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 86

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 86

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 86

Expert Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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