上下文关键字“var”;可能只出现在局部变量声明内的问题
我知道这意味着什么,我已经搜索过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不能使用 var 关键字定义方法参数,因为编译器无法在编译时确定方法签名。
在另一条评论中,您说它无法将
IEnumerable
转换为string
。所以要正确声明参数!我不知道是哪一个。You can't define method parameters with the var keyword because the compiler cannot determine the method signature at compile time.
In another comment you said it can't cast
IEnumerable<dynamic>
to astring
. So declare the parameter properly! I don't know which it is.您不能使用
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)只需将参数列表中的变量替换为具体类型即可。
所以假设它们是字符串,你会得到这样的:
Just replace the vars in the parameter list with concrete types.
So assuming they're strings you would have this:
和往常一样,这很愚蠢。
为什么不使用声明它作为构造函数的返回类型的类型?
作为“规则”,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.
这是你的实际代码吗?
该行甚至不应该编译。
关于错误转换,请尝试:
由于 PartNumber 和 Description 来自查询字符串,因此它们肯定是字符串,并且
true
肯定是bool
所以应该是IEnumerable
必须是 OrderId。但这并不能完全解决问题,因为
Session["OSFOID"]
是一些值的集合,而不是单个值。如果您确定它是单个值,请尝试如果这不起作用,那么您放入 Session 中的内容不是您想象的那样。
关于
这在桌面应用程序中也不起作用,您根本无法将
var
定义为参数类型。您可以获得的最接近的是动态,它不会为您提供智能感知(因为编译器不知道类型是什么),但它会让您传递编译器未知的类型并使用它(假设您知道实际类型是什么)Is this your actual code?
That line shouldn't even compile.
Regarding the error casting, try:
Since PartNumber and Description are coming from the querystring, those are definitely string, and
true
is definitelybool
so the thing that would beIEnumerable<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 tryIf that doesn't work then what you're putting into Session isn't what you think it is.
Regarding
This wouldn't work in a desktop app either, you simply cannot define
var
as a parameter type. The closest you can get isdynamic
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)