Solved

Instanciating Objects on the fly

Posted on 2006-06-12
11
222 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
Comment Utility
you could pass a number to the function and return an array of new queues like:
0
 

Author Comment

by:directxBOB
Comment Utility
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
Comment Utility
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
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
Comment Utility
and to remove a Queue from the Hashtable:

queues.remove(ID);

is this correct?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Expert Comment

by:Jase-Coder
Comment Utility
yes that is correct
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
That'll work...
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now