I reflected the codes of "Text" property for a TextBox control in web application, here's it:
[Editor("System.ComponentModel.Design.MultilineStringEditor,System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)), Localizable(true), Bindable(true, BindingDirection.TwoWay), WebCategory("Appearance"), DefaultValue(""), WebSysDescription("TextBox_Text"), PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
public virtual string Text
{
get
{
string str = (string) this.ViewState["Text"]; if (str != null) { return str; } return string.Empty;
}
set
{
this.ViewState["Text"] = value;
}
My question is so simple:
Plz have a look at my bold,underlined statement, this will avoid "null" value.
But if ViewState["Text"] contains a Null value, how it can be directly cast converted to string?
Why Microsoft doesn't use this instead:
public virtual string Text
{
get
{
string str = this.ViewState["Text"] as string;
if (str != null)
{
return str;
}
return string.Empty;
}
set
{
this.ViewState["Text"] = value;
}
Both codes can work because
you are typecasting the ViewState value and due to that you are to convert it in to string.
View state data is actually stored in one or more hidden fields as base64-encoded strings. You can access view state information using the page's
ViewState property, which exposes a dictionary object. Because the data in view state is stored as a string, only objects
that can be serialized can be stored.
The MS code may be considered slightly better because if something happened to ViewState["Text"] (either by a corruption or mailiciously) then the MS code would throw an exception when the cast failed but your code would silently return string.Empty with
no indication that anything bad had happened.
(C) Paul is simply trying to get across his point:
Paul Linton
Null is not a class.
There is no explicit cast convert function because there is nothing to convert
Examples:
for purpose of illustration, with a nullable value type:
System.Int32? nullableInt32 = 20130120;
Console.WriteLine (nullableInt32);
nullableInt32 = (System.Int32?)null; // this cast is NOT necessary
Console.WriteLine (nullableInt32);
output:
20130120
null
This is illegal because obamaTermTwo is not a nullable type:
DateTime obamaTermTwo = new DateTime(2013,1,20);
Console.WriteLine (obamaTermTwo);
obamaTermTwo = (DateTime)null; // illegal
// compile-time error message:
// Cannot execute text selection:
// Cannot convert null to 'System.DateTime'
// because it is a non-nullable value type
this works but is not necessary:
DateTime? obamaTermTwo = new DateTime(2013,1,20);
Console.WriteLine (obamaTermTwo);
obamaTermTwo = (DateTime?)null; // the cast is not required
Console.WriteLine (obamaTermTwo);
the above assignment of null is equivalent to this:
obamaTermTwo = null; // a cast is not required
output:
2013-01-20 00:00:00
null
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
void Main()
{
Foo foo = new Foo();
Console.WriteLine (foo);
foo = (Foo)null; Console.WriteLine (foo);
}
class Foo
{
}
output:
Foo
// Foo is not even null ... Foo is unitialized.
null
regards ~~ gerry
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
ToughMan
Participant
1490 Points
635 Posts
Why do a cast convert directly?
Jan 22, 2013 06:58 AM|LINK
I reflected the codes of "Text" property for a TextBox control in web application, here's it:
[Editor("System.ComponentModel.Design.MultilineStringEditor,System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)), Localizable(true), Bindable(true, BindingDirection.TwoWay), WebCategory("Appearance"), DefaultValue(""), WebSysDescription("TextBox_Text"), PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)] public virtual string Text { get { string str = (string) this.ViewState["Text"]; if (str != null) { return str; } return string.Empty; } set { this.ViewState["Text"] = value; }My question is so simple:
Plz have a look at my bold,underlined statement, this will avoid "null" value.
But if ViewState["Text"] contains a Null value, how it can be directly cast converted to string?
Why Microsoft doesn't use this instead:
public virtual string Text { get { string str = this.ViewState["Text"] as string; if (str != null) { return str; } return string.Empty; } set { this.ViewState["Text"] = value; }karthicks
All-Star
31378 Points
5422 Posts
Re: Why do a cast convert directly?
Jan 22, 2013 07:18 AM|LINK
hi, as opeartor returns null if the conversion failed instead of raising exception, so in next statement they are checking for != null
Refer : http://msdn.microsoft.com/en-us/library/cscsdfbt.aspx
Karthick S
ToughMan
Participant
1490 Points
635 Posts
Re: Why do a cast convert directly?
Jan 22, 2013 07:28 AM|LINK
Humm……U misunderstand what I mean.
I mean if ViewState["Text"] is Null, how can you convert this through:
string s = (string)ViewState["Text"]?
But to be very honest, I later test this statement below in a Console App:
string s = (string)null;
It runs OK? But where can I find the exclipit function defined in String class? \
guinnesslee
Participant
1050 Points
197 Posts
Re: Why do a cast convert directly?
Jan 22, 2013 09:38 AM|LINK
Both codes can work because you are typecasting the ViewState value and due to that you are to convert it in to string.
View state data is actually stored in one or more hidden fields as base64-encoded strings. You can access view state information using the page's ViewState property, which exposes a dictionary object. Because the data in view state is stored as a string, only objects that can be serialized can be stored.
twitter
www.mashupweb.wordpress.com
Paul Linton
Star
13421 Points
2535 Posts
Re: Why do a cast convert directly?
Jan 22, 2013 09:14 PM|LINK
Any reference type can be set to null.
Employee fred = null;
string name = null;
So you can cast null to any reference type
Employee fred = (Employee)null;
string name = (string)null;
The MS code may be considered slightly better because if something happened to ViewState["Text"] (either by a corruption or mailiciously) then the MS code would throw an exception when the cast failed but your code would silently return string.Empty with no indication that anything bad had happened.
ToughMan
Participant
1490 Points
635 Posts
Re: Why do a cast convert directly?
Jan 23, 2013 02:29 AM|LINK
Yes, but that's the problem……Don't u think so?
Null is a very special class that points to nothing. How can I turn it to a certain class?Is it meaningful?
Why don't we use:
SomeClass sc = null;
directly?
Why should we use:
SomeClass sc = (SomeClass)null;
And where's the exclipit cast convert function? Is that in "Object" class? Where?
Paul Linton
Star
13421 Points
2535 Posts
Re: Why do a cast convert directly?
Jan 23, 2013 02:34 AM|LINK
There is no explicit cast convert function because there is nothing to convert.
Using
SomeClass sc = null;
or
SomeClass sc = (SomeClass)null;
or
var sc = (SomeClass)null;
makes no difference. They all set the sc reference to be null (nothing) and declare it to be of type SomeClass.
ToughMan
Participant
1490 Points
635 Posts
Re: Why do a cast convert directly?
Jan 23, 2013 08:55 AM|LINK
Since nothing to convert, why should we say SomeClass w = (SomeClass)null?
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Why do a cast convert directly?
Jan 23, 2013 11:15 AM|LINK
@ ToughMan
(A) Paul's examples like the one above generally will not work will value types.
(B) Paul's examples will work for reference types. see next link http://forums.asp.net/post/5279308.aspx.
(C) Paul is simply trying to get across his point:
Examples:
for purpose of illustration, with a nullable value type:
output:
This is illegal because obamaTermTwo is not a nullable type:
this works but is not necessary:
the above assignment of null is equivalent to this:
output:
g.
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Why do a cast convert directly?
Jan 23, 2013 11:20 AM|LINK
@ Paul Linton
Paul, that is true for reference types ... please see http://forums.asp.net/post/5279300.aspx, above.
@ ToughMan ---- below, Foo is a reference type:
void Main() { Foo foo = new Foo(); Console.WriteLine (foo); foo = (Foo)null; Console.WriteLine (foo); } class Foo { }output:
regards ~~ gerry