I have a ProductService
class that defines the following methods. It doesn't work, as I will explain further below, but it gives the compile error CS1061: 'ProductService' does not contain a definition for 'GetByid'. Unless I've dropped the ball on method overloading, why is the public overload of GetById not seen by other code?
public IEnumerable<Product> ListActiveByCatId(Guid catId) {
return _entityContext.Products
.Include("Category")
.Where(p => p.Category.id == catId);
}
public Product GetById(string productId) {
return new Product();
}
private Product GetByid(Guid productId) {
return _entityContext.Products.First(p => p.id == productId);
}
I have the following code using a ProductService
, and the two compile errors are both on this call:
protected void Page_Load(object sender, EventArgs e开发者_运维问答)
{
if (!IsPostBack) {
var productId = Request["productId"];
if (productId == null) {
// TODO How to get the actual parameter, not default parameter.
productDetailDataSource.SelectParameters["productId"].DefaultValue = DefaultProductId;
}
*itemTitleLabel.Text = _productService.GetByid(productId).Name;*
}
}
The exact compiler error is:
C:\SCS\Products\Details.aspx.cs(21,51): error CS1061: 'ProductService' does not contain a definition for 'GetByid' and no extension method 'GetByid' accepting a first argument of type 'ProductService' could be found (are you missing a using directive or an assembly reference?)
Line 21 is the one I attempted to emphasise, i.e. the one with asterisks around it in the above calling code.
GetByid
!= GetById
You've got a casing issue.
First, your GetByid
method is set as private
, so it can't be accessed from outside your class.
Second, the GetByid method is actually not an overload of your GetById method, since C# is case sensitive, and both your methods name are not the same, casing considered.
Third, does Request["productId"]
not return an object? If that is so, a direct type cast might not be possible, thus causing the compiler unable to find an overload of your method that takes an object as an input parameter.
EDIT #1
C:\SCS\Products\Details.aspx.cs(21,51): error CS1061: 'ProductService' does not contain a definition for 'GetByid' and no extension method 'GetByid' accepting a first argument of type 'ProductService' could be found (are you missing a using directive or an assembly reference?)
The object returned by Request["productId"]
seem to be an instance of your ProductService
class, which value is provided to your GetByid
method. I can't see any GetById
method overload taking an instance of ProductService
as an input parameter.
Perhaps the use of polymorphism might help you understand what's going on here. The var
keyword is a placeholder for your resolved type, thus making it worth your Request["productId"]
object type. Hence, if this object returned by this call is a ProductService
, then var
will be replaced by ProductService
. Inputing this value to your call to GetByid
makes this that an overload of GetByid
method should accept a ProductService
as your parameter, but there is no such overload. The default of not finding an acceptable overload, the compiler tells you that it can't resolve your method call, since it cannot find any which suits the call.
Make sure that Request["productId"]
returns a value of an expected type, which seems to be an int.
I would recommend that you use the types instead of var
whenever possible. This would have pointed you to the right source of the problem, by saying that, for example, the type object
cannot directly be converted to int
or something alike.
Check your casing. I'd suggest not varying method names by case only (at least not intentionally!)
itemTitleLabel.Text = _productService.GetById(productId).Name;
var productId = Request["productId"];
Your variable productId might be of type Object
and you don't have method GetById accepting object as parameter.
If so, I suggest you change line above with this one:
var productId = Request["productId"] as string;
Also, as said above check casing (GetById and GetByid)
精彩评论