Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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. :)
Posted
Comments
jkirkerx 5-Aug-13 17:42pm    
I see user but not userData, but I'm not familiar with the language

var user = db.QuerySingle("SELECT * FROM UserProfile WHERE

I think you need a snickers bar
wirec aaht 5-Aug-13 17:48pm    
under variable user it is
Richard C Bishop 5-Aug-13 17:55pm    
What language is that?

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
 
Share this answer
 
v2
Comments
Joezer BH 6-Aug-13 7:31am    
5ed
Posh answers Sergey!
Sergey Alexandrovich Kryukov 6-Aug-13 11:16am    
Thank you very much.
—SA
You need to declare userData outside of the current scope; i.e declare it at the top, outside of the curly braces.
 
Share this answer
 
Comments
wirec aaht 5-Aug-13 18:05pm    
I updated it to this :
@{Layout="_Layout.cshtml";
var authenticated= WebSecurity.IsAuthenticated;
Array userData;
if(authenticated)
{
var db = Database.Open("Cosa Nostra Tips");
var user = db.QuerySingle("SELECT * FROM UserProfile WHERE UserId=@0",WebSecurity.CurrentUserId);
userData = File.ReadAllLines(Server.MapPath("~/App_Data/"+user.Group));
}
}



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

}
}
but it gives me error :
Compiler Error Message: CS0165: Use of unassigned local variable 'userData'

Source Error:


Line 14: @if(authenticated)
Line 15: {
Line 16: foreach (string dataLine in userData)
Line 17: {

which I dont understand is if condition is true userData should have value and also code should perfrom if its not than not.
R. Giskard Reventlov 5-Aug-13 18:12pm    
Declare it with an initial value.
http://msdn.microsoft.com/en-us/library/system.array.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900