I feel that this code can be rewritten and made shorter but doesn't find the right way

T

Tony Johansson

I'm not fully satisfied with this code. It feels like it could be made
smaller in some way.

protected void btnKop_Click(object sender, EventArgs e)
{
//Get shopping basket
Varukorg minKorg = (Varukorg)Session["varulistan"];
if (minKorg == null)
{
Product prod =
productRepository.GetProductById(Convert.ToInt32(lblId.Text));
Item item = new Item(prod.Id, prod.ProductName, 1,
prod.UnitPrice);

//Check if enough in stock
if (ExistInStock(item.Id, item.Antal))
{
minKorg = new Varukorg(item);
}
else
{
return;
}
}
else
{
Item item =
minKorg.GetItemByProdId(Convert.ToInt32(lblId.Text));

//Check if enough in stock
if (ExistInStock(item.Id, item.Antal + 1))
{
minKorg.AddItem(item);
}
else
{
return;
}
}

//Purt back shopping basket in Session
Session["varulistan"] = minKorg;

//Display shopping basket
GridViewKorg.DataSource = minKorg.VaruListan;
GridViewKorg.DataBind();

((MasterPage)this.Master).SwithONOFFProceedToCheckOut();
}

//tony
 
M

mikael

*sigh*

I emailed you a shorter version at 17.46...... just use that one and implement the constructors that is needed...
protected void btnKop_Click(object sender, EventArgs e)
{
//Hämta varaukorgen
Varukorg minKorg = (Varukorg)Session["varulistan"];
if (minKorg == null)
{
minKorg = new Varukorg();
}
Item item = new Item(Convert.ToInt32(lblId.Text));
//Kontrollera om önskat antal finns i lager
if (ExistInStock(item.Id, item.Antal))
{
minKorg.AddItem(item);
}
else
{
return;
}
//Lägg tillbaka varukorgen i Session
Session["varulistan"] = minKorg;

//Display shopping cart
GridViewKorg.DataSource = minKorg.VaruListan;
GridViewKorg.DataBind();

((MasterPage)this.Master).SwithONOFFProceedToCheckOut();
}


Den fredagen den 2:e maj 2014 kl. 21:58:54 UTC+2 skrev Tony Johansson:
 
Top