Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Equivalent of New String("") in C#

Posted on 2014-03-20
9
Medium Priority
?
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

 
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

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

660 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