The context keyword "var" can only appear in local problems with variable declaration

I know what that means, and I searched Google and MSDN. But, how else am I going to get around this?

I have a function in my razor code inside the App_Code folder (using WebMatrix), and I get the information from the database, do some calculations, and then update the database with a new result.

But how do I pass variables to my method in the App_Code folder if this does not allow me?

Here is what I have:

EditQuantity.cshtml (root folder):

        try
        {
            Base baseClass;
            baseClass.CalculateTotalPriceInCart(Request.QueryString["PartNumber"], Request.QueryString["Description"], Session["OSFOID"], true);

            Response.Redirect("~/Cart.cshtml");
        }
        catch(Exception exmes)
        {
            message = exmes;
        }

And, Base.cs (inside the App_Code folder):

using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using WebMatrix.Data;

/// <summary>
/// Summary description for ClassName
/// </summary>
public class Base
{   
    public void CalculateTotalPriceInCart(var PartNumber, var Description, var OrderId, bool IsBoxed)
    {
        var database = Database.Open("OSF");
        var query = "";
        var result = "";
        decimal price = 0.00M;

        if(IsBoxed)
        {
            // Select item.
            query = "SELECT Boxes, BoxPrice FROM Cart WHERE OrderId = '" + OrderId + "' AND PartNumber = '" + PartNumber + "' AND Description = '" + Description + "' AND IsBoxed = 1";
            result = database.Query(query);

            // Recalculate Price.
            foreach(var item in result)
            {
                price = result.Boxes * result.BoxPrice;
            }

            // Update item.
            query = "UPDATE Cart SET BoxPrice = '" + price + "' WHERE OrderId = '" + OrderId + "' AND PartNumber = '" + PartNumber + "' AND Description = '" + Description + "' AND IsBoxed = 1";
            database.Execute(query);
        }
    }
}

, , , . , , , , , , -, ?

!

+5
4

var, .

public void CalculateTotalPriceInCart(
         string Description, 
         string PartNumber, 
         string OrderId, 
         bool IsBoxed)

, IEnumerable<dynamic> string. ! , .

public void CalculateTotalPriceInCart(
         IEnumerable<dynamic> Description, // for example
         string PartNumber, 
         string OrderId, 
         bool IsBoxed)
+5

vars .

, , , :

public void CalculateTotalPriceInCart ( PartNumber, , OrderId, bool IsBoxed)

+2

var . (, int, Object ..). var (, foreach)

+2

?

Base baseClass;
baseClass.CalculateTotalPriceInCart(Request.QueryString["PartNumber"], Request.QueryString["Description"], Session["OSFOID"], true);

.

, :

public void CalculateTotalPriceInCart(string PartNumber, string Description, IEnumerable<dynamic> OrderId, bool IsBoxed)

PartNumber Description , , true bool, , IEnumerable<dynamic>, OrderId.

, Session["OSFOID"] - , . , ,

CalculateTotalPriceInCart(Request.QueryString["PartNumber"], Request.QueryString["Description"], ((IEnumerable<string>)Session["OSFOID"]).FirstOrDefault(), true);

, , , , .

, , , . , , , , , , -, shold ?

This also does not work in a desktop application, you simply cannot define it varas a parameter type. The closest you can get is dynamicthat Intellisense will not give you (since the compiler does not know what the type is), but it will allow you to pass a type unknown to the compiler and use it (provided that you know what the actual a type)

+1
source

All Articles