C# Workshop - Week 1 (Ch. 1 & 2) - Advanced

Started by
337 comments, last by paulecoyote 17 years, 1 month ago
The thing you're missing is the concept of scope. In the specification under Basic Concepts is a section on 'Scopes'; it's outside of this week's area, but should help. Your problem isn't using the array, it's understanding how different classes are separate.
Advertisement
You declared the variable but never gave it any memory.

It is the same as doing this.

myClass a;

To use myClass a I need to create an instance of it. myClass a = new myClass();

For your problem you need to add a static constructor and give the array some memory.

static MapEngine(){     MapArray = new string[xMaxArray, yMaxArray];     for(x = 0; x < xMaxArray; x++)        for(y = 0; y < yMaxArray; y++)           MapArray[x,y] = new string();}


That will do what you are trying to do.

Also to call MapArray you need to use the static call. MapEngine.MapArray.

Hope that helps.
theTroll


[source lang="C#" ]using System;using System.Collections;using System.Text;namespace WORKSHOP_01{    class MapEngine    {        private int xMaxArray = 40;        private int yMaxArray = 40;        static string[,] MapArray;         public MapEngine()        {            MapArray = new string[ModxMaxArray, ModyMaxArray];        }        public int ModxMaxArray        {            get { return xMaxArray; }            set { xMaxArray = value; }        }        public int ModyMaxArray        {            get { return yMaxArray; }            set { yMaxArray = value; }        }        public void InsertItem(string Item, int X, int Y)        {            MapArray.SetValue(Item, X, Y);        }        public string ReadItem(int X, int Y)        {            return (string)(MapArray.GetValue(X, Y));        }    }    class Unit    {        private string ID;        private string Type;        private string Status;        private int pHealth;        private int pAttack;        private int pDefense;        private int pCharge;        private int pMorale;        private int Range;        private int Speed;        private int Posx;        private int Posy;        private int Angle;        private MapEngine map;        // Constructor        public Unit(){            map = new MapEngine();        }        public void Die()        {            pHealth = 0;            Status = "Dead";            map.InsertItem("Empty", Posx, Posy); // i don't know if it will go at runtime..        }        public void Move(string Where, int Heading)        {            Angle = Heading;            switch (Where)            {                case "Forward":                    if (map.ReadItem(Posx, (Posy + 1)) == "Empty")                    {                        if (Posx != 60)                        {                            map.InsertItem("Empty", Posx, Posy);                            Posy += 1;                            map.InsertItem(ID, Posx, Posy);                            Status = "Moving Forward!";                        }                        else Status = "We cant Advance Anymore!";                    }                    else Status = "We can't advance, the enemy is holding that position!";                    break;            }        }        static void Main()        {            Unit gigi = new Unit();            // Very short life!!!            gigi.Move();            gigi.Die();        }    }    }
You should be able to remove your own post by editing it and checking the 'Delete this post' box above the text box. Then just submit the changes.

Where can I find the MS naming scheme? I've heard it referenced a couple of times and would like to follow its standards (especially since it seems pretty similar to what I'm used to).
Quote: Original post by Ezbez
You should be able to remove your own post by editing it and checking the 'Delete this post' box above the text box. Then just submit the changes.

Where can I find the MS naming scheme? I've heard it referenced a couple of times and would like to follow its standards (especially since it seems pretty similar to what I'm used to).

see This post

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Quote: private MapEngine map;

// Constructor
public Unit(){
map = new MapEngine();
}

public void Die()
{
pHealth = 0;
Status = "Dead";
map.InsertItem("Empty", Posx, Posy); // i don't know if it will go at runtime..


this was a typo, map was an older name i had.
So far i cant get it to work, but i am advancing. Usiing this the error i get is:
AncientWarGame.MapEngine.MapArray' is inaccessible due to its protection level.
The code goes like this:
 class MapEngine    {        private byte xMaxArray = 40;        private byte yMaxArray = 40;        static string[,] MapArray;        public MapEngine()        {            MapArray = new string[xMaxArray, yMaxArray];        }        public byte ModxMaxArray        {            get { return xMaxArray; }            set { xMaxArray = value; }        }        public byte ModyMaxArray        {            get { return yMaxArray; }            set { yMaxArray = value; }        }                public void InsterItem(string Item, byte X, byte Y)        {            MapArray.SetValue(Item, X, Y);        }        public string ReadItem(byte X, byte Y)        {            return MapArray.GetValue(X, Y);        }    }    class Unit    {        private string ID;        private string Type;        private string Status;        private int pHealth;        private int pAttack;        private int pDefense;        private int pCharge;        private int pMorale;        private int Range;        private int Speed;        private byte Posx;        private byte Posy;        private int Angle;        public string getStatus        {            get{ return Status; }        }        public void Die()        {            pHealth = 0;            Status = "Dead";            MapEngine.MapArray.InsertItem("Empty", Posx, Posy);        }        public void TakeDamage(int Damage)        {            if(pDefense < Damage)            pHealth += (pDefense - Damage);        }        public void Move(string Where, int Heading)        {            Angle = Heading;            switch (Where)            {                case "Forward":                    if (MapEngine.MapArray.ReadItem(Posx, (Posy + 1)) = "Empty")                    {                        if (Posx != 60)                        {                            MapEngine.MapArray.InsertItem("Empty", Posx, Posy);                            Posy += 1;                            MapEngine.MapArray.InsertItem(ID, Posx, Posy);                            Status = "Moving Forward!";                        }                        else Status = "We cant Advance Anymore!";                    }                    else Status = "We can't advance, the enemy is holding that position!";                    break;            }        }

If i change static string[,] MapArray; to public get these 2 errors:
An object reference is required for the nonstatic field, method, or property 'AncientWarGame.MapEngine.MapArray'
and
'System.Array' does not contain a definition for 'InsertItem'

[Edited by - alvarofrank on July 12, 2007 5:20:58 PM]
public changes visibility, static makes a value shared between all instances of a class rather then having a seperate instance per class.

public and static are not mutually exclusive.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
Ah, thank you, Washu.
Quote: Original post by alvarofrank
Quote: private MapEngine map;

// Constructor
public Unit(){
map = new MapEngine();
}

public void Die()
{
pHealth = 0;
Status = "Dead";
map.InsertItem("Empty", Posx, Posy); // i don't know if it will go at runtime..


this was a typo, map was an older name i had.
So far i cant get it to work, but i am advancing. Usiing this the error i get is:
AncientWarGame.MapEngine.MapArray' is inaccessible due to its protection level.
The code goes like this:
*** Source Snippet Removed ***
If i change static string[,] MapArray; to public get these 2 errors:
An object reference is required for the nonstatic field, method, or property 'AncientWarGame.MapEngine.MapArray'
and
'System.Array' does not contain a definition for 'InsertItem'


Ok, static members can only be accessed by static methods.

So for initialization you have to use the static constructor. For any method accessing it, you need to make it a public static method.

Your ReadItem method would become, public static string ReadItem(byte x, byte y).

To call your ReadItem method you would do this; MapEngine.ReadItem(x,y);

Hope that answers your questions.
theTroll

THNKS that was the issue! My problem was ussing a badconstructor, now i use
"public static Array MapArray = Array.CreateInstance(typeof(String), 60, 60);"
My only issue now is that the of statement is not of type bool :(

This topic is closed to new replies.

Advertisement