Home > Net >  How can I move code to create a list into a separate class and then call it
How can I move code to create a list into a separate class and then call it

Time:10-15

Newby c#/asp.net question here. I am creating a website with .aspx pages.

Within a method I have created a list and added items to it as shown below:

var footballTeams = new List<string>();
footballTeams.Add("Arsenal");
footballTeams.Add("Aston Villa");
footballTeams.Add("Bournemouth");
footballTeams.Add("Brentford");
footballTeams.Add("Brighton & Hove Albion");
footballTeams.Add("Chelsea");

However, I want to move this code into its own separate class - so it is easier to update the teams rather than have to go deep into code.

How would I code this in a class and then be able to call and use the list from another class.

I have tried creating a new folder called App_Code. And then putting a dummy method in it as shown below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FootballResultPredictor.App_Code
{
    public class Class1
    {
        public static String MyMethod()
        {
            string myString = "test";
            return myString;

        }
    }
}

However when I try to call the method from the other class, it just can't seem to find it at all as shown here:

enter image description here

Below is the file structure: enter image description here

I am trying to call a method in Class1.cs from StartNewSeason.aspx

CodePudding user response:

Looking quickly at your code, I have some comments.

-Don't put your new class in a separate folder.

-This code is wrong: string a=myMethod(); because you can't call a method before you have an instance (new...) of a class unless the class is a static class, which is not the case here.

-Coding string b=Class1.MyMethod(), is better, but still wrong, since Class1 is the name of the class not the name of the object.

At this point, I guess the concept of the class and the class object are somewhat not very clear to you. I suggest that you review this fundamental concept as it is the core of Object Oriented Programming. Also using ASP.NET at this point of your learning path is highly not advisable, I highly recommend that you get to learn OO fundamentals through C# console applications or Windows Forms. These two frameworks, are much simpler to deal with.

When you create a class (of .cs type) file under the same solution in VS it would have a namespace and a class definition. If the class is not static, you refer to it as (other ways can be used as well):

myClassName ObjectName = new myClassName();

The namespace can be specified if the class is in a different project, like new NameSpace2.myClassName, but this is not the case here.

Only after you create an instance (an object) of the non-static class, you can use the object and its method using the ObjectName (not the myClassName). For exmaple:

ObjectName.MethodName();

Back to the program at hand, here is one way to have a separate class handle the list. There are many ways to do this, this way provides validation and allows you to iterate over the list items. Let me know if this is not clear.

The new class is here:

//using System;
//using System.Collections.Generic;
//using System.Data;
public class FootballTeams
{
    public List<string> footballTeams = new List<string>();

    public FootballTeams()
    {
        //initialize the list
        
        this.AddFootballTeam("Arsenal");
        this.AddFootballTeam("Aston Villa");
        this.AddFootballTeam("Bournemouth");
        this.AddFootballTeam("Brentford");
        this.AddFootballTeam("Brighton & Hove Albion");
        this.AddFootballTeam("Chelsea");
    }
    //Method to add a new team
    public void AddFootballTeam(string parmTeam)
    {
        //validate input
        if (string.IsNullOrWhiteSpace(parmTeam))
        { throw new NoNullAllowedException("Error:Team name is empty"); }
        if (footballTeams.Contains(parmTeam))
        { throw new DuplicateNameException(); }
        //if valid add the name to the list
        footballTeams.Add(parmTeam);
    }
}

A sample usage of the above class is:

var _t = new FootballTeams();
try
            {
                _t.AddFootballTeam("Ringers");
            }
catch (Exception _ex)
            { 
                Console.WriteLine(_ex.Message);
                return;
            }

foreach (var _team in _t.footballTeams)
            {
                Console.WriteLine(_team);
            }
  • Related