Link to home
Start Free TrialLog in
Avatar of Ali Shah
Ali ShahFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Equivalent of New String("") in C#

hi there i have a variable of object type in my C# method

object objReplacer.

i want to check the type of a value returned in another variable and if the value is typeof string then  i want the equivalent of this in C#


 ObjReplacer = New String("")    (This is VB.Net code i want equivalent of this in C#)

kind regards
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ali Shah

ASKER

Thanks for your help but just want to make sure that the following code will work

if (strDBType.GetType() == typeof(char) || strDBType.GetType() == typeof(string) || strDBType.GetType() == typeof(DateTime))
                {
                    objREplacer = string.Empty;
                }
                else if (strDBType.GetType() == typeof(int))
                {
                    objREplacer = new int();
                    objREplacer = 0;
                }
                else if (strDBType.GetType() == typeof(decimal))
                {
                    objREplacer = new decimal();
                    objREplacer = 0.0;
                }
???????
hii

you may write it like this:

object o = "Some string";

The static type of the variable is object, but at run time, the type of the variable's referent is string. Therefore, the next line will print "System.String" to the console:

Console.WriteLine(o.GetType());
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh thanks a lot. Yes it is cleaner and works fine

regards,
I would have expected, at least, a split with Guy Hengel's comment (http:#a39942451), since he answered your original question.
Oh thanks how can i edit this now? or should we ask moderator to make a split?
Use the "Request Attention" link below your original question.