Solved

Equivalent of New String("") in C#

Posted on 2014-03-20
9
223 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 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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