Reputation: 149
I am using a service reference which connects to internet and I want to show message in a message box if ever the connection fails. How will I call message box in the member function of the class which has void return type? This is the member function of the class:
public void ReverseGeocodePoint()
{
try{
string results = "";
string key = "abc";
ReverseGeocodeRequest reverseGeocodeRequest = new ReverseGeocodeRequest();
// Set the credentials using a valid Bing Maps key
reverseGeocodeRequest.Credentials = new GeocodeService.Credentials();
reverseGeocodeRequest.Credentials.ApplicationId = key;
// Set the point to use to find a matching address
GeocodeService.Location point = new GeocodeService.Location();
point.Latitude = latitude;
point.Longitude = longitude;
reverseGeocodeRequest.Location = point;
// Make the reverse geocode request
GeocodeServiceClient geocodeService = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
//This will connect to the server
GeocodeResponse geocodeResponse = geocodeService.ReverseGeocode(reverseGeocodeRequest);
if (geocodeResponse.Results.Length > 0)
results = geocodeResponse.Results[0].DisplayName;
else
results = "No Results found";
address = results;
}} catch{ //here I want to show a msgbox but the problem is, this is not the form class}
Upvotes: 1
Views: 341
Reputation: 66499
Don't catch the exception in that class if there's nothing you can do about it there. Just let it bubble up and catch it where you can do something about it:
public class MyForm : Form
{
public void SomeMethod()
{
try
{
var sc = new ServiceClass();
sc.ReverseGeocodePoint();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
In ReverseGeocodePoint()
, remove the try/catch
statements.
Upvotes: 2