-2

I am attempting to teach myself C# and could really use some help grasping how to count a list from another .cs file (Note: Most of what I found when searching online made me confused, so I figured it wouldn't hurt to ask a question specific to what I have going on).

Using VSCode, I have the following folders: MAIN (which has Program.cs) \ C#_ASSETS (Which has Class.cs & List.cs)

The Class.cs file contains the following:

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;
        }
    }
}

        

The List.cs file contains the following:

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);
        }
    }
}
        

The Program.cs file contains the following:

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);
                }
            }
        }
    }
}

Within the Program.cs file, I'm getting the following error in: for(i = 1; i <= LIST_ONE.Count;):

The name 'LIST_ONE' does not exist in the current context

Not sure what happened to the answer post I saw, but someone posted this link: .NET Fiddle

E_net4
  • 27,810
  • 13
  • 101
  • 139
  • Because that variable doesn't exist in that context. You never call your `GET_LISTS` method, and that method never returns anything anyway. Did you mean to have it return the list, and then to call the method and use the returned list? Surely the introductory tutorial(s) you're using include variable scope, calling methods, and returning values? – David Aug 24 '22 at 18:13
  • Welcome to stack overflow, and to your C# learning journey. The problem is that LIST_ONE only exists within the scope of MY_LIST.GET_LISTS. Don't take this harshly but it seems like you might not have a firm grasp on OOP fundamentals. You may want to start there :) – Luke Aug 24 '22 at 18:13
  • This "question" would most likely be solved with a few more introductory tutorials, some specifically aimed at **Scope**. Here are some of Microsoft's documents regarding basic concepts in c# which goes over declaring **Members** and the ways you can access those members: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/basic-concepts – Ibrennan208 Aug 24 '22 at 18:52
  • Here are some interactive tutorials, provided by Microsoft as well, which may help you get more into the realm of Object Oriented Programming (OOP): https://learn.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/tutorials/ – Ibrennan208 Aug 24 '22 at 18:55
  • Hi there, I know you're new so you don't know this yet: please do not put your answer inline with your question - instead, if you want to answer your own question then post it as a separate answer. And don't forget to vote and/or mark answers if they helped you :) – slugster Aug 24 '22 at 20:58
  • I know this is unrelated to the question, but there are a few things bothering me that I would like to point out before you get into the habit of doing them. C# naming convention states that namespace, class, and method names should be PascalCase, not uppercase. Local variables (including method parameters) should be in camelCase. For more information, see [C# Coding Conventions](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions). – Jesse Aug 24 '22 at 22:32
  • Most companies will expect their developers to follow these basic naming conventions, and usually have more complex requirements outlined in a "style guide". – Jesse Aug 24 '22 at 22:38

1 Answers1

0

Here is a concrete example of what could work with your code. Change the GET_LIST() to return a List of ITEM instead of 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;
    }

Then in your Program.cs you actually need to create the list variable with this line before the for loop variable like this:

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

I hope that you can analyse this new code and better understand using reverse engineering!