Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

Instanciating Objects on the fly

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
directxBOB
Asked:
directxBOB
  • 5
  • 4
  • 2
2 Solutions
 
Jase-CoderCommented:
you could pass a number to the function and return an array of new queues like:
0
 
directxBOBAuthor Commented:
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
 
directxBOBAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
directxBOBAuthor Commented:
and to remove a Queue from the Hashtable:

queues.remove(ID);

is this correct?
0
 
Jase-CoderCommented:
yes that is correct
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
directxBOBAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
directxBOBAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
That'll work...
0

Featured Post

Hire Technology Freelancers with Gigs

Work with 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.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now