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

5

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 = 1";
            database.Execute(query);
        }
    }
}

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?

Thank you!

Valve answered 8/7, 2011 at 17:50 Comment(1)
As a side-note: Prepared/Parameterized statements are your friend. Don't build queries by string concatenations unless you have a really good reason.Evasive
G
8

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)
Gilmour answered 8/7, 2011 at 17:53 Comment(2)
I already tried that, but it gave me this error: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<dynamic>' to 'string'Valve
See my update, Jase. There is no way for me to know what the types of your arguments are. You need to look at what you are trying to pass in.Gilmour
J
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)

Jilt answered 8/7, 2011 at 17:52 Comment(7)
I tried using string, but then it throws an Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<dynamic>' to 'string' exceptionValve
Then you should use IEnumerable<dynamic> as the type, or possibly just IEnumerableJilt
I tried using dynamic as the type, but gave the exact same error in my questionValve
did you have other functions taking var parameters? dynamic is allowed as a method type. Also, try just IEnumerableJilt
Thanks, but no I didn't have any other functions taking var params. I tried with just IEnumerable too, but it displayed the original error again.Valve
Can you edit your post to include the attempt with dynamic and/or IEnumerable and the error + stack you get when you run itJilt
Yes, please try to clean up your question with the actual problem. The problem here, now is that the existing answers answer the question you asked, but they don't answer the question about the problem you have, which is not the same any more. You should consider accepting an answer here, and reposting a new question with different details. Questions that migrate sideways in meaning are problematic like that.Hammers
N
1

Just replace the vars in the parameter list with concrete types.

So assuming they're strings you would have this:

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

Nedry answered 8/7, 2011 at 17:52 Comment(2)
Nooope! Doesn't work sorry. Already tried that, and get this: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<dynamic>' to 'string'Valve
which variable is coming through as an IEnumerable? It sounds like you may also have issues in the code that's populating those variables since it sounds like one is coming through as a collection, although the code appears to be expecting single values. In any event, the solution is to give actual types to your params that match up with what's coming in, but before that you need to make sure that those variables are holding the types you think that they are.Nedry
C
1

as usual, this is dumb. Why not use declare it as the type of the return type of the constructor?. as a "rule" var should only be used with constructors. If you have some deep problem with that.. introduce dim into csharp;

just change the token for a class reference when compiling.

Cooee answered 7/3, 2021 at 1:3 Comment(0)
P
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)

Pruett answered 8/7, 2011 at 20:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.