Solved

Equivalent of New String("") in C#

Posted on 2014-03-20
9
222 Views
Last Modified: 2014-03-25
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
0
Comment
Question by:shah36
9 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 39942451
this should do:
objReplacer = string.Empty;
0
 

Author Comment

by:shah36
ID: 39942480
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;
                }
???????
0
 
LVL 5

Expert Comment

by:indrajitmahajan
ID: 39942482
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());
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 39942534
This would be cleaner, IMO:

if (strDBType is char || strDBType is string || strDBType is DateTime)
{
    objREplacer = string.Empty;
}
else if (strDBType is int)
{
    objREplacer = default(int);
}
else if (strDBType is decimal)
{
    objREplacer = default(decimal);
}

Open in new window

0
 

Author Comment

by:shah36
ID: 39942550
Oh thanks a lot. Yes it is cleaner and works fine

regards,
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39942581
I would have expected, at least, a split with Guy Hengel's comment (http:#a39942451), since he answered your original question.
0
 

Author Comment

by:shah36
ID: 39942584
Oh thanks how can i edit this now? or should we ask moderator to make a split?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39942614
Use the "Request Attention" link below your original question.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question