Just to make this clear - what is the difference between:
String(value)
and
value as String
What are the cases where you would use one over the other? They seem interchangeable...
Casting with Type(variable) can cause a runtime exeception (RTE), while "variable as type" will return null instead of throwing an exception.
See http://raghuonflex.wordpress.com/2007/07/27/casting-vs-the-as-operator/ for more explanations.
String (value) creates a new String object from a string literal. If the constructor argument is not a string literal, I assume it calls the argument object's .toString() method.
value as String will simply pass back value IF value is a String or a subclass of String. It will pass back null if value is not of type String.
The important thing to note is that String(val) creates a new object whereas value as String simply refers to value (and tests for compatibility to String).
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/String.html
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/operators.html#as
Related
I have a page with a nested Gridview where I'm trying to fill the inner grid by pulling a string value from each line of the outer grid. When I try to pass the value from the outer grid to the string variable I get the "Error when converting 'String' to 'System.IFormatProvider'" error. I'm using the following code to store the gridview cell value to the variable:
Dim Svc_Name As String = grdOuterGridView.DataKeyNames(e.Row.RowIndex).ToString("THIRD_PARTY_SERVICE")
Can anyone tell me what I'm doing wrong here? Thanks.
The DataKeysNames is already an array of string, you don't need the ToString(....) part.
Dim Svc_Name As String = grdOuterGridView.DataKeyNames(e.Row.RowIndex)
Actually your error comes from the ToString("THIRD_PARTY_SERVICE"). The ToString() override that takes one parameter requires an object that implements IFormatProvider interface, but of course a string doesn't have this interface, thus the error. However, calling ToString() on a String has no effect as you can read from the MSDN docs
Returns this instance of String; no actual conversion is performed.
I've got a form with some display only fields in it. These fields are usually DateTime values... but when empty/null I would like to display the string "never".
EDIT:
To be more explicit: The field should show the DateTime value from the database and if null the string 'never' should be displayed.
How should I do that?
Thanks in advance
You can use Symfony2 Data Transformers :
In the transform() function you can check if your date is null and then return the 'never' string. Otherwise return a string representation of your date.
In the reverseTransform() function you can check if the string is 'never' and then construct a null DateTime object. Otherwise, you transform the given string into a valid DateTime object with something like 'strtotime()` PHP function.
Is it possible to cast a command-line passed string object back to actual object?
I want to do the following, but throwing error can't cast.
Button objPro = (Button) sender;
cProduct cp = (cProduct) objPro.CommandArgument;
If no, then why?
This is what the string holds.
cProduct cpObj = (cProduct)e.Row.DataItem;
Button btnAddProduct = (Button)e.Row.FindControl("btnAddProduct");
if (btnAddProduct != null)
{
btnAddProduct.CommandArgument = cpObj.ToString();
}
You probably can't, because it's a string. It's not a cProduct (whatever that is - consider following .NET naming conventions and naming it Product instead).
Now you could do this if you had a explicit conversion operator in cProduct to create an instance from a string.
You haven't really explained what's in the string, or what's in the type - but if your cProduct type provides a ToString method which contains all the data in a reversible form, then you could easily write a method or a constructor to create the product again:
Product product = new Product(objPro.CommandArgument);
or maybe:
Product product = Product.Parse(objPro.CommandArgument);
You'll have to write that constructor/method, of course.
I would strongly recommend using a constructor or method instead of an operator, just to keep your code clearer - it's very rarely a good idea to write your own conversion operators.
Take a look at CommandArgument on MSDN. The property is a string, when you assign the a value to the property, you aren't casting some complex type to string, you are setting a string value on the property. Can you cast a string back to your object type anyway, regardless of it being a CommandArgument. I doubt it. If the argument is an int you could try int.Parse or similar for other types which have a parse method.
I need to assign my variable to session. I tried this:
string name = string.Empty
Session["N"] = name;
and it won't work.
Error 1 Invalid token '[' in class, struct, or interface member declaration
Error 2 Invalid token '"N"' in class, struct, or interface member declaration
Error 3 Identifier expected
Where I'm wrong?
I'm using ASP.net in Visual Studio 2008.
Error 1 Invalid token '[' in class, struct, or interface member declaration
Error 2 Invalid token '"N"' in class, struct, or interface member declaration
Error 3 Identifier expected
missing ; end of first line.
string name = string.Empty;
Session["N"] = name;
string test= Session["N"].ToString();//Catch Your session
There is nothing wrong with that code (except a missing semicolon, as Shree Khanal pointed out, but that can't be the issue, right?).
As long as the code is in the page class, the Session property is available. If you have the code in a different class, you don't have the Session property available, then you need to get it from the current context:
HttpContext.Current.Session["N"] = name;
When reading the value from the session collection, the type is Object, not String, so you need to cast it:
string name = Session["N"] as string;
Using the as keyword means that you can attempt to read the value even if it would not exist, or if it happens to be set to a different data type. In that case you will get a null reference back.
I have a .Net Web Service function that can accept one string.
That function will then serialize that string to JSON, but I only want to serialize it if it's value is not "".
I found these instructions:
http://msdn.microsoft.com/en-us/library/aa347792.aspx
[DataContract]
public class MyClass
{
[DataMember (EmitDefaultValue=false)]
public string myValue = ""
}
Unfortunatelly I can not hide the myValue from the serialization because "" is not the .Net default value for a string (how dumb is that!)
One of two option ocurred
On the web service have some kind of attribute that sets the "" to null
Have some condition on the class
I would prefer the 1st because it makes the code cleaner but an opinion would be great.
Thanks
You can explicitly set what the default value is (for the purposes of serialization) using the DefaultValueAttribute class:
[DataContract]
public class MyClass
{
[DataMember (EmitDefaultValue=false)]
[DefaultValue("")]
public string myValue = ""
}
I think you have at least a couple of options here. It's extra work but worth it.
You can encapsulate the string in a reference type. Since reference types are null if not present, that lets you know right away if a string was present or not (because the encapsulating reference type would be either non-null or null, if the string is non-empty or not.)
A final option you have is to add an extra complementary variable (perhaps a boolean) that is set on OnDeserializing/OnDeserialized/OnSerializing/OnSerialized and use this to track whether or not something was actually present on the wire. You might, for example, set this complementary variable to true only when you're actually serializing out a non-empty string and similarly