"并非所有代码路径都返回值"C#

Jas*_*L95 0 c# windows xaml

您好我目前正在Windows Phone/C#上编写petshop应用程序的婴儿阶段.我在下面的课程中设置了我的通用列表,并且能够使用给定的字段(姓名,年龄,品种和类型)将宠物添加到商店.我相信的问题在于我正在制作一个显示方法(在下面称为Mainpage.xaml.cs的另一个页面中,名为Mainpage.xaml.cs),名为DisplayShop(),我正在阅读3个字符串和一个int,我相信它与我的DisplayShop()方法是一个字符串这一事实有关,虽然我试图通过将age字段转换为Display()方法中的字符串来解决问题,尽管这并没有解决问题.

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink; 
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Assignment_1
{
    public class Shop
    {
        private string name;
        private int age;
        private string breed;
        private string type;

        public Shop(string Name, int Age, string breed, string Type)
        {
            this.Name = name;
            this.Age = age;
            this.breed = breed;
            this.Type = type;
        }

        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                this.name = value;
            }
        }

        public int Age
        {
            get
            {
                return this.age;
            }
            set
            {
                this.age = value;
            }
        }

        public string Breed
        {
            get
            {
                return this.breed;
            }
            set
            {
                this.breed = value;
            }
        }

        public string Type
        {
            get
            {
                return this.type;
            }
            set
            {
                this.type = value;
            }
        }

        //Create the Collection
        List<Shop> myShop = new List<Shop>();

        public String Display()
        {
            return (Name + "\n" + Age.ToString() + "\n" + Breed + "\n" + Type);
        }

        public String DisplayShop()
        {
            foreach (Shop tmp in myShop)
            {
                tmp.Display();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是Mainpage.xaml.cs页面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Assignment_1
{
    public partial class MainPage : PhoneApplicationPage
    {
        List<Shop> myShop = new List<Shop>();

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            setUpShop();
        }

        //Add animals to the Shop
        private void setUpShop()
        {
            myShop.Add(new Shop("John", 3 ,"Labrador", "Dog"));
            myShop.Add(new Shop("Billy", 9, "Terrier", "Dog"));
            myShop.Add(new Shop("Sam", 2, "Persian", "Cat"));
            myShop.Add(new Shop("Molly", 3, "Siamese", "Cat"));
            myShop.Add(new Shop("Nemo", 1, "Clown Fish", "Fish"));
            myShop.Add(new Shop("Dory", 1, "Palette Surgeonfish", "Fish"));
            myShop.Add(new Shop("Keith", 1, "Bearded Dragon", "Lizard"));
            myShop.Add(new Shop("Ozzy", 1, "Gecko", "Lizard"));

            imgHolder1.Items.Add(myShop[1].DisplayShop());
        } 
    }
}
Run Code Online (Sandbox Code Playgroud)

第一部分的目的是将宠物添加到我的Windows应用程序中的listBox,如果你们中的任何人可以建议修复这个问题,我将非常感激,我已经包含了手机应用程序当前看起来的方式的图像>>> http://gyazo.com/06217efae9265d0fef59cd2a44be7923和错误代码>>> http://gyazo.com/40c47628f1dfb7d735af4c72dde3a651 在此先感谢Jason

And*_*rei 5

看起来你忘了从DisplayShopfuncion 返回值:

public String DisplayShop()
{
    List<string> shops = new List<string>();
    foreach (Shop tmp in myShop)
    {
        shops.Add(tmp.Display());
    }
    return string.Join(",", shops);
}
Run Code Online (Sandbox Code Playgroud)

Join 这里用来表示一般的想法,你可以使用你想要的任何东西来构造整个字符串.