检查var是否为字符串类型

时间:2022-03-22 17:01:11

I have a problem in code in C#:

我的c#代码有问题:

I don't know how to implement logic - iterating through Hashtable having values of different data types, the schema I want is below:

我不知道如何实现逻辑——遍历具有不同数据类型值的Hashtable,我想要的模式如下:

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

There is a hashtable containing data of Types - String and Int (combined):

有一个包含类型字符串和Int(组合)数据的散列表:

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;
        }   
    }

May be I am wrong in coding, please advise!

可能是我编码有误,请告知!

Thanks!

谢谢!

3 个解决方案

#1


33  

if(conditions[name] is string)
{
}
else
{
}

#2


4  

Hmm, I'm not sure why you are calling IsValueType, but this should be sufficient:

嗯,我不知道你为什么要调用IsValueType,但这应该足够了:

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

#3


0  

Approach - 1

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

Approach - 2 (using Late Binding)

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
    }
}

#1


33  

if(conditions[name] is string)
{
}
else
{
}

#2


4  

Hmm, I'm not sure why you are calling IsValueType, but this should be sufficient:

嗯,我不知道你为什么要调用IsValueType,但这应该足够了:

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

#3


0  

Approach - 1

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

Approach - 2 (using Late Binding)

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
    }
}