?
Solved

Instanciating Objects on the fly

Posted on 2006-06-12
11
Medium Priority
?
230 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
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!

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

800 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