I want to check whether string is empty or not
when i create object=Shared.getLocal("abc");
it assigns undefinded to the object at the first time
if(object.data.name=="undefnied") {
// is this correct
}
Use the hasOwnProperty function to test if the variable exists. For example:
if ( object.data.hasOwnProperty("name") ){
// Ok. object.data.name exists...
var value_of_name : String = String(object.data["name"]);
// Check for non-null, non-empty
if ( value_of_name ){
// Ok. It is a non-null, non-empty string
// ...
}
}
undefined is a value, not a string to compare to. You want:
if (object.data.name == undefined) {
//This property on your SharedObject was/is not defined.
}
Note that setting a property on a SharedObject to null does not delete it, it must be deleted with "delete".
I am not sure with flex, but it should be undefined or null without quotes, I think.
To answer your exact question (if is empty), I'd do this:
var name : String = object.data.name;
if(name != null && name.length > 0) {
//also, a common actionScript technique is to say if(name && name.length...)
//same difference.
}
Related
I receive the following error when I try to run my code. I haven't managed to solve it yet, please Help:
edit: Marked with * where it fails.
>
public IQueryable<Video> GetVideos([QueryString("id")] int? categorieId)
{
var _db = new TeleviziuneAcademicaAspSilverlight.Models.VideoContext();
IQueryable<Video> query = *_db.Videos;*
if (categorieId.HasValue && categorieId > 0)
{
query = query.Where(v => v.CategorieID == categorieId);
}
return query;
Change
IQueryable<Video> query =
to
IQueryable<Appname.Models.Video> query =
The reason for your error is that you have the type Video defined twice and because of using a short type name you accidentally reference not the one Video you should.
Also change it in the method's return value
public IQueryable<Appname.Models.Video> GetVideos( ... )
You seem to have two classes called Video. If you need both, you'll need to project from one to the other before your return statement:
return query.Select(dbVideo => new Appname.Video()
{
Prop1 = dbVideo.Prop1,
Prop2 = dbVideo.Prop2,
// etc.
});
Though you'll probably need to change the return type to an IEnumerable<> if you do that.
If you can just work with Appname.Models.Video, change IQueryable<Video> to IQueryable<Appname.Models.Video> in the method signature and the method body.
Good afternoon.
I need to check if the item already exists before add in an dataprovider. But the indexOf method always return -1 wheen i try to do this check in my array. Anyone who can help me? Thank you.
var contacts:Array = new Array();
for each(var i:Object in windowAddContact.selectedContacts) {
if(contacts.indexOf(i) == -1) {
contacts.push(i);
}
}
contactList.dataProvider = contacts;
Given the code; you've provided; I wouldn't expect indexOf to return anything but -1; unless youhave multiple instances of the same object in windowAddContact.selectedContacts; which is possible but seems unlikely.
The contacts array starts out empty and all this code does is copy items from a collection into an Array. Try this:
var contacts:Array = new Array();
contacts.add(windowAddContact.selectedContacts[0]);
for each(var i:Object in windowAddContact.selectedContacts) {
if(contacts.indexOf(i) == -1) {
contacts.push(i);
}
}
And I'll bet you'll see indexOf(i) return something other than -1 once. I'm unclear what you're trying to do, though. Why is the loop necessary? Can't you just do:
contactList.dataProvider = windowAddContact.selectedContacts;
if(oDataTable.Rows[0]["File"] != null) //byte array
{
}
File column in my table seems null. However when I try to check if it is null or not in the code part, it doesn't go to else part. Can someone tell me how to fix this?
I think you should check for DBNull with Convert.IsDBNull:
if(!Convert.IsDBNull(oDataTable.Rows[0]["File"]))
{
}
Hi i would like to check if a property exists?
This is my code
string abpath=null;
var hc= HttpContext.Current.Request.UrlReferrer;
if (hc.AbsolutePath !=null)
{
var _temp = HttpContext.Current.Request.UrlReferrer.AbsolutePath;
abpath = _temp.ToString();
}
I would like to Find out if AbsolutePath exists
can any one help me how to check.
right now it throws error as AbsolutePath doesn't exists to check
Thanks for your time
First you have to map the absolute path (url) to a local file system path. Then you can check whether the file exists:
var localPath = Server.MapPath(hc.AbsolutePath);
var exists = System.IO.File.Exists(localPath);
Update:
I guess I misunderstood the question. The problem is, that if your page/action/etc is called directly (e.g. by entering its URL in the browser), then there is no Referrer (previous page). So you have to first check Request.UrlReferrer for null:
if (hc != null && hc.AbsolutePath != null)
{
// ...
}
BTW: since AbsolutePath is already a string, there is no need to call ToString()on it. So you can simplify your code some more:
if (hc != null)
{
abpath = hc.AbsolutePath;
}
Check for url referrer first.
So try using code as below,
string abpath=null;
var hc= HttpContext.Current.Request.UrlReferrer;
if (hc !=null && !string.isNullOrEmpty(hc.AbsolutePath))
{
var _temp = HttpContext.Current.Request.UrlReferrer.AbsolutePath;
abpath = _temp.ToString();
}
can someone tell me how I can identify the type of an object in flex? In particular I have an array where I store multiple types in (but all UIComponents) now as I evaluate the array I want to find out whether I have a TextInput Control or a RadioButton. Does someone have an idea?
Thanks in advance
You can either test against a particular class using the "is" operator or you can use flash.utils.getQualifiedClassName() (you pass it your object) - which will return a string of the fully qualified class name.
ITS WORKING :)
Following is the way I solved this issue
switch( true )
{
case item is Customer_SF:
c = item as Customer_SF;
break;
case item is Opportunity:
o = item as Opportunity;
break;
case item is Product:
o = ( item as Product )._opportunity;
break;
default:
return true;
}
Try to use the "className" property.
It should return "TextInput" or "Button" depending the case
for each (var item:* in myArray)
{
if(item.hasProperty('className'))
{
trace("item ["+i+"] is :" + item['className']);
}
}
The operator "is" represents one option.
Then there is the operator instanceof, which might or might not be useful depending on situation.
Also there's the ObjectUtil class with static method getClassInfo. This one returns more than just the object's class name.
Operator "typeof" unfortunately is useless for classes.
And, as Branden Hall already suggested, flash.utils.getQualifiedClassName().
here's some simple pseudo-code which demonstrates how to use the is operator for what you want to do:
for each (var item:* in myArray)
{
if (item is TextInput)
doSomethingWithTextInput(item as TextInput);
else if (item is RadioButton)
doSomethingWithRadioButton(item as RadioButton);
}
The is operator tests for type compatibility, yes. From the docs, is:
... evaluates whether an object is
compatible with a specific data type,
class, or interface. Use the is
operator instead of the instanceof
operator for type comparisons. You can
also use the is operator to check
whether an object implements an
interface.
Other useful operators in this category are typeof (which returns a string representation of a primitive), instanceof (similar to is, but disregards interface compatibility) and as. A great and complete list of ActionScript operators is available here.
Hope it helps!
Your best bet is using the "is" operator and use something like:
for( var i:int = 0; i < componentArr.length; i++ )
{
var comp:UIComponent = componentArr[ i ];
if( comp is DataGrid )
// Handle DataGrid functionality here.
else if (comp is DropDown )
// Handle DropDown here
}
There is one problem with this approach, however. Because "is" will return true for all descendant classes, you must put all of the descendant classes before their ancestors -- List must come before ListBase. This can cause some annoyances.
// This is important to remember:
var mc:MovieClip = new MovieClip();
trace( mc is Sprite ); // true
There is one other option for cases where you want objects to be a member of a specific class (and not a descendant class): you can use the constructor property of the object and use a switch statement.
for( var i:int = 0; i < componentArr.length; i++ )
{
var klass:Class = componentArr[ i ].constructor;
switch( klass )
{
case DataGrid:
// Handle DataGrid
break;
case Text:
// Handle Text
break;
case NumericStepper:
// Handle NumericStepper
break;
default:
// Handle default
break;
}
}