# Referring to a variable within a class

**URL:** <https://forum.kirupa.com/t/referring-to-a-variable-within-a-class/281203>\
**Category:** Uncategorized\
**Created:** [February 6, 2009, 12:13am UTC](https://forum.kirupa.com/t/referring-to-a-variable-within-a-class/281203 "2009-02-06T00:13:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ersk](https://avatars.discourse-cdn.com/v4/letter/e/a9a28c/32.png) [@Ersk](https://forum.kirupa.com/u/Ersk)\
**Post date:** [February 6, 2009, 12:13am UTC](https://forum.kirupa.com/t/referring-to-a-variable-within-a-class/281203/1 "2009-02-06T00:13:08Z")

</div>

Ok, so I have found a tutorial which shows me how to create a class. It also shows me how to construct an object in the main code. What I want to do is to access a variable from the object that I made. Heres the code…

This is the link to the tutorial:  
[http://flash.lillegutt.com/?p=29](http://flash.lillegutt.com/?p=29)

the Car.as file:

```auto
package  
{  
public class Car  
{  
	// defining attributes to the car  
	private var _name:String = "My custom Porche";  
	private var _engine:Number = 6.3;  
	private var _numberWheels:int = 4;  
	private var _doors:String = "Elevating doors";  
  
	// The Car constructor  
	public function Car(name:String="custom car", engine:Number=0, numberWheels:int=4, doors:String="")  
	{  
		// setting the global attributes to the incoming variables  
		_name = name;  
		_engine = engine;  
		_numberWheels = numberWheels;  
		_doors = doors;  
	}  
}  
}

```

Then I use this code to construct the object, which in this case is a car (the code is in my main flash file):

```auto
var mycar:Car = new Car("Porche", 3.2, 4, "Normal doors");

```

I understand all of the code, but how do I reference a variable like the engine? I have tried lots of things like “trace(this[mycar].engine)”, "trace(mycar.engine) but nothing seems so work. I just get error messages of property undefined.

Any help would be really appreciated, thanks.
