web-dev-qa-db-fra.com

Pour vérifier si var est de type String

J'ai un problème de code en C #:

Je ne sais pas comment implémenter la logique - itérant via Hashtable Ayant des valeurs de types de données différents, le schéma que je souhaite est ci-dessous:

if the value in variable is String type
{
 do action1;
}
else 
{
  do action2;
}

Il y a une table de hachage contenant des données de Types - String et Int (combinées):

public string SQLCondGenerator {

        get
        {

            Hashtable conditions = new Hashtable();

            //data having String data type
            conditions.Add("miap", ViewState["miap_txt"]);
            conditions.Add("pocode", ViewState["po_txt "]);
            conditions.Add("materialdescription", ViewState["mat_desc_txt"]);
            conditions.Add("suppliername", ViewState["supplier_txt"]);
            conditions.Add("manufacturername", ViewState["manufacturer_txt"]);

            //data having Int32 data type
            conditions.Add("spareparts", ViewState["sp_id"]); 
            conditions.Add("firstfills", ViewState["ff_id"]);
            conditions.Add("specialtools", ViewState["st_id"]);
            conditions.Add("ps_deleted", ViewState["ps_del_id"]);
            conditions.Add("po_manuallyinserted", ViewState["man_ins_id"]);

            String SQLCondString = "";
            String SQLCondStringConverted = "";


            string s = string.Empty;
            foreach (string name in conditions.Keys) 
            {
                if (conditions[name] != null)
                {
                    SQLCondString += name+ "=" +conditions[name]+ " and ";
                    Response.Write(conditions[name].GetType());

                    bool valtype = conditions[name].GetType().IsValueType;
                    if (valtype == string)
                    {
                      SQLCondString.Substring(0, SQLCondString.Length - 4);
                      SQLCondString += name + " and like '%" + conditions[name] + "%' and ";
                    }
                }
            }

        //Response.Write("********************");
         SQLCondStringConverted = SQLCondString.Substring(0, SQLCondString.Length - 4);
         return SQLCondStringConverted;
        }   
    }

Peut-être que je me trompe dans le codage, s'il vous plaît aviser!

Merci!

13
Bulat Makhmutov
if(conditions[name] is string)
{
}
else
{
}
36
Lee

Hmm, je ne sais pas pourquoi vous appelez IsValueType, mais cela devrait suffire:

if (conditions[name] is string) 
{
    ///  
}
4
Candide

Approche - 1

Int32 Val = 0;
if (Int32.TryParse("Your Value", out Val))
{
    //Your Logic for int
}
else
{
    //Your Logic for String
}

Approche - 2 (en utilisant une liaison tardive)

Int32 Val = 0;
dynamic conditions = new Hashtable();
conditions.Add("miap", ViewState["miap_txt"]);
conditions.Add("pocode", ViewState["po_txt "]);
foreach (string name in conditions.Keys)
{
    if (Int32.TryParse(conditions[name].ToString(), out Val))
    {
        //Your Logic for int
    }
    else
    {
        //Your Logic for String
    }
}
0
Koko