C#: access variable in the inner scope from the outer scope

大憨熊 提交于 2019-12-12 02:52:28

问题


I'm creating an instance of a class inside a while loop - I need the while loop to get the input arguments i need to create the instance (I have to read it in from an extern file). But then I would like to have access to the instance from outside the loop. Is there a possibility?

public static <class> leseProjektDatei()
{

while ((zeile = datei.ReadLine()) != null)
       {
           Debug.WriteLine(zeile);
           string id, wetterdatei, fruchtfolge, bodenprofil, grundwasser;
           id = zeile.Substring(0, 5);
           wetterdatei = zeile.Substring(7, 14);
           fruchtfolge = zeile.Substring(22, 14);
           bodenprofil = zeile.Substring(37, 14);


           <class> projekt = new <class>(id, wetterdatei, fruchtfolge, bodenprofil);

            } 
 return <class>
}

回答1:


Yes do it like this:

public class Project{
    public string id{get;set;} 
    public string wetterdatei{get;set;}
    public string fruchtfolge {get;set;} 
    public string bodenprofil {get;set;} 
}


public static Project leseProjektDatei(){
Project projekt = new Project();
while ((zeile = datei.ReadLine()) != null)
       {
           projekt.id = zeile.Substring(0, 5);
           projekt.wetterdatei = zeile.Substring(7, 14);
           projekt.fruchtfolge = zeile.Substring(22, 14);
           projekt.bodenprofil = zeile.Substring(37, 14);

             } 
return projekt;
}

Name the class anyway you would like.




回答2:


Seems like you need a List of YourClass objects, for which the following code should work:

public static IEnumerable<YourClass> leseProjektDatei()
{
var list = new List<YourClass>();
while ((zeile = datei.ReadLine()) != null)
       {
           Debug.WriteLine(zeile);
           string id, wetterdatei, fruchtfolge, bodenprofil, grundwasser;
           id = zeile.Substring(0, 5);
           wetterdatei = zeile.Substring(7, 14);
           fruchtfolge = zeile.Substring(22, 14);
           bodenprofil = zeile.Substring(37, 14);


           list.Add(new YourClass(id, wetterdatei, fruchtfolge, bodenprofil));

       } 
return list;
}



回答3:


Use List<class> and add newly created instances of class to it in the while loop. Afterwards every created class is stored and can be accessed?




回答4:


No, you cannot retrieve the object outside of the while loop. One way around this is to add the object to an array that was declared outside of the loop, during the loop and returning the value of that array. This is assuming you want to create a new object with every iteration of the while loop. But due to your implementation it is unclear.




回答5:


You can not retrive it outside of the loop, try delcaring it out of the loop and then you will be able to access it.

public static <class> leseProjektDatei()
{
 <class> projekt;
while ((zeile = datei.ReadLine()) != null)
       {
           Debug.WriteLine(zeile);
           string id, wetterdatei, fruchtfolge, bodenprofil, grundwasser;
           id = zeile.Substring(0, 5);
           wetterdatei = zeile.Substring(7, 14);
           fruchtfolge = zeile.Substring(22, 14);
           bodenprofil = zeile.Substring(37, 14);


            projekt = new <class>(id, wetterdatei, fruchtfolge, bodenprofil);

            } 
 return projekt;
}


来源:https://stackoverflow.com/questions/25802195/c-access-variable-in-the-inner-scope-from-the-outer-scope

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!