上下文关键字“var”;可能只出现在局部变量声明内的问题

发布于 2024-11-18 22:05:05 字数 1924 浏览 4 评论 0原文

我知道这意味着什么,我已经搜索过 Google 和 MSDN。但是,我还能怎样解决这个问题呢?

我的 razor 代码中的 App_Code 文件夹(使用 WebMatrix)中有一个函数,我从数据库中获取信息,进行一些计算,然后使用新的总数更新数据库。

但是,如果 App_Code 文件夹中的方法不允许,我该如何将变量传递给我呢?

这是我得到的:

EditQuantity.cshtml(根文件夹):

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

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

并且,Base.cs(App_Code 文件夹内):

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 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

美人骨 2024-11-25 22:05:05

不能使用 var 关键字定义方法参数,因为编译器无法在编译时确定方法签名。

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

在另一条评论中,您说它无法将 IEnumerable 转换为 string。所以要正确声明参数!我不知道是哪一个。

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

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)
空名 2024-11-25 22:05:05

您不能使用 var 作为方法参数的类型。它必须是真实类型(字符串、整数、对象等)。 var 只能在方法内部使用(例如在 foreach 中)

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)

巴黎夜雨 2024-11-25 22:05:05

只需将参数列表中的变量替换为具体类型即可。

所以假设它们是字符串,你会得到这样的:

公共无效
计算购物车中的总价格(字符串
零件编号,字符串 描述,字符串
OrderId, bool IsBoxed)

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)

他夏了夏天 2024-11-25 22:05:05

和往常一样,这很愚蠢。
为什么不使用声明它作为构造函数的返回类型的类型?
作为“规则”,var 只能与构造函数一起使用。
如果你有一些深层次的问题..将dim引入csharp;

只需在编译时更改类引用的标记即可。

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.

蓝色星空 2024-11-25 22:05:05

这是你的实际代码吗?

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 必须是 OrderId。

但这并不能完全解决问题,因为 Session["OSFOID"] 是一些值的集合,而不是单个值。如果您确定它是单个值,请尝试

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

如果这不起作用,那么您放入 Session 中的内容不是您想象的那样。

关于

我尝试了一些方法来看看是否可以
工作,但没有。我显然正在做
错了,但这就是我的做法
桌面应用程序,我不明白为什么会这样
这里的网页有所不同,以及如何
我应该这样做吗?

这在桌面应用程序中也不起作用,您根本无法将 var 定义为参数类型。您可以获得的最接近的是动态,它不会为您提供智能感知(因为编译器不知道类型是什么),但它会让您传递编译器未知的类型并使用它(假设知道实际类型是什么)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文