Skip to main content
GameDev.net gamedev.net
🔒 Locked

Accessing ArrayList elements.

Started by APCJim Sep 26, 2005 at 4:34 PM 1 replies 550+ views
Original Post
APCJim
APCJim
In VC++.NET 2003 I'm adding a pointer to a process measurement object to an ArrayList. How do I access the process measurement object after including them in the ArrayList? When I try to compile the code I get the following error: error C2440: '=' : cannot convert from 'System::Collections::ArrayList __gc *' to 'Measurement_Class __gc *' It fails when I try to access the ArrayList elemnent: "new_Measurement = measurement_array->Item[0];" It also fails when I try to access the data in this manner: String* tempName = measurement_array->Item[0]->Name; etc. The process measurement class (probably irrelevant to the problem):

#pragma once
#using <mscorlib.dll>
using namespace System;

public __gc class Measurement_Class
{
public:
	Measurement_Class()
	{
	}
	Measurement_Class(String* new_Name, double new_Value)
	{
		initialize_Measurement(new_Name, new_Value);
	}
	__property void set_Name(String* new_Name){name = new_Name;}
	__property String* get_Name(){return name;}
	__property void set_Current_Value(double new_Current_Value){current_value = new_Current_Value;}
	__property double get_Current_Value(){return current_value;}
	__property void set_Size_Of_Past_Values_Array(int new_Size_Of_Array){size_of_past_values_array = new_Size_Of_Array;}
	__property int get_Size_Of_Past_Values_Array(){return size_of_past_values_array;}
	void initialize_Measurement(String* new_Name, double new_Current_Value)
	{
		Name = new_Name;
		Current_Value = new_Current_Value;

		Size_Of_Past_Values_Array = 10080;
		create_Past_Values_Array();
		set_All_Past_Values(Current_Value);
	}
  void create_Past_Values_Array()
	{
		past_value = new double __gc[Size_Of_Past_Values_Array];
	}
  void set_All_Past_Values(double new_Value)
	{
		for (int i = 0; i < Size_Of_Past_Values_Array; ++i){past_value = new_Value;}
	}
	double get_Past_Value(int index)
	{
		return past_value[index];
	}
	void set_Past_Value(int index, double new_Value)
	{
		past_value[index] = new_Value;
	}
	void modify_Measurement(String* new_Name, double new_Current_Value, double new_Past_Values)
	{
		Name = new_Name;
		Current_Value = new_Current_Value;
		set_All_Past_Values(new_Past_Values);
	}

	void modify_Measurement(String* new_Name, double new_Current_Value)
	{
		Name = new_Name;
		Current_Value = new_Current_Value;
	}

private:
	String* name;
	double current_value;
	double past_value __gc[];
	int size_of_past_values_array;
};


The ArrayList Declaration:

	private: static ArrayList* measurement_array = new ArrayList;



The code that will not compile:

private: System::Void button_Add_Click(System::Object *  sender, System::EventArgs *  e)
{
  Measurement_Class* new_Measurement = new Measurement_Class(

  textBox_Name->Text,

  Double::Parse(textBox_Value->Text));


  measurement_array->Add(new_Measurement);
  new_Measurement = measurement_array->Item[0];
  new_Measurement = NULL;
}


ldeej
ldeej
You need a cast:

new_Measurement = static_cast(measurement_array->Item[0]);

The returned object by Item[0] is of type object, and you are assigning it to an object of type Measurement_Class. Since there is no implicit conversion between the two you need to make it explicit by adding a cast.
APCJim
APCJim
That did it. Thanks for your help.

I found the type cast reference you showed listed under polymorphism; obviously another area of OOP I need to study more.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.