jeudi 24 avril 2014

c# - ne peut pas convertir implicitement en type « System.Collections.Generic.List <int>» à « int » - Stack Overflow


Receiving the error:


CS0029: Cannot implicitly convert type 'System.Collections.Generic.List' to 'int'


Not sure how to fix.


I am using:


Microsoft .NET Framework Version:2.0.50727.5477;


ASP.NET Version:2.0.50727.5479


Section that's giving me trouble is at:


{
debugStr = debugStr + "-a=noattributesadd";
CartItem item = new CartItem(context);
item.ProductId = product.ProductId;
item.Quantity = qty;
items.Add(item);
}

Specifically, the item.Quantity = qty; portion


Complete code is:


CartItemCollection items = new CartItemCollection();
Cart cart = Core.GetCartObject();
string skus = "";
string debugStr = "";
Product product = null;
List<int> qty = new List<int>();
foreach (string item in HttpContext.Current.Request.Form.GetValues("quantity_input"))

{
qty.Add(int.Parse(item));
}

try
{
string[] productNumbers = HttpContext.Current.Request.Form.GetValues("ProductNumber");
foreach (string productNumber in productNumbers)
{
debugStr = debugStr + "-p=" + productNumber;
if(!string.IsNullOrEmpty(productNumber.Trim()) && !productNumber.StartsWith("Enter Product #"))
{
try
{ //redirect if no product found
product = Core.GetProductObjectByProductNumber(productNumber);
}
catch (Exception e)
{
debugStr = debugStr + "-e=noproductfound";
continue; //do nothing, process the next user input
}
//check if we have a valid product object, allow virtual and other type(s) for adding directly to cart which may need special handling
if(product != null)
{
debugStr = debugStr + "-t=" + product.ProductTypeName;
if(!product.ProductTypeName.Equals("NORMAL"))
{
//assume VIRTUAL (or other type) and redirect for selecting child/group products or other special handling
form.Redirect("product.aspx?p=" + product.ProductNumber);
}
else
{
debugStr = debugStr + "-a=noattributesadd";
CartItem item = new CartItem(context);
item.ProductId = product.ProductId;
item.Quantity = qty;
items.Add(item);
}
skus = skus + ";" + productNumber;
product = null; //reset the product object in case the next product number submitted is invalid
} //product not null
} //sanity check for empty or default data
} //iterate on each product submitted
cart.AddItems(items);
form.Redirect("cart.aspx?skus=" + skus);
}
catch (Exception e)
{
form.AddError("*** ProductNumber provided was not found ***");
form.Redirect("quickorder.aspx?qo=2&e=" + e.Message);
return;
}

Essentially, this is the logic for a Quick Order form. I'm trying to add the qty of each item to the Cart.




You problem is in this line:


 item.Quantity = qty;  

item.Quantity is an int and qty is a List<int>




A guess at how to solve (assume all lists are in the same order and enumeration will read them in the same order):


int index = 0; // add this line
foreach (string productNumber in productNumbers)
{

// all the stuff you have already till:

item.Quantity = qty[index];

// all the stuff you have already

index = index + 1;
} //iterate on each product submitted

NOTE: I HATE THIS SOLUTION. But it will probably work.


A good solution would be to create a data structure that holds both the productnumber and the quantity in the same list.



Receiving the error:


CS0029: Cannot implicitly convert type 'System.Collections.Generic.List' to 'int'


Not sure how to fix.


I am using:


Microsoft .NET Framework Version:2.0.50727.5477;


ASP.NET Version:2.0.50727.5479


Section that's giving me trouble is at:


{
debugStr = debugStr + "-a=noattributesadd";
CartItem item = new CartItem(context);
item.ProductId = product.ProductId;
item.Quantity = qty;
items.Add(item);
}

Specifically, the item.Quantity = qty; portion


Complete code is:


CartItemCollection items = new CartItemCollection();
Cart cart = Core.GetCartObject();
string skus = "";
string debugStr = "";
Product product = null;
List<int> qty = new List<int>();
foreach (string item in HttpContext.Current.Request.Form.GetValues("quantity_input"))

{
qty.Add(int.Parse(item));
}

try
{
string[] productNumbers = HttpContext.Current.Request.Form.GetValues("ProductNumber");
foreach (string productNumber in productNumbers)
{
debugStr = debugStr + "-p=" + productNumber;
if(!string.IsNullOrEmpty(productNumber.Trim()) && !productNumber.StartsWith("Enter Product #"))
{
try
{ //redirect if no product found
product = Core.GetProductObjectByProductNumber(productNumber);
}
catch (Exception e)
{
debugStr = debugStr + "-e=noproductfound";
continue; //do nothing, process the next user input
}
//check if we have a valid product object, allow virtual and other type(s) for adding directly to cart which may need special handling
if(product != null)
{
debugStr = debugStr + "-t=" + product.ProductTypeName;
if(!product.ProductTypeName.Equals("NORMAL"))
{
//assume VIRTUAL (or other type) and redirect for selecting child/group products or other special handling
form.Redirect("product.aspx?p=" + product.ProductNumber);
}
else
{
debugStr = debugStr + "-a=noattributesadd";
CartItem item = new CartItem(context);
item.ProductId = product.ProductId;
item.Quantity = qty;
items.Add(item);
}
skus = skus + ";" + productNumber;
product = null; //reset the product object in case the next product number submitted is invalid
} //product not null
} //sanity check for empty or default data
} //iterate on each product submitted
cart.AddItems(items);
form.Redirect("cart.aspx?skus=" + skus);
}
catch (Exception e)
{
form.AddError("*** ProductNumber provided was not found ***");
form.Redirect("quickorder.aspx?qo=2&e=" + e.Message);
return;
}

Essentially, this is the logic for a Quick Order form. I'm trying to add the qty of each item to the Cart.



You problem is in this line:


 item.Quantity = qty;  

item.Quantity is an int and qty is a List<int>




A guess at how to solve (assume all lists are in the same order and enumeration will read them in the same order):


int index = 0; // add this line
foreach (string productNumber in productNumbers)
{

// all the stuff you have already till:

item.Quantity = qty[index];

// all the stuff you have already

index = index + 1;
} //iterate on each product submitted

NOTE: I HATE THIS SOLUTION. But it will probably work.


A good solution would be to create a data structure that holds both the productnumber and the quantity in the same list.


0 commentaires:

Enregistrer un commentaire