# Instance Naming

**URL:** <https://forum.kirupa.com/t/instance-naming/265114>\
**Category:** flash\
**Created:** [July 4, 2008, 4:29am UTC](https://forum.kirupa.com/t/instance-naming/265114 "2008-07-04T04:29:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevin416](https://avatars.discourse-cdn.com/v4/letter/k/51bf81/32.png) [@kevin416](https://forum.kirupa.com/u/kevin416)\
**Post date:** [July 4, 2008, 4:29am UTC](https://forum.kirupa.com/t/instance-naming/265114/1 "2008-07-04T04:29:26Z")

</div>

The basic method of making new instances is to write out

```auto

var t1:MyClass = new MyClass( 0 );
var t2:MyClass = new MyClass( 0 );
etc

```

I know there is a much quicker way to do this than writing them out, say if I want to go up to t20, but I can’t seem to quite remember how.  
Thanks for help, this should be a quick fix.

---

<div class="post-metadata">

**Author:** ![hasch2o](https://avatars.discourse-cdn.com/v4/letter/h/ba8739/32.png) [@hasch2o](https://forum.kirupa.com/u/hasch2o)\
**Post date:** [July 4, 2008, 5:07am UTC](https://forum.kirupa.com/t/instance-naming/265114/2 "2008-07-04T05:07:12Z")

</div>

How about something like:

```auto

var tArray:Array = new Array();
var MyTemp:MyClass;

for(var i:int = 0;i<20;i++){
  MyTemp = new MyClass(0);
  tArray(i) = MyTemp;
  tArray(i).name = "t" & i+1;
}

```

(probably lots of typos, don’t have flash at work to check)  
Basically with that you can reference all your objects that you create directly with the array. The name is not even necessary.

hth  
Carlo

---

<div class="post-metadata">

**Author:** ![kevin416](https://avatars.discourse-cdn.com/v4/letter/k/51bf81/32.png) [@kevin416](https://forum.kirupa.com/u/kevin416)\
**Post date:** [July 4, 2008, 6:48pm UTC](https://forum.kirupa.com/t/instance-naming/265114/3 "2008-07-04T18:48:38Z")

</div>

Thanks Carlo, but I keep getting an error when I try your method. Here’s the whole code, maybe someone can see what’s wrong.  
BTW, the error is: [Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference.  
Fault, Main.as:39

```auto

package {
	import flash.display.Sprite;
	public class Main extends Sprite {
		public var tiles:Array;
		public var t_name:String;
		private var map:Array = [
		[0, 0, 0, 0, 0],
		[0, 1, 1, 1, 0],
		[0, 1, 1, 1, 0],
		[0, 1, 1, 1, 0],
		[0, 0, 0, 0, 0]
		];

		public function Main():void {
			initTiles();	
			var t1:Tile = new Tile( 5 );
		}

		public function initTiles():void {	
			var tempTile:Tile;			
			for ( var i:int = 0; i < map.length; i++ ) {
				for ( var j:int = 0; j < map[0].length; j++ ) {
					tempTile = new Tile( map*[j] )
					trace( map*[j] )
					tiles*[j] = tempTile;					
				}
			}
			for ( i = 0; i < map.length; i++ ) {
				for ( j = 0; j < map[0].length; j++ ) {
					trace( tiles*[j] )
				}
			}
		}
	}
}
class Tile {
	
	public var walkable:Boolean;
	public var pic:int;
	
	public function Tile( setup:int ) {
		pic = setup;
		walkable = ( setup > 50 );
		
	}
	public function getWalkable():Boolean {
		return walkable;
	}
}

```

thanks for any and all help

---

<div class="post-metadata">

**Author:** ![kevin416](https://avatars.discourse-cdn.com/v4/letter/k/51bf81/32.png) [@kevin416](https://forum.kirupa.com/u/kevin416)\
**Post date:** [July 6, 2008, 2:22am UTC](https://forum.kirupa.com/t/instance-naming/265114/4 "2008-07-06T02:22:29Z")

</div>

It looks like the problem is more confusing than I thought. Will someone please tell me why this does not work, because I am at my wit’s end.

```auto

package {
	import flash.display.Sprite;
	public class Main extends Sprite {		
		public var tiles:Array;		
		public function Main():void {
		        var t1:Tile = new Tile( 5 );
			tiles.push( t1 );
		}
	}
}

class Tile {
	public var walkable:Boolean;
	public var pic:int;
	public function Tile( setup:int ) {
		pic = setup;
		walkable = ( setup > 50 );
	}
	public function toString():String {
		return "#=" + pic + " || walkable=" + walkable 
	}
}

```

I get error 1009 even for this seemingly simple piece of code.

---

<div class="post-metadata">

**Author:** ![yaim0310](https://avatars.discourse-cdn.com/v4/letter/y/85e7bf/32.png) [@yaim0310](https://forum.kirupa.com/u/yaim0310)\
**Post date:** [July 6, 2008, 2:32am UTC](https://forum.kirupa.com/t/instance-naming/265114/5 "2008-07-06T02:32:45Z")

</div>

To create an Array object, you use the new Array() constructor.

```auto
public var tiles:Array = new Array();

```

you were trying to push onto the array before you actually defined it as one

---

<div class="post-metadata">

**Author:** ![kevin416](https://avatars.discourse-cdn.com/v4/letter/k/51bf81/32.png) [@kevin416](https://forum.kirupa.com/u/kevin416)\
**Post date:** [July 6, 2008, 2:43am UTC](https://forum.kirupa.com/t/instance-naming/265114/6 "2008-07-06T02:43:46Z")

</div>

Thanks so much.  
You spared me much trouble - I thought the tiles were the ones that didn’t exist.

---

<div class="post-metadata">

**Author:** ![Pier25](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@Pier25](https://forum.kirupa.com/u/Pier25)\
**Post date:** [July 6, 2008, 12:26pm UTC](https://forum.kirupa.com/t/instance-naming/265114/7 "2008-07-06T12:26:51Z")

</div>

This is so cool…

I’m having this same problem right now… so I open kirupa forum, and find this post right there at the top without even having to search for it 😃

Thank you guys 😉
