Quick C# help needed. set, get, override

Associate
Joined
29 Dec 2007
Posts
1,414
Location
London
Hey guys. im currently making a game for a windows mobile

The problem im having is probably very very simple just I just cant seem to work it out

Its a treasurehunt game, i need to be able to read gps info from the device. The two files im having problems with are:

form3.cs
gpsMonitor.cs

from form3.cs i need to get the latitude and longitude values from gpsMonitor.cs

gpsMonitor.cs is split into two classes: gpsMonitor() and gpsCoordinate(lat, lng)

the class gpsMonitor opens a serial port, validates sentences, along with aload of other stuff (unimportant atm). when it has a valid sentence it will extract the lat and long decimal values and call gpsCoordinate() with the two values as parameters

gpsCoordinate class looks like this:


public class GpsCoordinate
{
private decimal latitude;
private decimal longitude;
private const string strFormat = "Latitude: {0} degrees /t Longitude: {1} degrees";

public GpsCoordinate(decimal lat, decimal lng)
{
Latitude = lat;
Longitude = lng;
}

public decimal Latitude
{
get { return latitude; }
set { latitude = value; }
}

public decimal Longitude
{
get { return longitude; }
set { longitude = value; }
}

public override string ToString ()
{
return String.Format(strFormat, latitude, longitude);
}
}



what i do from form3.cs is make a new instance of gpsMonitor (threaded by the way) then what i need to do is collect lat and long from the gpsCoordinate class. But since its in this set,get,override stuff i have no idea what to do :(

i can get info from the gpsMonitor class but since it is receiving invalid sentences 95% of the time its abit useless :(

Anyone albe to shed some light? thanks :)
 
OK, assuming you have your coordinate defined as follows:

Code:
var coord = new GpsCoordinate(your latitude, your longitude);

You can get the latitude/longitude by simply using:
Code:
coord.Latitude;

and 

coord.Longitude;

If that's all you meant?
If that's not the issue please post some more detail.
 
It's not particularly clear what you're asking... do you want to try again?

Are you trying to do something like this??
Code:
GpsCoordinate foo = new GpsCoordinate(100,50);
GpsMonitor bar = new GpsMonitor();
bar.Latitude = foo.Latitude;
bar.Longitude = foo.Longitude;
 
The thing is i cant create an instance of GpsCoordinate because I dont have the lat/lng decimals.

I create an instance of gpsMonitor()

GpsMonitor() then reads from the serial port, validates sentences and then extracts the lat/lng decimals. It then calls GpsCoordinate like so:

location = new GpsCoordinate( lat / 100 , lng / 100);

So its put lat/lng decimals into GpsCoordinate, which then runs the code in the first post.

n.b. I cant grab the data straight from GpsMonitor because its set to private.

So in form3 ive created a new instance of GpsMonitor()

GpsMonitor myGpsMonitor = new GpsMonitor();

What i need to work out is how to then get the data from GpsCoodinate, without actually calling it directly myself.

Hopefully that makes more sense? im pretty rubbish at programming to be honest :(

so thanks guys :)
 
Create a public method in GpsMonitor called getLocation() or something that returns a pointer to the GpsCoordinate object - you then have access to the lat/lng

or am I over simplifying it??
 
The thing is i cant create an instance of GpsCoordinate because I dont have the lat/lng decimals.

I create an instance of gpsMonitor()

GpsMonitor() then reads from the serial port, validates sentences and then extracts the lat/lng decimals. It then calls GpsCoordinate like so:

location = new GpsCoordinate( lat / 100 , lng / 100);

So its put lat/lng decimals into GpsCoordinate, which then runs the code in the first post.

n.b. I cant grab the data straight from GpsMonitor because its set to private.

So in form3 ive created a new instance of GpsMonitor()

GpsMonitor myGpsMonitor = new GpsMonitor();

What i need to work out is how to then get the data from GpsCoodinate, without actually calling it directly myself.

Hopefully that makes more sense? im pretty rubbish at programming to be honest :(

so thanks guys :)

You say that you can't create an instance of GpsCoordinate, yet you appear to be doing exactly this.

location = new GpsCoordinate( lat / 100 , lng / 100);
instaniates a GpsCoordinate object with the specified latitude and longitude.

You can then use location.Latitude and location.Longitude to access the values.
 
You say that you can't create an instance of GpsCoordinate, yet you appear to be doing exactly this.

location = new GpsCoordinate( lat / 100 , lng / 100);
instaniates a GpsCoordinate object with the specified latitude and longitude.

You can then use location.Latitude and location.Longitude to access the values.

I think the issue is he doesnt have access to the location object from his class - only to the GpsMonitor (which in turn has access to the GpsCoordinate) so I think what he actually needs is a method in his GpsMonitor class that returns the GpsCoordinate object (location)... but I don't know if I have read the question correctly.
 
Back
Top Bottom