Alex Rivera | Logout

The contextual keyword 'var' may only appear within a local variable declaration issues

Asked 2011-07-08T17:50:55.587
9

I know what this means, and I have 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 info from the database, do some calculations, then update the database with the new total.

But, how am I to pass variables to my method in the App_Code folder if it won't let me?

Here's what I've got:

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 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 = 
Edit
Report

3 Answers

9

You can't define method parameters with the var keyword because the compiler cannot determine the method signature at compile time.

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

In another comment you said it can't cast IEnumerable<dynamic> to a string. So declare the parameter properly! I don't know which it is.

public void CalculateTotalPriceInCart(
         IEnumerable<dynamic> Description, // for example
         string PartNumber, 
         string OrderId, 
         bool IsBoxed)
answered 2011-07-08T17:53:43.473
2

you can't use var as the type of a method parameter. It has to be a real type (string, int, Object, etc). var can only be used inside of a method (like in your foreach)

answered 2011-07-08T17:52:54.560
0

Is this your actual code?

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

That line shouldn't even compile.

Regarding the error casting, try:

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

Since PartNumber and Description are coming from the querystring, those are definitely string, and true is definitely bool so the thing that would be IEnumerable<dynamic> must be OrderId.

This doesn't completely solve the problem though, because Session["OSFOID"] is some collection of values and not a single value. If you're sure it's a single value, then try

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

If that doesn't work then what you're putting into Session isn't what you think it is.

Regarding

I've tried a few things to see if it'd work, but nope. I'm obviously doing it wrong, but this is how I do it in Desktop apps, I don't get why it'd be different here for WebPages, and how shold I go about doing this?

This wouldn't work in a desktop app either, you simply cannot define var as a parameter type. The closest you can get is dynamic which won't give you Intellisense (since the compiler doesn't know what the type is) but it will let you pass a type that's unknown to the compiler and use it (assuming you know what the actual type is)

answered 2011-07-08T20:37:42.313

Your Answer