R
raylopez99
I seem to get name collision between the Generic collection SortedList
and C++.NET Framework collection SortedList.
How to resolve? Here are the libraries that seem to clash:
System::Collections::SortedList,
System::Collections::Generic::SortedList,
using namespace System::Collections;
using namespace System::Collections::Generic;
Below is a working version of the generic template SortedList, which
works fine, but clashes with everything in the C++.NET library
(SortedList and even Hashtable), so I cannot compile any NET
SortedList or Hashtable in the same translation unit.
RL
/////////////
// Generic Sorted List // January 29, 2007
// main project file.
// Eclass.h file
#pragma once
using namespace System;
using namespace System::Collections::Generic;
public ref class Inventory: public IComparable<Inventory^ > {
public:
virtual Int32 CompareTo(Inventory^ other2);
public:
Inventory(void);
Inventory(int i);
int j;
};
//////////// Eclass.cpp
#include "StdAfx.h"
#include "EClass.h"
Inventory::Inventory():j(123){}
Inventory::Inventory(int i): j(i){}
Int32 Inventory::CompareTo(Inventory ^other2){
Int32 temp;
if (this->j == other2->j)
temp=0;
if (this->j < other2->j)
temp=1;//opposite of normal usage, ie. temp=-1; (to give
inverse sort)
if (this->j > other2->j)
temp=-1; //opposite of normal usage, ie. temp=1; (to give
inverse sort)
return (temp);
}
///////////////////
/// main.cpp
#include "stdafx.h"
#include "EClass.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
Inventory ^ myInv1 = gcnew Inventory();
Inventory ^ myInv2 = gcnew Inventory(321);
SortedList<Inventory^, int>^ sort = gcnew SortedList<Inventory^,
int>();
sort->Add(myInv1, 11); //keys, values is left to right
sort->Add(myInv2, 22);
Console::WriteLine("\n My enumerator SortedList");
IEnumerator<Inventory^> ^sortkeys1 = sort->Keys->GetEnumerator(); //
works! note format of IEnumerator generic class
while (sortkeys1->MoveNext()) {
Console::WriteLine("current!{0}, value!{1}, IofK {2}",sortkeys1-
}
Console::WriteLine("\n \n Value for key(0) is: {0}, for key(1) is:
{1}",sort[myInv1],sort[myInv2]);
////////// now try reversing it!
SortedList<int, Inventory^>^ sort2 = gcnew SortedList<int,
Inventory^>();
sort2->Add(11, myInv1); //keys, values is left to right
sort2->Add(22, myInv2);
Console::WriteLine("\n my enumerator SortedList2");
IEnumerator<int> ^sortkeys2 = sort2->Keys->GetEnumerator(); //works!
note format of IEnumerator generic class
while (sortkeys2->MoveNext()) {
Console::WriteLine("current!{0}, IofK {1}",sortkeys2->Current,sort2-
{0}",sort2[sortkeys2->Current]);
}
Console::WriteLine("\n \n Value for key(0) is: {0}, for key(1) is:
{1}",sort2[11],sort2[22]);
return 0;
}
//////////
// output:
Hello World
By enumerator SortedList
current!Inventory, value!321, IofK 0
Value for key(i) is: 22
current!Inventory, value!123, IofK 1
Value for key(i) is: 11
Value for key(0) is: 11, for key(1) is: 22
my enumerator SortedList2
current!11, IofK 0
Value for key(i) is (hard to print): Inventory
current!22, IofK 1
Value for key(i) is (hard to print): Inventory
Value for key(0) is: Inventory, for key(1) is: Inventory
Press any key to continue . . .
and C++.NET Framework collection SortedList.
How to resolve? Here are the libraries that seem to clash:
System::Collections::SortedList,
System::Collections::Generic::SortedList,
using namespace System::Collections;
using namespace System::Collections::Generic;
Below is a working version of the generic template SortedList, which
works fine, but clashes with everything in the C++.NET library
(SortedList and even Hashtable), so I cannot compile any NET
SortedList or Hashtable in the same translation unit.
RL
/////////////
// Generic Sorted List // January 29, 2007
// main project file.
// Eclass.h file
#pragma once
using namespace System;
using namespace System::Collections::Generic;
public ref class Inventory: public IComparable<Inventory^ > {
public:
virtual Int32 CompareTo(Inventory^ other2);
public:
Inventory(void);
Inventory(int i);
int j;
};
//////////// Eclass.cpp
#include "StdAfx.h"
#include "EClass.h"
Inventory::Inventory():j(123){}
Inventory::Inventory(int i): j(i){}
Int32 Inventory::CompareTo(Inventory ^other2){
Int32 temp;
if (this->j == other2->j)
temp=0;
if (this->j < other2->j)
temp=1;//opposite of normal usage, ie. temp=-1; (to give
inverse sort)
if (this->j > other2->j)
temp=-1; //opposite of normal usage, ie. temp=1; (to give
inverse sort)
return (temp);
}
///////////////////
/// main.cpp
#include "stdafx.h"
#include "EClass.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
Inventory ^ myInv1 = gcnew Inventory();
Inventory ^ myInv2 = gcnew Inventory(321);
SortedList<Inventory^, int>^ sort = gcnew SortedList<Inventory^,
int>();
sort->Add(myInv1, 11); //keys, values is left to right
sort->Add(myInv2, 22);
Console::WriteLine("\n My enumerator SortedList");
IEnumerator<Inventory^> ^sortkeys1 = sort->Keys->GetEnumerator(); //
works! note format of IEnumerator generic class
while (sortkeys1->MoveNext()) {
Console::WriteLine("current!{0}, value!{1}, IofK {2}",sortkeys1-
Console::WriteLine("Value for key(i) is: {0}",sort[sortkeys1-Current,sortkeys1->Current->j,sort->IndexOfKey(sortkeys1->Current));
Current]);
}
Console::WriteLine("\n \n Value for key(0) is: {0}, for key(1) is:
{1}",sort[myInv1],sort[myInv2]);
////////// now try reversing it!
SortedList<int, Inventory^>^ sort2 = gcnew SortedList<int,
Inventory^>();
sort2->Add(11, myInv1); //keys, values is left to right
sort2->Add(22, myInv2);
Console::WriteLine("\n my enumerator SortedList2");
IEnumerator<int> ^sortkeys2 = sort2->Keys->GetEnumerator(); //works!
note format of IEnumerator generic class
while (sortkeys2->MoveNext()) {
Console::WriteLine("current!{0}, IofK {1}",sortkeys2->Current,sort2-
Console::WriteLine("Value for key(i) is (hard to print):IndexOfKey(sortkeys2->Current));
{0}",sort2[sortkeys2->Current]);
}
Console::WriteLine("\n \n Value for key(0) is: {0}, for key(1) is:
{1}",sort2[11],sort2[22]);
return 0;
}
//////////
// output:
Hello World
By enumerator SortedList
current!Inventory, value!321, IofK 0
Value for key(i) is: 22
current!Inventory, value!123, IofK 1
Value for key(i) is: 11
Value for key(0) is: 11, for key(1) is: 22
my enumerator SortedList2
current!11, IofK 0
Value for key(i) is (hard to print): Inventory
current!22, IofK 1
Value for key(i) is (hard to print): Inventory
Value for key(0) is: Inventory, for key(1) is: Inventory
Press any key to continue . . .