tugoprogrammer tugoprogrammer - 3 months ago 10
ASP.NET (C#) Question

Doesn't contain a definition for '' and no extension method accepting a first argument of type, could be found

public class Customer
{
public string CustomerName { get; set; }
public string Address { get; set; }
}

public class TestController : Controller
{
public string GetString()
{
return "wassup bro";
}

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


Here are my codes and i have some problems like below.
What's the solution? Thanks already a lot.

Error 1:


'day7.Controllers.TestController' does not contain a definition for
'CustomerName' and no extension method 'CustomerName' accepting a
first argument of type 'day7.Controllers.TestController' could be
found (are you missing a using directive or an assembly
reference?) c:\users\sena\documents\visual studio
2013\Projects\day7\day7\Controllers\TestController.cs 36 25 day7


Error 2:


'day7.Controllers.TestController' does not contain a definition for
'Address' and no extension method 'Address' accepting a first argument
of type 'day7.Controllers.TestController' could be found (are you
missing a using directive or an assembly
reference?) c:\users\sena\documents\visual studio
2013\Projects\day7\day7\Controllers\TestController.cs 36 51 day7

Answer

If you want to overwrite the ToString() from you Model class you have to do it in the Class itself:

public class Customer
{
   public string CustomerName { get; set; }
   public string Address { get; set; }

   public override string ToString()
   {
        return CustomerName + "|" + Address;
   }
}

The this keyword refers to the current instance of your class where you're using it. So you're overwriting ToString() in the Controller class. But this class does not have the properties CustomerName and Address.