473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Get System Default Types

Hi All,

I have the string value of the System types.. now I need to get the default
value for that system type..

I have string s = "int";

not I need string sd = "0";

sd is the default for the int type..

same way is it is a bool then default value should be "false"

How can I do this..

Nirosh.
Nov 16 '05 #1
2 1400
This does what you described, but probably not what you expect...:

static string GetDefaultValueString(string typeName)
{
Type t = Type.GetType(typeName);
object o = Activator.CreateInstance(t);
return o.ToString();
}

Why do I say it won't do what you expect? Well here's my test code:

Console.WriteLine(GetDefaultValueString("System.In t32"));
Console.WriteLine(GetDefaultValueString("System.Bo olean"));

and this prints out:

0
False

Note that I'm using the real names of the types here. You said:
I have string s = "int";
That's not the name of the type. That's the name of an alias C# happens to
use as a shorthand for System.Int32.

The only mechanism I'm aware of for mapping from one of these C# aliases to
the real type name is the C# compiler itself! You could use the codedom to
do this, but it's rather heavyweight - a much simpler solution would just be
to have a hard-coded list for all these types.

Do you really need to use the C#-specific aliases for these system types?

Also, note that the default value of Boolean prints out as "True" not
"true", so again that's not valid C#. So this might not do what you
require. What exactly are you trying to achieve?
--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/

"Champika Nirosh" <te**@test.lk> wrote:
I have the string value of the System types.. now I need to get the
default
value for that system type..

I have string s = "int";

not I need string sd = "0";

sd is the default for the int type..

same way is it is a bool then default value should be "false"

How can I do this..

Nov 16 '05 #2
This does what you described, but probably not what you expect...:


Plus it doesn't work for reference types unless you add something like

if ( !t.IsValueType ) return "null";

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
3
by: System.Reflection Activator | last post by:
************************************** //Load the Assembly Assembly a = Assembly.LoadFrom(sAssembly); //Get Types so we can Identify the Interface. Type mytypes = a.GetTypes(); BindingFlags...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.