Please help me its urgent...

I have a MSDN Queue class, I need your suggestion can I make a message class and enqueue its objects as an element.

// new.cpp : main project file.

#include "stdafx.h"
#include "message.h"
using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myCollection );


void PrintValues( IEnumerable^ myCollection )
{
   IEnumerator^ myEnum = myCollection->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::Write( "    {0}", obj );
   }
 Console::ReadLine(); 
   
}

int main(array<System:: String ^> ^args)
{
    Console::WriteLine(L"Hello World");
	Queue^ myQ = gcnew Queue;
    message m1= gcnew message(101);
    myQ->Enqueue( m1);
    myQ->Enqueue( "World" );
    myQ->Enqueue( "!" );

  // Displays the properties and values of the Queue.
   Console::WriteLine( "myQ" );
   Console::WriteLine( "\tCount:    {0}", myQ->Count );
   Console::Write( "\tValues:" );
   PrintValues( myQ );
  
   //system("pause");

   return 0;
}

**********************************************
my created class is this 
message.h
class message
{
  int MsgId;
  char properties; 
  message(int Id);
};


#include "message.h"


int message::getId()
{
  return this.MsgID;

}

void message::setId(int Id)
{
  this.MsgID=Id;
}

message::message(int Id)
{
	this. MsgID=Id;
}

If you fix this so it will compile, that would be a great start.

I removed the PrintValues function for the simpler "for each".

Here is the main.cpp

#include "stdafx.h"
#include "message.h"
using namespace System;
using namespace System::Collections;

int main(/*array<System:: String ^> ^args*/)
{
   Queue^ myQ = gcnew Queue;
   myQ->Enqueue(gcnew message(101));
   myQ->Enqueue("World");
   myQ->Enqueue("!");

   // Displays the properties and values of the Queue.
   Console::WriteLine("myQ\n\tCount:\t{0}\n\tValues:", myQ->Count );

   for each(message^ msg in myQ)
   {  // PrintValues
      Console::WriteLine(msg->MsgID);
   }

   Console::ReadLine();
   return 0;
}

The message ID is now a property with built-in accessors.
Here is the message.h

public ref class message
{
public:
   property int MsgID;
   message(int Id) {MsgID=Id;}
};

The message.cpp is now empty unless you need to add methods.

Thank you very much for your kind reply... I am going to try this.

Thanks its working, if I wanna add another class, should I make it ref class as well.

Thank you very much.

Yes, it should be a ref class if you expect it to be used with managed collections.

Hi,

I wanted to use another class "exchange" whose code is here:
exchange.h

public ref class exchange
{
 public:
  bool Durable;
  int type;
  bool AutoDelete;
  bool Arguments;


  exchange::exchange()
  {
   Durable= false;
   AutoDelete=false;
  }
 exchange::exchange(bool D, bool AD, int T)
 {
    this.Durable= D;
    this.AutoDelete=AD; 
    this.type=T;
 }
 void exchange:: DelExchange()
 {
   this.Durable= false;
   this.AutoDelete=false;
   this.type=0;
  }

 int exchange::getType()
  {
   return this.type;
  }


 void exchange::setType(int t)
  {
    this.type= t;
  }

};

and this cod I added in the main file

 exchange^ E1= gcnew exchange();
   E1.setType(101);
   Console::WriteLine("Excnahge type is  {0}:", E1.getType());

Its giving me a lot of errors :(

the erros are all of type::

error C2228: left of '.AutoDelete' must have class/struct/union

I sloved this problem :) Thanks.

Good.
Be sure to investigate the use of the "property" designation so you won't need to create get() and set() methods yourself.

Also, you should set default values for all of your variables and properties in the constructor.

Hi, I wanna check each and every message and according to its routingkey wanna route to a new queue. But with "for each" its not allowing me to do this. How should I do that.

// new.cpp : main project file.

#include "stdafx.h"
#include "message.h"
#include "exchange.h"
using namespace System;
using namespace System::Collections;

int main()
{
   Queue^ mainQ = gcnew Queue;
   //creating a queue and inserting messages with particular message Ids from the publisher
   mainQ->Enqueue(gcnew message(101));
   mainQ->Enqueue(gcnew message(102));
   mainQ->Enqueue(gcnew message(103));

   // Displays the properties and values of the Queue.
   Console::WriteLine("myQ\n\tCount:\t{0}:", mainQ->Count );

   for each(message^ msg in mainQ)
   { 
      Console::WriteLine(msg->routingkey);
   }
for each(message^ msg in mainQ)
   { 
  if (msg->routingkey==101)
	 {
      exchange^ E1= gcnew exchange(101);
	  Queue^ Q1 = gcnew Queue;
	  Q1->Enqueue(msg);
	  mainQ->Dequeue();
	 }
   
}

  
   
 
   //Console::WriteLine("Excnahge type is  {0}:", E1->getType());

   Console::ReadLine();
   return 0;
}

message.h ************************************
public ref class message
{
 public:
  property int routingkey;
  
  message(int Id){ routingkey = Id;}
  
};
 exchange.h********************************************
public ref class exchange
{
 public:
  bool Durable;
  int type;
  bool AutoDelete;
  bool Arguments;

 
  exchange::exchange(int t)
  {
   type=t;
   Durable= false;
   AutoDelete=false;
  }

 void exchange::DelExchange()
 {
   Durable= false;
   AutoDelete=false;
   type=0;
  }

 int exchange::getType()
  {
   return type;
  }
 
 
 void exchange::setType(int t)
  {
        type= t;
  }

};

Additional information: Collection was modified; enumeration operation may not execute.

I tried to access the element in this way but error is
'msg' : undeclared identifier
left of '->routingkey' must point to class/struct/union/generic type

While (id <= mainQ->Count)
  {
     
	  if(msg->routingkey==101)
	 {
      exchange^ E1= gcnew exchange(101);
	  Queue^ Q1 = gcnew Queue;          // this is the new queue to hold messages for routing key=101
	  Q1->Enqueue(msg);
	 }
	}
mainQ->Dequeue(); //removin message at the head of the queue.

Did you pass msg to this or is it inside a loop containing msg?
Also, using the [ CODE ] tags around your code will really help readability.
Just highlight your code and click the CODE link above.

I have used two methods one for each and in last code example I am using Queue->count to iterate every message in the queue.

Sorry I understand your question now. Yes message is inside the loop.

int id=0;
While (id <= mainQ->Count)
  {
     
	  if(message ^ msg->routingkey==101)
	 {
      exchange^ E1= gcnew exchange(101);
	  Queue^ Q1 = gcnew Queue;          // this is the new queue to hold messages for routing key=101
	  Q1->Enqueue(msg);
	 }
	}
mainQ->Dequeue(); //removin message at the head of the queue.

You get an error on line 5, right?

yes. please help how can I solve this

remove message^

I did this but it still asking for how to identify message object to iterate through queue

OK.
You would need to declare a variable or an object outside of an if-statement.

'msg' : undeclared identifier.
left of '->routingkey' must point to class/struct/union/generic type

But how can define without "for each"

How about:

#include "stdafx.h"
#include "message.h"
using namespace System;
using namespace System::Collections;

int main(/*array<System:: String ^> ^args*/)
{
   Queue^ myQ = gcnew Queue;
   myQ->Enqueue(gcnew message(101));
   myQ->Enqueue("World");
   myQ->Enqueue("!");

   // Displays the properties and values of the Queue.
   Console::WriteLine("myQ\n\tCount:\t{0}\n\tValues:", myQ->Count );

   auto arr_msg = myQ->ToArray();
   for (int i=0; i<arr_msg->Length; i++)
   {
      Console::WriteLine(arr_msg[i]);
   }

   Console::ReadLine();
   return 0;
}

Thanks for another suggestion but as I tried to access the queue messages copied to array. Its giving me the response.
Error C2039: 'MsgID' : is not a member of 'System::Object'

auto arr_msg = myQ->ToArray();
   for (int i=0; i<arr_msg->Length; i++)
   {
      Console::WriteLine(arr_msg[i]->MsgID);	
    }

Please help............

Are you using something OTHER than Visual Studio to compile this?

No, I am using visual studio 2010 Express

Hi, I forgot to tell from your last given code if I store message objects in queue. Itis just printing
"message" 3 times. Not the value of the routing key of message I can print.

You have 3 things in the Queue.
Were you expecting 1?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.