代码中的无根据错误 [英] Baseless error in code

查看:57
本文介绍了代码中的无根据错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了简单的代码,我很确定没有错误,但编译器说它是。

这是所有代码。

I have written simple code, I am pretty sure there is no mistake but compiler says it is.
Here is all code.

@{Layout="_Layout.cshtml";
  var authenticated= WebSecurity.IsAuthenticated;
 
 if(authenticated)
{ 
 var db = Database.Open("aaa");
 var user = db.QuerySingle("SELECT * FROM UserProfile WHERE UserId=@0",WebSecurity.CurrentUserId);
 Array userData = File.ReadAllLines(Server.MapPath("~/App_Data/"+user.Group)); 
 } 
  }



@if(authenticated)
{
foreach (string dataLine in userData) 
{
  foreach (string dataItem in dataLine.Split(',')) 
  {@dataItem <text> </text>}
  <br />

}
}



和错误


and error

Compiler Error Message: CS0103: The name 'userData' does not exist in the current context

Source Error:


Line 14: @if(authenticated)
Line 15: {
Line 16: foreach (string dataLine in userData) 
Line 17: {
Line 18:   foreach (string dataItem in dataLine.Split(',')) 



谢谢。 :)


Thanks. :)

推荐答案

编译器是正确的,你错了。错误出现在代码的第二个片段中: foreach(userData中的字符串dataLine)... 此处,未声明 userData 。如果,由一些奇怪的原因。你认为上面提到的 Array userData 会产生任何不同,你错了:这是stack(local)变量的声明,它只存在于当前的堆栈框架中,宣布它的地方。



此外,计算你显示 userData 对象毫无意义:当你离开上下文(包含' {...} '对)时,此对象变为无法访问并最终被摧毁垃圾收集。要了解它,请参阅:

http://en.wikipedia.org/wiki / Garbage_collection_%28computer_science%29 [ ^ ]。



-SA
Compiler is correct, you are wrong. The error is in the second fragment of code: foreach (string dataLine in userData) … Here, userData is not declared. If, by some weird reason. you think that having Array userData mentioned above makes any difference, you are wrong: this is the declaration of the stack (local) variable, which only exist in the current stack frame, where it was declared.

Moreover, calculating the userData object you show makes no sense at all: when you go out of context (enclosing '{… }' pair), this object becomes unreachable and eventually gets destroyed by the Garbage Collection. To understand it, please see:
http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^].

—SA


您需要在当前范围之外声明userData;即在花括号外面的顶部声明它。
You need to declare userData outside of the current scope; i.e declare it at the top, outside of the curly braces.


这篇关于代码中的无根据错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆