no9_ch

Whenever i run the program, i keep having format exception at double lat1 = Convert.ToDouble(latitude1); is there anyway i can solve it I even try to add another condition if(latitude1 != "" && longitude1 !="") and if(latitude1!=null && longitude1!=null). Both of these 2 condition can't help me to solve the format exception, however this condition if(latitude1 != "" && longitude1 !="") give me an infinity answer.

this is the coding:

string latitude1, latitude2;
string longitude1, longitude2;

latitude1 = label7.Text;
longitude1 = label8.Text;

label7.Text = gps.RMC.Latitude.ToString();
label8.Text = gps.RMC.Longitude.ToString();

templat = gps.RMC.Latitude.ToString();
templong = gps.RMC.Longitude.ToString();

latitude2 = label7.Text;
longitude2 = label8.Text;

if ((latitude1 != latitude2) || (longitude1 != longitude2))
{
double lat1 = Convert.ToDouble(latitude1);
double long1 = Convert.ToDouble(longitude1);
double lat2 = Convert.ToDouble(latitude2);
double long2 = Convert.ToDouble(longitude2);

double lat1r = lat1 * (Math.PI / 180);
double long1r = long1 * (Math.PI / 180);
double lat2r = lat2 * (Math.PI / 180);
double long2r = long2 * (Math.PI / 180);

double latitude = lat2r - lat1r;
double longitude = long2r - long1r;

double a = Math.Pow(Math.Sin(latitude / 2.0), 2.0) + Math.Cos(lat1r) * Math.Cos(lat2r) * Math.Pow(Math.Sin(longitude / 2.0), 2.0);

double c = 2.0 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1.0 - a));

const Double earthRadius = 6376.5;

distance = earthRadius * c;
}

distanceCovered = distanceCovered + distance;
lblDC.Text += distanceCovered.ToString();


any idea of how i can solve this problem


Re: .NET Base Class Library How to solve this format exception

Jeremy Filburn

Code Snippet
latitude1 = label7.Text; // What is the text Should this be below (label7.Text = gps.RMC.Latitude.ToString();)
longitude1 = label8.Text;

label7.Text = gps.RMC.Latitude.ToString(); // here is your latitude
label8.Text = gps.RMC.Longitude.ToString();


I would also try using double.TryParse();




Re: .NET Base Class Library How to solve this format exception

no9_ch

latitude1 = label7.Text; // when the program start. latitude1 meant to be of no value. same for longitude1. This (label7.Text = gps.RMC.Latitude.ToString()Wink should not be below latitude1 = label7.Text;
longitude1 = label8.Text;

both label7.Text and label8.Text will automatically have a new latitude and longitude whenever the gps receive a new coordinate.





Re: .NET Base Class Library How to solve this format exception

ARK88

Could you provide a sampling of the data that could be in "latitude1". Even better would be a sample of what it is when the exception is thrown.





Re: .NET Base Class Library How to solve this format exception

no9_ch

when the exception occur, latitude1 and longitude1 is "" and "" respectively.





Re: .NET Base Class Library How to solve this format exception

ARK88

The thing I'd like to see is the code you posted previously, but, I'd like to see it with the [attempted] conditions you added. From your description of what you added, and the problem you described, it seems like your conditions should have worked... which leads me to think that maybe you just had a logical coding error.

Other than that, did you try Jeremy Filburn's suggestion of utilizing the "double.TryParse()" method I would think that should resolve this problem you are describing as well.





Re: .NET Base Class Library How to solve this format exception

no9_ch

this is the code i previously attempted to solve the format exception. however it does not work as after the calculation of the distance, the answer given is infinity. so i'm seeking help over here to see anyone could help me solve it. i didn't try using double.TryParse() method because it was not being taught to me and i'm not sure how to use it as well, on the other hand, double lat2 = Convert.ToDouble(latitude2); double long2 = Convert.ToDouble(longitude2); this 2 conversion does work but not for double lat1 = Convert.ToDouble(latitude1); and double long1 = Convert.ToDouble(longitude1); .

string latitude1, latitude2;
string longitude1, longitude2;

latitude1 = label7.Text;
longitude1 = label8.Text;

label7.Text = gps.RMC.Latitude.ToString();
label8.Text = gps.RMC.Longitude.ToString();

templat = gps.RMC.Latitude.ToString();
templong = gps.RMC.Longitude.ToString();

latitude2 = label7.Text;
longitude2 = label8.Text;

if ((latitude1 != latitude2) || (longitude1 != longitude2))
{

if(latitude1!="" && longitude1!="")

{
double lat1 = Convert.ToDouble(latitude1);
double long1 = Convert.ToDouble(longitude1);
double lat2 = Convert.ToDouble(latitude2);
double long2 = Convert.ToDouble(longitude2);

double lat1r = lat1 * (Math.PI / 180);
double long1r = long1 * (Math.PI / 180);
double lat2r = lat2 * (Math.PI / 180);
double long2r = long2 * (Math.PI / 180);

double latitude = lat2r - lat1r;
double longitude = long2r - long1r;

double a = Math.Pow(Math.Sin(latitude / 2.0), 2.0) + Math.Cos(lat1r) * Math.Cos(lat2r) * Math.Pow(Math.Sin(longitude / 2.0), 2.0);

double c = 2.0 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1.0 - a));

const Double earthRadius = 6376.5;

distance = earthRadius * c;
}
}

distanceCovered = distanceCovered + distance;
lblDC.Text += distanceCovered.ToString();





Re: .NET Base Class Library How to solve this format exception

nobugz

If you can't figure out how to use the debugger, you'll need to talk to your teacher. That's what she is there for.





Re: .NET Base Class Library How to solve this format exception

no9_ch

if my teacher would be so kind to help i won't be finding forum and seek help of how i could solve this problem. now that i have tried using the double.tryparse. it still don't work for my case. i still get the infinity answer.





Re: .NET Base Class Library How to solve this format exception

nobugz

Establishing a working relationship with your teacher is probably the most important lesson you could learn from your class. Real life, after you graduated, isn't going to be any different. You'll quickly run out of resources when you don't respect your boss and think that you can find a way around an interaction problem. Posting to an anonymous person on the web and ask him/her to solve your problem doesn't work. It didn't work now, it won't work in the future. Hope it helped, I kinda doubt it.





Re: .NET Base Class Library How to solve this format exception

Jeremy Filburn

Another thing you should try is using things that you are not taught. Just because your teacher did not show you how does not mean you can't!

A good student will teach the teacher!

Like I said before, we need the text like this "This is my text", instead of someControl.Text.

How can you expect us to help without knowing the "Text "