Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To check if var is String type

Tags:

I have a problem in code in C#:

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

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):

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!

like image 445
Bulat Makhmutov Avatar asked Dec 19 '12 15:12

Bulat Makhmutov


People also ask

How do you check whether a variable is string or not in Python?

Method #1 : Using isinstance(x, str) This method can be used to test whether any variable is a particular datatype. By giving the second argument as “str”, we can check if the variable we pass is a string or not.

Can var be a string JS?

To declare variables in JavaScript, you need to use the var, let, or const keyword. Whether it is a string or a number, use the var, let, or const keyword for its declaration. But for declaring a string variable we had to put the string inside double quotes or single quotes.

How do you check if a variable is a string or an integer?

The most efficient way to check if a string is an integer in Python is to use the str. isdigit() method, as it takes the least time to execute. The str. isdigit() method returns True if the string represents an integer, otherwise False .

How do I check if a string is TypeScript?

Use the typeof Operator to Check if a Variable Is a String in TypeScript. The typeof is a TypeScript unary operator that returns the data type of the specified operand. The operator returns a string that denotes the type of the operand.


1 Answers

if(conditions[name] is string) { } else { } 
like image 142
Lee Avatar answered Oct 13 '22 20:10

Lee