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!

marcind
  • 52,944
  • 13
  • 125
  • 111
bendr
  • 2,415
  • 6
  • 19
  • 24
  • 2
    As a side-note: Prepared/Parameterized statements are your friend. Don't build queries by string concatenations unless you have a really good reason. – CodesInChaos Jul 08 '11 at 18:03

5 Answers5

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)
  • I already tried that, but it gave me this error: `Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'string'` – bendr Jul 08 '11 at 17:57
  • 3
    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. –  Jul 08 '11 at 18:10
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)

Jim Deville
  • 10,632
  • 1
  • 37
  • 47
  • I tried using string, but then it throws an `Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'string'` exception – bendr Jul 08 '11 at 17:54
  • Then you should use IEnumerable as the type, or possibly just IEnumerable – Jim Deville Jul 08 '11 at 17:55
  • I tried using dynamic as the type, but gave the exact same error in my question – bendr Jul 08 '11 at 17:58
  • did you have other functions taking var parameters? dynamic is allowed as a method type. Also, try just IEnumerable – Jim Deville Jul 08 '11 at 18:17
  • 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. – bendr Jul 08 '11 at 18:31
  • Can you edit your post to include the attempt with dynamic and/or IEnumerable and the error + stack you get when you run it – Jim Deville Jul 08 '11 at 18:32
  • 2
    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. – Lasse V. Karlsen Jul 08 '11 at 20:33
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)

heisenberg
  • 9,665
  • 1
  • 30
  • 38
  • Nooope! Doesn't work sorry. Already tried that, and get this: `Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'string'` – bendr Jul 08 '11 at 17:56
  • 2
    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. – heisenberg Jul 08 '11 at 17:58
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.

Fr Anconia
  • 11
  • 1
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)

Davy8
  • 30,868
  • 25
  • 115
  • 173