Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / January 2007

Tip: Looking for answers? Try searching our database.

Generic SortedList clashes with SortedList (namespace collision)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
raylopez99 - 30 Jan 2007 01:31 GMT
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-
>Current,sortkeys1->Current->j,sort->IndexOfKey(sortkeys1->Current));
        Console::WriteLine("Value for key(i) is: {0}",sort[sortkeys1-
>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-
>IndexOfKey(sortkeys2->Current));
        Console::WriteLine("Value for key(i) is (hard to print):
{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 . . .
raylopez99 - 30 Jan 2007 15:50 GMT
Well I finally "fixed" this by putting the two different SortedList
(s) in their own namespace, to avoid having to use a scope resolution
operator qualifier that's 30 letters long.  There was a conflict
before because each was in main().

What amazes me is that MS's compilers don't recognize there are two
different classes from the declaration--the generic uses <,> notation
while the standard collection does not.  But so be it.

RL

> I seem to get name collision between the Generic collection SortedList
> and C++.NET Framework collection SortedList.
[quoted text clipped - 4 lines]
> using namespace System::Collections;
> using namespace System::Collections::Generic;

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.