Husein Behboudi Rad
Husein Behboudi Rad

Reputation: 5462

CLLocationCoordinate2D null value

In my monodroid application many types I need to assign CLLocation object to null value. I need it in order to detect some specific conditions.For example suppose a function that do something to CLLocationCoordinate2D objects and we want that if and exeption accourd to this function it returns null.But monotouch do not allow null value for this type.

This is the test Method that I used for this propose:

        private static CLLocationCoordinate2D sampleMethod ()
    {
        try {
            return new CLLocationCoordinate2D ();
        } catch (Exception ex) {
            return null;
        }
    }

and the error accord:

Error CS0037: Cannot convert null to `MonoTouch.CoreLocation.CLLocationCoordinate2D' because it is a value type (CS0037) (Realtyna_iPhone_Project)

In this states how we should do?

Upvotes: 2

Views: 1270

Answers (1)

Jason
Jason

Reputation: 89179

CLLocationCooredinate2D is a value type, which cannot be null. However, C# allows you to wrap value types in a Nullable wrapper, so that they can contain null values.

For example, this should be valid:

private CLLocationCoordinate2D? ProcessLocation ()
{
    CLLocationCoordinate2D? retLoc;

    try {
        // do something with retLoc here
    } catch (Exception ex) {
        retLoc = null;
    }

    return retLoc;
}

Upvotes: 2

Related Questions