小编CSh*_*zie的帖子

无法将类型'customtype'隐式转换为'othercustomtype'

我是C#的新手.我有一个Persons类和一个继承自Persons类的User类.我的控制台我在一个数组中输入用户.然后,我只需输入用户ID即可向用户数组中的用户添加注释.在我的Persons类中,我有这个函数,必须搜索此用户是否在users数组中.

public static Persons FindPerson(Persons[] persons, int noteid)
{
    foreach (Persons person in persons)
        if (person.ID == noteid) return person;
    return null;
}
Run Code Online (Sandbox Code Playgroud)

在我的User类中,我有一个函数循环id的整个输入,直到它获得users数组中的id.

public static User SelectUser(User[] users)
{
    while (true)
    {
        Console.Write("Please enter the User id: ");
        string input = Console.ReadLine();
        int id;
        if (int.TryParse(input, out id))
        {
            Persons person = Persons.FindPerson(users, id);
            if (person != null) return person; // fail on "return person"                   
        }
        Console.WriteLine("The User does not exist. Please try again.");                
    }
}
Run Code Online (Sandbox Code Playgroud)

一切正常,但我现在在if语句中的"return person"上收到此错误消息.

无法将类型'UserCustomerNotes.Persons'隐式转换为'UserCustomerNotes.User'.存在显式转换(您是否错过了演员?) …

c# type-conversion

9
推荐指数
1
解决办法
5万
查看次数

如果字符串项不在字符串数组中

我有一个字符串数组.我需要根据所选项目是否在数组中显示按钮.我需要知道如何告诉程序"(array.NOT Contains("string"))".请任何人帮帮我?提前致谢

我的代码:

    List<string> activationids = new List<string>();
    foreach (ModuleActivation moduleactivation in activationid)
         activationids.Add(moduleactivation.ActivationID);

    string gvselectActID = GridView1.SelectedRow.Cells[1].Text;

    if (activationids.Contains(gvselectActID))
    {
      activateInsert.Visible = true;
      activateUpdate.Visible = false;
      deactivate.Visible = true;
    }
    else if (activationids."NOT" Contains(gvselectActID))
    {
      activateInsert.Visible = false;
      activateUpdate.Visible = true;
      deactivate.Visible = false;
    }
    else
    {
     activateInsert.Visible = false;
     activateUpdate.Visible = false;
     deactivate.Visible = false;
    }
  } 
Run Code Online (Sandbox Code Playgroud)

c#

5
推荐指数
3
解决办法
1万
查看次数

将类型字符串转换为类型对象

我是C#的新手.我需要在控制台应用程序中显示用户fullname,但我一直收到此错误

无法将类型'string'隐式转换为'UserCustomerNotes.User'

我的代码只在我使用时工作,return user但它在应用程序中显示的所有内容都是"UserCustomerNotes.User",我需要显示用户的全名.谁能请帮忙.这是我的代码:

public static User FindUser(User[] users, int noteid)
{
    foreach (User user in users)                
        if (user.ID == noteid) return user.FullName;
    return null;
}
Run Code Online (Sandbox Code Playgroud)

c#

3
推荐指数
1
解决办法
1712
查看次数

非静态字段,方法或属性需要对象引用

这是我第一次与Lists合作,而我似乎并没有做到这一点.我有一个Customer类,其客户列表作为Customer类中的属性(可以这样做吗?)

public class Customer
{
    private List<Customer> customers = new List<Customer>();

    public List<Customer> Customers
    {
        get { return customers; }
        set { customers = value; }
    }
Run Code Online (Sandbox Code Playgroud)

在我的程序中,我添加到此客户列表中,如下所示:

Customer C = new Customer();
Customer.InputCustomer(C);
C.Customers.Add(C);
Run Code Online (Sandbox Code Playgroud)

现在我需要在此列表中显示客户.我已将AllCustomers函数添加到Customer Class,如下所示:

public static void AllCustomers()
    {
        foreach (Customer customer in Customers) //Fail on "Customers"
        {
            Console.WriteLine("Customer ID: " + customer.ID);
            Console.WriteLine("Customer Name: " + customer.FullName);
            Console.WriteLine("Customer Address: " + customer.Address);
            Console.WriteLine();
        }
    }
Run Code Online (Sandbox Code Playgroud)

但我在foreach声明中得到了这个错误:

非静态字段,方法或属性'AddCustomerList.Customer.Customers.get'需要对象引用

就像我说的,这是我第一次使用List,maby我不明白吧?谁能帮帮我吗?

c#

3
推荐指数
1
解决办法
3万
查看次数

循环切换语句

我是C#的新手.我在控制台应用程序中有一个"菜单".现在,当我完成从菜单中选择一个项目并执行该菜单项所需的操作时,我想循环并再次显示菜单,以便用户可以选择不同的菜单项.我在菜单上有一个退出,我只想用它退出.我尝试了一个while循环,但这不起作用.选择菜单项并运行所选项目代码后,它会关闭应用程序.我究竟做错了什么?

static void Main()
    {   
        int input = 0;
        while (true)
        {

            Console.WriteLine("MENU");
            Console.WriteLine("Please enter the number that you want to do:");
            Console.WriteLine("1. Do thing A");
            Console.WriteLine("2. Do thing B");
            Console.WriteLine("3. Do thing C");
            Console.WriteLine("4. Do thing D");
            Console.WriteLine("5. Do thing E");
            Console.WriteLine("6. Do thing F");
            Console.WriteLine("7. Exit");

            int menuchoice = int.Parse(Console.ReadLine());

            switch (menuchoice)
            {
                case 1:
                    Console.WriteLine("Thing A has been done"); 
                    break;
                case 2:
                    Console.WriteLine("Thing B has been done");
                    break;
                case 3:
                    Console.WriteLine("Thing C has been done");
                    break;
                case …
Run Code Online (Sandbox Code Playgroud)

c#

1
推荐指数
1
解决办法
2万
查看次数

标签 统计

c# ×5

type-conversion ×1