Solved

Equivalent of New String("") in C#

Posted on 2014-03-20
9
220 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
 
LVL 74

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

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

regards,
0
 
LVL 74

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 74

Expert Comment

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now