Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi Friends,
below is my code snippet.

C++
#include "stdafx.h"

#using "system.dll"
using namespace System;
using namespace System::Collections::Generic;


int _tmain(int argc, _TCHAR* argv[])
{
	
	Dictionary<string^,string^>^ storeDir = gcnew  Dictionary<string^,>();	

	storeDir->Add("G02"	,"G01");
	storeDir->Add("G23"	,"G02");
	storeDir->Add("G45"	,"G03");
	storeDir->Add("G55"	,"G04");

	for each (KeyValuePair<string^,> p in storeDir)
	{
		Console::WriteLine("{0} ==== {1}\n",p.Key,p.Value);
	}

	String^ StringstrtobeSearch =  "G55";
	String^ strgatevalue;

	if (storeDir->TryGetValue(StringstrtobeSearch,strgatevalue))
	{
		Console::WriteLine(strgatevalue);
	}
	storeDir->Remove("G23");
	Console::WriteLine("Remove data from Store Directory \n");
	for each (KeyValuePair<string^,> p in storeDir)
	{
		Console::WriteLine("{0} ==== {1}\n",p.Key,p.Value);
	}
	storeDir->Add("G23"	,"G02");

	Console::WriteLine("After adding again\n");
	for each (KeyValuePair<string^,> p in storeDir)
	{
		Console::WriteLine("{0} ==== {1}\n",p.Key,p.Value);
	}
	return 0;
}


I have a scenario where I have to remove "storeDir->Add("G23" ,"G02");" from Dictionary and then I have to add in such a way that
"storeDir->Add("G23" ,"G02");" should come at the end.
but here in the above example it always sort the directory.
is there any other container which i can use to achive my objectice ?
Posted
Updated 17-Jun-15 1:35am
v2

A dictionary is lookup object which stores the data somehow "internally" sorted. If you want to have a special order you better use an array. But it has not such lookup function and you must iterate to find a specific value.
 
Share this answer
 
You can't do it with dictionary as it has no notion of order. Instead, use NameValueCollection Class[^] wihch can be accessed either with the key or with the index.e.g.
C#
using System;
using System.Collections.Specialized;

public class Program
{
    public static void Main()
    {
        NameValueCollection grade = new NameValueCollection();

        grade.Add("excellent", "80");
        grade.Add("good", "60");
        grade.Add("average", "50");
        grade.Add("fail", "40");

        Console.WriteLine("Before: " + grade[3]);

        grade.Remove("good");
        grade.Add("good","60");

        Console.WriteLine("After: " + grade[3]);
    }
}

The outcome:
VB
Before: 40
After: 60
 
Share this answer
 
v4
Conceptually, a dictionary does not have a beginning and an end - just a key and value.
Adding a new key and value will add at the last location.
 
Share this answer
 
Hi All,
Thanks for your prompt response. special thanks to "Peter Leow" - your advice helped me a lot to get my work done.
Thanks again.

Regards,
praveer
 
Share this answer
 
Comments
[no name] 17-Jun-15 22:30pm    
You have added this as a solution. Suggest you delete this and add a comment to your question.
Member 4201820 18-Jun-15 5:28am    
Hi All,
Thanks for your prompt response. special thanks to "Peter Leow" - your advice helped me a lot to get my work done.
Thanks again.

Regards,
praveer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900