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;
}

Edited 4 Years Ago by Narue: Added code tags

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.

Edited 4 Years Ago by thines01: formatting

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

Thank you very much.

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

Edited 3 Years Ago by Dani: Formatting fixed

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.

Edited 4 Years Ago by thines01: clarity

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;
  }

};

Edited 4 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

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.

Edited 4 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

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.

Edited 4 Years Ago by thines01: additional

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.

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

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

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);	
    }

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.

This question has already been answered. Start a new discussion instead.