skip to Main Content

I’m relatively inexperienced with C# in Visual Studio. I have created a class called Visitor and I want to track be able to access these visitors in different Forms. What would be the best way of doing this. I thought of having a Visitors class which was a list that could be accessed. However, I can’t seem to find a way of doing this. I get the following error

Error   CS1955  Non-invocable member 'Visitors' cannot be used like a method.

He is the code for my Visitors Class…

namespace VisitorSignInSystem
{
    public class Visitors
    {
        List<Visitor> current { get; set; } 

        public Visitors()
        {
            current = new List<Visitor>();
        }
        public Visitors(Visitor v) {
            current.Add(v);
        }
    }
}

I initialise the list on the first form….

public Form1()
{
    InitializeComponent();
    Visitors();
}

And try and ass something to the list in a different form…

        public VisitorSignInWelcome(Visitor v)
        {
            InitializeComponent();
            Visitors(v);
        }

Any help greatly appreciated or a signpost to the correct way of doing this please.

2

Answers


  1. First of all, lets clean up your class a littlebit:

        public class Visitors
        {
            private List<Visitor> current  = new List<Visitor>();
    
            public Visitors(){}
            public Visitors(Visitor v) => current.Add(v);
        }
    

    This ensures that the current list is always created. Note that there is not much point to such a class without additional methods or properties, I assume these have been omitted for brewity. Also consider removing the Visitors class completely, and just use a List<Visitor> to represent multiple visitors.

    Next, lets actually create an object from our class and assign it to a field:

    private Visitors visitors;
    public Form1()
    {
        InitializeComponent();
        visitors = new Visitors();
    }
    

    for the last part, lets share the same object between another form:

    private Visitors visitors;
    public Form2(Visitors visitors){
        InitializeComponent();
        this.visitors = visitors;
    }
    ....
    // in form1
    public void ShowForm2(){
        var form2 = new Form2(visitors);
        form2.Show();
    }
    

    Note that this is just a very simple example of the basics. As soon as you start actually making the UI more advanced the complexity greatly increases. As an example, if Form2 makes changes to your visitors, it may need to inform Form1 so it can update itself so the changes are visible to the user.

    You may also consider if there is some better term than "Visitor", since it may be conflated with the visitor-pattern, at least that is what I think of when I see a class named "Visitor".

    Login or Signup to reply.
  2. It sounds like you want to create a Singleton VisitorsService that provides CRUD-like functionality for a list of visitors.

    public interface IVisitorsService
    {
      Visitors { get; }
      void Add(Visitor v);
      void Remove(Visitor v);
    }
    public class VisitorsService: IVisitorsService
    {
      private readonly List<Visitor> _visitors = new();
      public Visitors => _visitors;
      public void Add(Visitor v) => _visitors.Add(v);
      public void Remove(Visitor v) ... etc
    }
    

    Put this service into your DI Container:

    builder.Services.AddSingleton<IVisitorsService, VisitorsService>();
    

    you can then inject IVisitorsService into a controller, class, etc., or can pull the singleton instance from the IServiceProvider that you inject.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search