This code is for a friend's WinMo application, but the problem is when he calls a method it resets all of the array's values to 0, but it isn't the code within the method that does it but the act of calling the method.

double[] enPosX = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
double[] enPosY = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };

Under "class VEarth" both get filled, but they get reset to 0 as shown above when "coll.isCollided()" is called under Form1.cs.

I am not experienced with the C# class layout and I got a feeling it has something to do with the coll.isCollided() making the code in Collision create a new "VEarth" even though coll was already is initialized and the code within isCollided isn't causing it, but I'm not sure, and I need somebody's help with this.

Collision.cs

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace GlobalFighter
{
    class Collision
    {
        VEarth ve;
        Player p1;
        Enemy[]enemie = { new Enemy(0, 0, 0), new Enemy(0, 0, 0), new Enemy(0, 0, 0), new Enemy(0, 0, 0), new Enemy(0, 0, 0), new Enemy(0, 0, 0), new Enemy(0, 0, 0), new Enemy(0, 0, 0), new Enemy(0, 0, 0) };
        
        public Collision()
        {
            ve = new VEarth();
            p1 = new Player();
        }

        public Collision(double x, double y, int hp)
        {
            ve = new VEarth();
            p1 = new Player(x, y, hp);
        }

        public void setPlayerPos(double x, double y)
        {
            p1.XPos = x;
            p1.YPos = y;
        }

        public void addEnemy()
        {
            for (int i = 0; i < enemie.Length; i++)
            {
                enemie[i] = ve.enemies[i];
            }
        }

        public bool isCollided()
        {
            ve.setPositions();
            addEnemy();
            Boolean coll = false;
            for(int i=0;i<enemie.Length;i++)
            {
                if (Math.Round(p1.XPos, 6) == Math.Round(enemie[i].XPos, 6) && Math.Round(p1.YPos, 6) == Math.Round(enemie[i].YPos, 6))
                {
                    coll = true;
                }
            }
            
            return coll;
        }
    }
}

VEarth.cs

using System;
using System.Net;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace GlobalFighter
{
    class VEarth : IDisposable
    {
        VEarthImagery.MapUriRequest mapRequest;
        VEarthImagery.ImageryService iService;
        string VEarthToken = "AjEoQ0Zdram8QflPadNoCIlGhiG163Tsgn-5oF2ebN72sw7X9K_doo3XaTLfj6eU";

        Random rnd;
        String[] coordEnd;
        double[] enPosX = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        double[] enPosY = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        public Enemy[] enemies = { new Enemy(0, 0, 0), new Enemy(0, 0, 0), new Enemy(0, 0, 0), new Enemy(0, 0, 0), new Enemy(0, 0, 0), new Enemy(0, 0, 0), new Enemy(0, 0, 0), new Enemy(0, 0, 0), new Enemy(0, 0, 0) };
        double latt=0;
        double lonn=0;


        public VEarth()
        {
            rnd = new Random();
            coordEnd = new String[19];
            for (int i = 0; i < coordEnd.Length; i++)
            {
                coordEnd[i] = "0.000" + rnd.Next(7777, 42822).ToString();
            }
        }

        public VEarth(VEarthImagery.MapStyle mapStyle, VEarthImagery.SizeOfint mapSize)
        {
            InitializeImageryService(mapStyle, mapSize);
            rnd = new Random();
            coordEnd = new String[19];
            for (int i = 0; i < coordEnd.Length; i++)
            {
                coordEnd[i] = "0.000" + rnd.Next(7777, 42822).ToString();
            }            
        }

        public void setPositions()
        {
            for (int i = 0; i < enPosX.Length; i++)
            {
                enemies[i] = new Enemy(enPosX[i], enPosY[i], 100);
            }            
        }

        public Image getMap(double lat, double lon, int zoom, VEarthImagery.MapStyle mapStyle)
        {
            iService = new VEarthImagery.ImageryService();
            mapRequest.Center = new VEarthImagery.Location()
            {
                Latitude = lat,
                LatitudeSpecified = true,
                Longitude = lon,
                LongitudeSpecified = true
            };

            if (latt == 0) latt = lat;
            if (lonn == 0) lonn = lon;

            mapRequest.Pushpins[0].Location.Latitude = lat;
            mapRequest.Pushpins[0].Location.LatitudeSpecified = true;
            mapRequest.Pushpins[0].Location.Longitude = lon;
            mapRequest.Pushpins[0].Location.LongitudeSpecified = true;

            mapRequest.Pushpins[1].Location.Latitude = latt + Convert.ToDouble(coordEnd[0]);
            mapRequest.Pushpins[1].Location.LatitudeSpecified = true;
            mapRequest.Pushpins[1].Location.Longitude = lonn + Convert.ToDouble(coordEnd[1]);
            mapRequest.Pushpins[1].Location.LongitudeSpecified = true;
            enPosX[0] = mapRequest.Pushpins[1].Location.Latitude;
            enPosY[0] = mapRequest.Pushpins[1].Location.Longitude;

            mapRequest.Pushpins[2].Location.Latitude = latt - Convert.ToDouble(coordEnd[2]);
            mapRequest.Pushpins[2].Location.LatitudeSpecified = true;
            mapRequest.Pushpins[2].Location.Longitude = lonn - Convert.ToDouble(coordEnd[3]);
            mapRequest.Pushpins[2].Location.LongitudeSpecified = true;
            enPosX[1] = mapRequest.Pushpins[2].Location.Latitude;
            enPosY[1] = mapRequest.Pushpins[2].Location.Longitude;

            mapRequest.Pushpins[3].Location.Latitude = latt + Convert.ToDouble(coordEnd[4]);
            mapRequest.Pushpins[3].Location.LatitudeSpecified = true;
            mapRequest.Pushpins[3].Location.Longitude = lonn - Convert.ToDouble(coordEnd[5]);
            mapRequest.Pushpins[3].Location.LongitudeSpecified = true;
            enPosX[2] = mapRequest.Pushpins[3].Location.Latitude;
            enPosY[2] = mapRequest.Pushpins[3].Location.Longitude;

            mapRequest.Pushpins[4].Location.Latitude = latt - Convert.ToDouble(coordEnd[6]);
            mapRequest.Pushpins[4].Location.LatitudeSpecified = true;
            mapRequest.Pushpins[4].Location.Longitude = lonn + Convert.ToDouble(coordEnd[7]);
            mapRequest.Pushpins[4].Location.LongitudeSpecified = true;
            enPosX[3] = mapRequest.Pushpins[4].Location.Latitude;
            enPosY[3] = mapRequest.Pushpins[4].Location.Longitude;

            mapRequest.Pushpins[5].Location.Latitude = latt + Convert.ToDouble(coordEnd[8]);
            mapRequest.Pushpins[5].Location.LatitudeSpecified = true;
            mapRequest.Pushpins[5].Location.Longitude = lonn + Convert.ToDouble(coordEnd[9]);
            mapRequest.Pushpins[5].Location.LongitudeSpecified = true;
            enPosX[4] = mapRequest.Pushpins[5].Location.Latitude;
            enPosY[4] = mapRequest.Pushpins[5].Location.Longitude;

            mapRequest.Pushpins[6].Location.Latitude = latt - Convert.ToDouble(coordEnd[10]);
            mapRequest.Pushpins[6].Location.LatitudeSpecified = true;
            mapRequest.Pushpins[6].Location.Longitude = lonn - Convert.ToDouble(coordEnd[11]);
            mapRequest.Pushpins[6].Location.LongitudeSpecified = true;
            enPosX[5] = mapRequest.Pushpins[6].Location.Latitude;
            enPosY[5] = mapRequest.Pushpins[6].Location.Longitude;

            mapRequest.Pushpins[7].Location.Latitude = latt + Convert.ToDouble(coordEnd[12]);
            mapRequest.Pushpins[7].Location.LatitudeSpecified = true;
            mapRequest.Pushpins[7].Location.Longitude = lonn - Convert.ToDouble(coordEnd[13]);
            mapRequest.Pushpins[7].Location.LongitudeSpecified = true;
            enPosX[6] = mapRequest.Pushpins[7].Location.Latitude;
            enPosY[6] = mapRequest.Pushpins[7].Location.Longitude;

            mapRequest.Pushpins[8].Location.Latitude = latt - Convert.ToDouble(coordEnd[14]);
            mapRequest.Pushpins[8].Location.LatitudeSpecified = true;
            mapRequest.Pushpins[8].Location.Longitude = lonn + Convert.ToDouble(coordEnd[15]);
            mapRequest.Pushpins[8].Location.LongitudeSpecified = true;
            enPosX[7] = mapRequest.Pushpins[8].Location.Latitude;
            enPosY[7] = mapRequest.Pushpins[8].Location.Longitude;

            mapRequest.Pushpins[9].Location.Latitude = latt + Convert.ToDouble(coordEnd[16]);
            mapRequest.Pushpins[9].Location.LatitudeSpecified = true;
            mapRequest.Pushpins[9].Location.Longitude = lonn + Convert.ToDouble(coordEnd[17]);
            mapRequest.Pushpins[9].Location.LongitudeSpecified = true;
            enPosX[8] = mapRequest.Pushpins[9].Location.Latitude;
            enPosY[8] = mapRequest.Pushpins[9].Location.Longitude;

            mapRequest.Options.ZoomLevel = zoom;
            mapRequest.Options.ZoomLevelSpecified = true;
            mapRequest.Options.Style = mapStyle;

            var mapResponse = iService.GetMapUri(mapRequest);

            HttpWebResponse response = (HttpWebResponse)((HttpWebRequest)WebRequest.Create(mapResponse.Uri)).GetResponse();

            Image img = new Bitmap(response.GetResponseStream());
            response.Close();

            return img;
        }


        public void InitializeImageryService(VEarthImagery.MapStyle mapStyle, VEarthImagery.SizeOfint mapSize)
        {
            var you = new VEarthImagery.Pushpin
            {
                IconStyle = "16",
                Location = new VEarthImagery.Location()
            };

            var ghost1 = new VEarthImagery.Pushpin
            {
                IconStyle = "19",
                Location = new VEarthImagery.Location()
            };

            var ghost2 = new VEarthImagery.Pushpin
            {
                IconStyle = "19",
                Location = new VEarthImagery.Location()
            };

            var ghost3 = new VEarthImagery.Pushpin
            {
                IconStyle = "19",
                Location = new VEarthImagery.Location()
            };

            var ghost4 = new VEarthImagery.Pushpin
            {
                IconStyle = "19",
                Location = new VEarthImagery.Location()
            };

            var ghost5 = new VEarthImagery.Pushpin
            {
                IconStyle = "19",
                Location = new VEarthImagery.Location()
            };

            var ghost6 = new VEarthImagery.Pushpin
            {
                IconStyle = "19",
                Location = new VEarthImagery.Location()
            };

            var ghost7 = new VEarthImagery.Pushpin
            {
                IconStyle = "19",
                Location = new VEarthImagery.Location()
            };

            var ghost8 = new VEarthImagery.Pushpin
            {
                IconStyle = "19",
                Location = new VEarthImagery.Location()
            };

            var ghost9 = new VEarthImagery.Pushpin
            {
                IconStyle = "19",
                Location = new VEarthImagery.Location()
            };

            var ghost10 = new VEarthImagery.Pushpin
            {
                IconStyle = "19",
                Location = new VEarthImagery.Location()
            };

            var mapUriOptions = new VEarthImagery.MapUriOptions
            {
                Style = mapStyle,
                StyleSpecified = true,
                ImageSize = mapSize,
                ImageType = VEarthImagery.ImageType.Gif,
                ImageTypeSpecified = true
            };

            mapRequest = new VEarthImagery.MapUriRequest
            {
                Credentials = new VEarthImagery.Credentials
                {
                    Token = VEarthToken
                },
                Pushpins = new VEarthImagery.Pushpin[10]
                {
                    you,
                    ghost1,
                    ghost2,
                    ghost3,
                    ghost4,
                    ghost5,
                    ghost6,
                    ghost7,
                    ghost8,
                    ghost9
                },
                Options = mapUriOptions
            };
        }

        #region IDisposable Members

        public void Dispose()
        {
            iService.Dispose();
        }

        #endregion
    }
}

Form1.cs

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Samples.Location;

namespace GlobalFighter
{
    public partial class Form1 : Form
    {
        Gps gps = new Gps();
        GpsPosition pos;
        Collision coll;

        VEarth ve;
        VEarthImagery.MapStyle mapStyle = VEarthImagery.MapStyle.AerialWithLabels;

        Boolean hasImage = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var mapSize = new VEarthImagery.SizeOfint
            {
                Height = mapView.Height,
                HeightSpecified = true,
                Width = mapView.Width,
                WidthSpecified = true
            };

            coll = new Collision();

            ve = new VEarth(mapStyle, mapSize);

            status.Text = "0";

            gps.LocationChanged += new LocationChangedEventHandler(gps_LocationChanged);

        }

        private delegate void UpdateMap();
        protected void gps_LocationChanged(object sender, LocationChangedEventArgs args)
        {
            pos = args.Position;
            if (pos.LatitudeValid && pos.LongitudeValid)
            {
                coll.setPlayerPos(pos.Latitude, pos.Longitude);

                mapView.Invoke((UpdateMap)delegate()
                {
                    status.Text = pos.Latitude + ", " + pos.Longitude;
                    mapView.Image = ve.getMap(pos.Latitude, pos.Longitude, 19, mapStyle);
                    if (mapView.Image != null) hasImage = true;
                });

                if (hasImage == true)
                {
                    if (coll.isCollided() == true)
                    {
                        gps.Close();
                        status.Invoke((UpdateMap)delegate()
                        {
                            status.Text = "HURF DURF";
                        });
                        mapView.Invoke((UpdateMap)delegate()
                        {
                            mapView.Image = null;
                        });
                    }
                }
            }
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            if (!gps.Opened)
            {
                gps.Open();
                menuItem2.Text = "Stop";
            }
            else
            {
                gps.Close();
                menuItem2.Text = "Start";
            }
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            Application.Exit();
            if (gps.Opened) gps.Close();
            if (hasImage == true) ve.Dispose();
        }
    }
}

Program.cs

using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;

namespace GlobalFighter
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [MTAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }
    }
}

Player.cs

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace GlobalFighter
{
    class Player : Item
    {
        int att;

        public Player()
        {
        }

        public Player(double x, double y, int hp)
        {
            Type = "Player";
            XPos = x;
            YPos = y;
            Health = hp;
        }

        public int Attack
        {
            get { return att; }
            set { att = value; }
        }
    }
}

Item.cs

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace GlobalFighter
{
    class Item
    {
        double xPos;
        double yPos;
        String type;
        int health;

      /*  public Item(String typ, int hp)
        {
            type = typ;
            health = hp;
        }*/

        public Item()
        {
        }

        public double XPos
        {
            get { return xPos; }
            set { xPos = Math.Round(value,6); }
        }

        public double YPos
        {
            get { return yPos; }
            set { yPos = Math.Round(value, 6); }
        }

        public String Type
        {
            get { return type; }
            set { type = value; }
        }

        public int Health
        {
            get { return health; }
            set { health = value; }
        }
    }
}

Enemy.cs

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace GlobalFighter
{
    class Enemy : Item
    {
        int def;

        public Enemy()
        {
        }

        public Enemy(double x, double y, int hp)
        {
            Type = "Enemy";
            XPos = x;
            YPos = y;
            Health = hp;
        }

        public int Defense
        {
            get { return def; }
            set { def = value; }
        }
    }
}

Edited 6 Years Ago by Peacer: n/a

The problem is two VEarth instances.
The Collision class has its own instance of VEarth.

class Collision
    {
        VEarth ve; //<-- VEarth used by Collision
        Player p1;

The one initialised in Form1 is not the same instance.

public partial class Form1 : Form
    {
        Gps gps = new Gps();
        GpsPosition pos;
        Collision coll;

        VEarth ve; //<-- VEarth initilised by Form1
        VEarthImagery.MapStyle mapStyle = VEarthImagery.MapStyle.AerialWithLabels;

The problem is two VEarth instances.
The Collision class has its own instance of VEarth.

I can't test this code or anything, but it seems to be re-running code outside of the constructor and resetting the arrays to 0 aren't they not? Should that code only be run once and not every time a new instance is declared?

No. The array used by the Collision is never initialised.
The array is initialised in the VEarth.getMap method.
This is called by the form in gps_LocationChanged

mapView.Image = ve.getMap(pos.Latitude, pos.Longitude, 19, mapStyle);

However, in the call to coll.isCollided() the VEarth used is the instance for the Collision object.

public bool isCollided()
{
    ve.setPositions();            //<--- This ve is Collision.ve NOT FORM1.ve
    addEnemy();
    Boolean coll = false;
    for(int i=0;i<enemie.Length;i++)
    {
        if (Math.Round(p1.XPos, 6) == Math.Round(enemie[i].XPos, 6) && Math.Round(p1.YPos, 6) == Math.Round(enemie[i].YPos, 6))
        {
            coll = true;
        }
    }
    
    return coll;
}
This question has already been answered. Start a new discussion instead.