Nádia Teles
Nádia Teles

Reputation: 33

Multiple instances of the class

I edited my post informing more details.

I have a Terminal class and I dynamically create some instances of it.

It has a timer that if the Lista > 0 will communicate with the device.

public class Terminal 
{
        public string Endereco { get; set; }
        List<string> Lista = new List<string>();
        System.Timers.Timer timer;
 
        public Terminal()
        {     
            this.timer = new System.Timers.Timer(); 
            this.timer.Interval = 500;     
            this.timer.Enabled = true;         
            this.timer.Elapsed += ProcessaTimer;
        }

        private void ProcessaTimer(object sender, System.Timers.ElapsedEventArgs e)
        {   
              if (Lista.count > 0)      
                 ProcessaLista();
        }      

        public void ProcessaLista()
        {
            //Send command to device

        }

       public void AdicionaLista(Comando comando)
        {
            Lista.Add(comando));
        } 
}

I have the Principal class that instantiates the Terminal class and a method that receives information from other processes:

These instances are dynamic, I put these values ​​just to exemplify

public class Principal
{
    public Principal()
    {
        Terminal Obj = new Terminal();
        Obj .Endereco = "192.168.0.100";

        Obj = new Terminal();
        Obj .Endereco = "192.168.0.200";

        Obj = new Terminal();
        Obj .Endereco = "192.168.0.300";
    }
        
    void RecebeDados(Comando comando)
    {
        //if comando.Endereco == "192.168.0.100"
        //You must add to the list referring to the instance whose address is 192.168.0.100


        //if comando.Endereco == "192.168.0.200"
        //You must add to the list referring to the instance whose address is 192.168.0.200
        //

        //if comando.Endereco == "192.168.0.300"
        //You must add to the list referring to the instance whose address is 192.168.0.300
        //

    }
}

Command class that receives the data

public class Comando
{
    public string Endereco { get; set; }
    public string Mensagem { get; set; }
}

The question I have is on the ReceiveData method. How to put each command received in the list corresponding to the terminal? Each command goes to the terminal equivalent to the IP address. A command must be added in only 1 list according to IP address

How to make?

Upvotes: 2

Views: 265

Answers (2)

johannespartin
johannespartin

Reputation: 501

I see two options here. Using a dictionary or a list and query it with Linq. Dictionary might be the best solution:

public class Terminal
{
    private static Dictionary<string, Terminal> _allTerminals = new Dictionary<string, Terminal>();

    public string Endereco { get; set; }
    List<string> Lista = new List<string>();

    public Terminal(string ip) 
    {
        Terminal._allTerminals.Add(ip, this);
    }

    public static Terminal GetTerminalByIp(string ip) 
    {
        return Terminal._allTerminals[ip];
    }

    ~Terminal()  // finalizer
    {
         Terminal._allTerminals.Remove(this.Endereco);
    }

    public void AdicionaLista(string comando)
    {
        Lista.Add(comando));
    }
 }

And you then instantiate and get the object like so:

Terminal obj = new Terminal("192.168.1.1");


Terminal fetchedTerminal = Terminal.GetTerminalByIp("192.168.1.1");

Upvotes: 2

JonasH
JonasH

Reputation: 36361

You create a list of objects

var myList = new List<Terminal>();
myList.Add(new Terminal(){Endereco = "192.168.0.100"});
...

you can then use linq to find the correct item

myList.First(t => t.Endereco == myAddress).Lista.Add(myReceivedValues);

Note that the this example does not handle if there is no corresponding item. You could use FirstOrDefault and check for null to handle this. Also, a regular list is fine if there are few values. If there are a many, say thousands or more, it would be better to use a dictionary than a plain list.

Upvotes: 0

Related Questions