为什么我无法覆盖到string()方法,即我无法访问客户名称和地址 [英] Why I am unable to override to string() method i.e I am unable to access customer name and address

查看:76
本文介绍了为什么我无法覆盖到string()方法,即我无法访问客户名称和地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我无法覆盖To string()方法,即我无法访问客户姓名和地址



我尝试了什么:



 命名空间 MVCPractise1.Controllers 
{
public class 客户
{
公共 字符串客户名称{获取; set ; }
public string 地址{ get ; set ; }
}

public class TestController:Controller
{

public 客户GetCustomer()
{
客户c = new Customer();
c.CustomerName = 客户1;
c.Address = Address1;
return c;
}
public 覆盖 string ToString()
{
return 客户+ | + this .Address;
}
}
}

解决方案

因为名称和地址不是TestController的一部分class - 他们是Customer类的一部分。



将ToString移动到客户(并将Customer更改为CustomerName)并且它将起作用。

why i am unable to override To string() method i.e i am unable to access Customer Name and address

What I have tried:

namespace MVCPractise1.Controllers
{
    public class Customer
    {
        public string CustomerName { get; set; }
        public string Address { get; set; }
    }

    public class TestController : Controller
    {

        public Customer GetCustomer()
        {
            Customer c = new Customer();
            c.CustomerName = "Customer 1";
            c.Address = "Address1";
            return c;
        }
        public override string ToString()
        {
            return Customer + "|" + this.Address;
        }
    }
}

解决方案

Because the name and address are not part of the TestController class - they are part of the Customer class.

Move your ToString to the Customer (and correct "Customer" to "CustomerName") and it'll work.


这篇关于为什么我无法覆盖到string()方法,即我无法访问客户名称和地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆