Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Equivalent of New String("") in C#

Posted on 2014-03-20
9
Medium Priority
?
228 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 1000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

886 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