Browse element of a Hashtable in c #

1

Well it turns out that I have a program where you ask the user to enter a barcode, name and price of a product then I keep these inside a Hashtable and in another class I have a method called ValidData and in this receipt as parameter the Hashtable, which I run with a foreach, the problem is that I need to get the value of an element within the foreach and verify that it does not exceed a certain number of characters but I do not know how to do it. this is a fraction of my code:

  public void nuevo(){
        c.WriteLine("Ingresar codigo de barras");
        string code =c.ReadLine();

        c.WriteLine("Ingresar nombre");
        string nom =c.ReadLine();

        c.WriteLine("Ingresar precio");
        double pre =Double.Parse(c.ReadLine());

        Hashtable valores = new Hashtable();
        valores.Add("Codigo", code);
        valores.Add("Nombre", nom);
        valores.Add("Precio", pre);

        prod.store(valores);
    }

public class ProductController     {         public ProductController ()         {         }

    public void store(Hashtable valores){

        ICollection keys = valores.Keys;
        foreach(Object f in keys){
            c.WriteLine(f + " = " + valores[f] );
            if(f.Equals("Codigo")){
  // Aqui necesito acceder al elemento con el key "Codigo"
  // y mandar un mensaje de error en caso de que el valor ingresado sea 
  // mayor a 13
            }else{ 

                if(f.Equals("Nombre")){

                }else{  

                    if(f.Equals("Precio")){

                    }
                }
            }

        }


    }

}
    
asked by MARCO ANTONIO LUNA SALAS 22.10.2018 в 05:25
source

1 answer

0

The Hashtable documentation is clear. Actually as inherited from a collection (IDictionary), you can access a particular element of it if you know the key.

As it seems, you know the key you are looking for, particularly it is easier to access it:

Reach with what you do:

valores["codigo"].lenght

But in reality, it seems that you are misusing the hashtable . If you are going to store three totally different values, a Hashtable does not make sense. It is used to access a complex object based on a key that is "hashed" (to put it in some way).

I can not give you more information, since it seems that what you are doing is totally wrong, or not very clear.

    
answered by 22.10.2018 / 05:48
source