簡體   English   中英

public Customer(int id)給出錯誤:方法必須具有返回類型C#

[英]public Customer(int id) Gives error: Method must have return type C#

在我的代碼底部,“ CustomerEmail”突出顯示,並說“方法”必須具有返回類型。 然后,“高亮顯示return,說:“因為CustomerEmail(int)返回void,所以return關鍵字后不能包含對象表達式”我不知道CustomerEmail如何返回void?

公共類CustomerCollection {列出customerList = new List();

    public List<Customer> CustomerList
    {
        get { return customerList; }
        set { customerList = value; }
    }

    public void RegisterCustomer(int id, string first, string last)
    {
        Customer c = new Customer(id, first, last);
        customerList.Add(c);
    }
    public void RegisterCustomer(int id, string first, string last,string phone,string email)
    {
        Customer c = new Customer(id, first, last,phone,email);
        customerList.Add(c);
    }

    public void RemoveCustomer(int id)
    {
        //works if there is a single-parameter constructor and Equals method in Faculty class
        Customer rem = new Customer(id);
        customerList.Remove(rem);
    }

  public CustomerEmail(int id)
  {
      Customer findEmail = new Customer(id);
      for (int i=0; i < customerList.Count;i++)
          if (customerList[i].Equals(findEmail))
              return customerList[i].CustomerEmail;
      return null;
  }



    public FindCustomer(int id)
    {
        Customer find = new Customer(id);
        for (int i = 0; i < customerList.Count; i++)
            if (customerList[i].Equals(find))
                return customerList[i];
       return null;

    }

}

您必須聲明一個返回類型(對於不返回值的方法,則聲明為void )。 看起來在這種情況下string是合適的。 給使用Get返回值的方法加前綴是一種常見的約定:

  public string GetCustomerEmail(int id)
  {
      Customer findEmail = new Customer(id);
      for (int i=0; i < customerList.Count;i++)
          if (customerList[i].Equals(findEmail))
              return customerList[i].CustomerEmail;
      return null;
  }

對於FindCustomer Customer似乎是適當的返回類型:

public Customer FindCustomer(int id)
{
    Customer find = new Customer(id);
    for (int i = 0; i < customerList.Count; i++)
        if (customerList[i].Equals(find))
            return customerList[i];
   return null;

}

您沒有方法的返回類型。 因此,您的方法簽名不正確。

嘗試將其更改為

public string CustomerEmail(int id)

public Customer FindCustomer(int id)

但是,如果您不希望退貨,則需要使用void return類型。

看看有關如何創建方法的MSDN文檔

方法(C#編程指南)

方法可以將值返回給調用方。 如果返回類型(在方法名稱之前列出的類型)不為空,則該方法可以使用return關鍵字返回值。 帶有return關鍵字后跟匹配返回類型的值的語句將把該值返回給方法調用者。 return關鍵字還會停止方法的執行。 如果返回類型為void,則不帶值的return語句對於停止執行該方法仍然有用。 如果沒有return關鍵字,則該方法將在到達代碼塊末尾時停止執行。 必須使用非無效返回類型的方法才能使用return關鍵字返回值

將方法更改為

  public <return type> CustomerEmail(int id)
  {
      Customer findEmail = new Customer(id);
      for (int i=0; i < customerList.Count;i++)
          if (customerList[i].Equals(findEmail))
              return customerList[i].CustomerEmail;
      return null;
  }

這里的return typeCustomerEmail類型。 FindCustomer方法也采用相同的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM