簡體   English   中英

C#,從 another.cs 文件中計算 List<> 並使用 Console.WriteLine 打印字符串值

[英]C#, Counting a List<> from another .cs file and printing the string value with Console.WriteLine

我正在嘗試自學 C# 並且可以真正使用一些幫助來掌握如何從 another.cs 文件中計算列表(注意:我在網上搜索時發現的大部分內容讓我感到困惑,所以我認為問一個特定於我正在發生的事情的問題)。

使用 VSCode,我有以下文件夾: MAIN (其中有 Program.cs)\ C#_ASSETS (其中有 Class.cs 和 List.cs)

Class.cs 文件包含以下內容:

namespace CLASSES{
    public class ITEM{
        public string Name {get;set;}
        public string Location {get;set;}

        public ITEM(string Name, string Location){
            this.Name = Name;
            this.Location = Location;
        }
    }
}

        

List.cs 文件包含以下內容:

using CLASSES;
namespace MY_LISTS{
    public class MY_LIST{
        public static void GET_LISTS(){

            List<ITEM> LIST_ONE = new List<ITEM>();
            var List_Item_1 = new ITEM("Test Name", "Test Location"); LIST_ONE.Add(List_Item_1);
        }
    }
}
        

Program.cs 文件包含以下內容:

using CLASSES;
namespace MYCODE{
    class Program{
        static void Main(string[] args){
            int i;
            for(i = 1; i <= LIST_ONE.Count; i++){
                if(LIST_ONE[i].Location == "Test Location"){
                    Console.WriteLine("LIST_ONE has an item named: " + LIST_ONE[i].Name  + " with a location of: " + LIST_ONE[i].Location);
                }
            }
        }
    }
}

在 Program.cs 文件中,我收到以下錯誤: for(i = 1; i <= LIST_ONE.Count;)

當前上下文中不存在名稱“LIST_ONE”

編輯:不確定我看到的答案帖子發生了什么,但有人發布了這個鏈接(然后它被否決了?) .NET Fiddle

回答:

Class.cs 文件現在包含以下內容:

namespace CLASSES{
    public class ITEM{
        public string Name {get;set;}
        public string Location {get;set;}

        public ITEM(string Name, string Location){
            this.Name = Name;
            this.Location = Location;
        }
    }
}

List.cs 文件現在包含以下內容:

using CLASSES;
public class MY_LIST{
    public static List<ITEM> GET_LISTS(){
        List<ITEM> LIST_ONE = new List<ITEM>();
        var List_Item_1 = new ITEM("Test Name", "Test Location"); LIST_ONE.Add(List_Item_1);
        return LIST_ONE;
    }
}

Program.cs 文件現在包含以下內容:

public class Program{
    public static void Main(){
        int i;
        var list = MY_LIST.GET_LISTS();
        for(i = 0; i < list.Count; i++){
            if (list[i].Location == "Test Location"){
                Console.WriteLine("LIST_ONE has an item named: " + list[i].Name  + " with a location of: " + list[i].Location);
                Console.ReadLine();
            }
        }
    }
}

這是一個可以與您的代碼一起使用的具體示例。 更改 GET_LIST() 以返回 ITEM 列表而不是 void:

    public static List<ITEM> GET_LISTS(){

        List<ITEM> LIST_ONE = new List<ITEM>();
        var List_Item_1 = new ITEM("Test Name", "Test Location");
        LIST_ONE.Add(List_Item_1);
        return LIST_ONE;
    }

然后在您的 Program.cs 中,您實際上需要在 for 循環變量之前使用這一行創建列表變量,如下所示:

        List<ITEM> LIST_ONE = MY_LIST.GET_LIST();

我希望您可以分析這段新代碼並使用逆向工程更好地理解!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM