I've created a class PulldownArray, which contains two strings Value and Description. I created an array of this class called PulldownTABLE with the ArrayList^ command in my main program and I then populate it with the .Add method. All this works fine.

How do I retrive the Value and Description strings for an individual item in PulldownTABLE?

public ref class PulldownArray{
	public:
		String^ myValue;
		String^ myDescription;
	public:
		~PulldownArray(){
		}
		PulldownArray( String^ strValue, String^ strDescription ){
			this->myValue = strValue;
			this->myDescription = strValue + " \t " + strDescription;
		}
	property
		String^ Value{
			String^ get(){
			return myValue;
			}
		}
	property
		String^ Description{
			String^ get(){
				return myDescription;
			}
	   }
	virtual 
		String^ ToString() override{
		   return this->myValue;
	    }
};


main{.....

String^ ReturnString;

ArrayList^ PulldownTABLE = gcnew ArrayList;
PulldownTABLE->Add(gcnew PulldownArray("TABLE1", "TABLE1_description"));
PulldownTABLE->Add(gcnew PulldownArray("TABLE2", "TABLE2_description"));

//Works, but I don't want to have to write overrides for every element
ReturnString = PulldownTABLE[1]->ToString();
//Can see both myDescription and Description in the debugger but these both fail at compile
ReturnString = PulldownTABLE[1]->myDescription;
ReturnString = PulldownTABLE[1]->Description();
...
}

Any help would be appreciated but please keep it simple as I'm particularily stupid and ignorant.

Because of how the ArrayList stores objects (as objects of type Object ^) you need to cast your ArrayList member before it will give up it's public properties and methods.

ReturnString = (static_cast<PulldownArray^>(PulldownTABLE[0]))->Description;
	
//Consider the List instead, no need to cast
List<PulldownArray^> ^ pdtl = gcnew List<PulldownArray^>();
String ^ returnstring2 = pdtl[1]->Description;

The List is in System::Collections::Generic but if you are using an older .NET you may be stuck with ArrayList

Hi Jonsca (love your title!). Thank you muchly for the tip on the casting of the ArrayList member. I definitely give that a try.

I'm using Visual C++ 2008 so I probably do have access to the List. BUT I'm currently teaching myself C++ so it's going to take me a couple of days to assimulate what you've suggested here. It certainly looks a whole lot neater, cleaner and whole heap more elegant so I'm interested....

Thanks once again.

Thanks!

Yeah,ArrayList was a .NET 2.0 thing. I've never dissected them but I believe the List is better designed behind the scenes. I don't know if there's any speed improvement.

Take your time with it. The C++ that comes with the CLR bits is different from standard C++. All the ref classes, handles (^), etc. are particular to the C++/CLI dialect. Just something to bear in mind while you are learning this. If you decide to branch into C# eventually you'll find this to be a hair closer to C# than standard c++ (in that C# and C++/CLI both use the .NET libraries). Just my 0.02.

Edited 6 Years Ago by jonsca: n/a

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