0

I want to return an array to my WP 8 page and have an error: not all code paths return a value. How can I correct it? My WCF service has the following code:

public Point getAllusrs (int myID)
    {
        DataClasses1DataContext data = new DataClasses1DataContext();
        var a = (from s in data.Tabs where s.u1ID == myID && s.status == true select s.u2ID).ToArray();
        int inc = 0;

        foreach (var d in data.Users)
        {
            if (a[inc] != null && d.Id == a[inc])
            {
                inc++;
                return new Point() { Lat = d.usrLat, Lon = d.usrLong };
            }
            else inc++;
        }       
    } 
2
  • Do you actually want to return an array of Point or a single Point? Commented Jul 25, 2014 at 17:10
  • @Dave, array of Point to create multiple pushpins on the map. Commented Jul 25, 2014 at 17:18

2 Answers 2

2

You must handle the case, when there is no user with target Id.

For example, you can return default value in the end of the method:

public Point getAllusrs (int myID)
{
    DataClasses1DataContext data = new DataClasses1DataContext();
    var a = (from s in data.Tabs where s.u1ID == myID && s.status == true select s.u2ID).ToArray();
    int inc = 0;

    foreach (var d in data.Users)
    {
        if (a[inc] != null && d.Id == a[inc])
        {
            inc++;
            return new Point() { Lat = d.usrLat, Lon = d.usrLong };
        }
        else inc++;
    }       
    return default(Point); // or some Point with default Lat and Lon
} 

or throw some exception:

public Point getAllusrs (int myID)
{
    DataClasses1DataContext data = new DataClasses1DataContext();
    var a = (from s in data.Tabs where s.u1ID == myID && s.status == true select s.u2ID).ToArray();
    int inc = 0;

    foreach (var d in data.Users)
    {
        if (a[inc] != null && d.Id == a[inc])
        {
            inc++;
            return new Point() { Lat = d.usrLat, Lon = d.usrLong };
        }
        else inc++;
    }       
    throw new Exception("Point not found"); // or some custom Exception
} 
Sign up to request clarification or add additional context in comments.

Comments

0

It's because if the code doesn't satisfy the condition it doesn't return any value.

public Point getAllusrs (int myID)
{
    Point pnt = null; // or new Point ();
    DataClasses1DataContext data = new DataClasses1DataContext();
    var a = (from s in data.Tabs where s.u1ID == myID && s.status == true select s.u2ID).ToArray();
    int inc = 0;

    foreach (var d in data.Users)
    {
        if (a[inc] != null && d.Id == a[inc])
        {
            inc++;
            pnt = new Point() { Lat = d.usrLat, Lon = d.usrLong };
        }
        else inc++;
    }  

    return pnt;   
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.