I am getting this exception "Object must implement IConvertible." while converting rules to tolist(). below is my code
var rules = from m in db.Rules select m;
return rules.ToList().ToDataTable(); // exception occurs here
I am using MySQL 6.3.6 ..the same code is working fine with MSSQL.
I will be grateful if someone helps me in this
regards
Umair
Make sure the source type is convertible to the destination type.
Probably the rules.ToList() don't match with you ToDataTable destination cast.
Can you verify what var list = rules.ToList() contains?
And... I suggest deal with List and IEnumerable against Datatable.
Related
I have seen quite a bunch of codes casting objects from one type to another type, using what I call the "standard" casting, like this:
var myDO:DisplayObject = loader.content;
var myCastedMC:MovieClip = MovieClip(myDO);
However the as operator seems to work the same way, because when I traced both objects I get the same value:
var myAsMC:MovieClip = myDO as MovieClip;
trace(myAsMC,myCastedMC); //both outputs read [object MainTimeline]
So, what is the difference between these two? When do yo use the as operator and when do you use the "standard" casting?
You cast only when you are certain the cast will succeed. If casting fails a runtime error is thrown.
You use 'as' to produce a soft cast that will never throw an error. In that case either the cast succeed or the default value of the datatype is returned (for most object that is null).
Both casts are meant to be used in very different situations but since they are misunderstood often you will see 'as' being used when the coder really meant a direct cast.
If following a cast the coder will not check or need to check if the cast has succeeded then it should have used a direct cast. If following a cast the coder needs to check if the cast has succeeded, he should use 'as'.
It should be explained why hitman answer is not correct. The provided code assume success:
(getChildAt(i) as TextField).text=i.toString();
Meaning coder knows the display list only contains TextField object (or else an error will occur). In that case a direct cast is recommended:
TextField(getChildAt(i)).text = i.toString();
If the display list contains other object types then 'as' can be used:
var field:TextField = getChildAt(i) as TextField;
if(field)//if null then getChildAt(i) is not a TextField
{
//field exist so cast succeeded
}
Good question!
on of their differences is that casting is conversion at compile and "as" is converting at runtime.and one of others is that you can only convert subclasses to superclasses.but "as" converts anything.
let's have an example:
let's say we have a lot of textfields on stage and we don't have them in variables or an array.and we want to change their text by a loop:
for(var i:uint=0;i<numChildren;i++){
var t:TextField=getChildAt(i)
t.text=i.toString();
}
probably you see an error because getChildAtreturns displayObject and displayObject doesn't have text property!
so you must use as here:
for(var i:uint=0;i<numChildren;i++){
(getChildAt(i) as TextField).text=i.toString();
}
H☻pes this helps!
I'm learning Dart and was reading the article Using Dart with JSON Web Services, which told me that I could get help with type checking when converting my objects to and from JSON. I used their code snippet but ended up with compiler warnings. I found another Stack Overflow question which discussed the same problem, and the answer was to use the #proxy annotation and implement noSuchMethod. Here's my attempt:
abstract class Language {
String language;
List targets;
Map website;
}
#proxy
class LanguageImpl extends JsonObject implements Language {
LanguageImpl();
factory LanguageImpl.fromJsonString(string) {
return new JsonObject.fromJsonString(string, new LanguageImpl());
}
noSuchMethod(i) => super.noSuchMethod(i);
}
I don't know if the noSuchMethod implementation is correct, and #proxy seems redundant now. Regardless, the code doesn't do what I want. If I run
var lang1 = new LanguageImpl.fromJsonString('{"language":"Dart"}');
print(JSON.encode(lang1));
print(lang1.language);
print(lang1.language + "!");
var lang2 = new LanguageImpl.fromJsonString('{"language":13.37000}');
print(JSON.encode(lang2));
print(lang2.language);
print(lang2.language + "!");
I get the output
{"language":"Dart"}
Dart
Dart!
{"language":13.37}
13.37
type 'String' is not a subtype of type 'num' of 'other'.
and then a stacktrace. Hence, although the readability is a little bit better (one of the goals of the article), the strong typing promised by the article doesn't work and the code might or might not crash, depending on the input.
What am I doing wrong?
The article mentions static types in one paragraph but JsonObject has nothing to do with static types.
What you get from JsonObject is that you don't need Map access syntax.
Instead of someMap['language'] = value; you can write someObj.language = value; and you get the fields in the autocomplete list, but Dart is not able to do any type checking neither when you assign a value to a field of the object (someObj.language = value;) nor when you use fromJsonString() (as mentioned because of noSuchMethod/#proxy).
I assume that you want an exception to be thrown on this line:
var lang2 = new LanguageImpl.fromJsonString('{"language":13.37000}');
because 13.37 is not a String. In order for JsonObject to do this it would need to use mirrors to determine the type of the field and manually do a type check. This is possible, but it would add to the dart2js output size.
So barring that, I think that throwing a type error when reading the field is reasonable, and you might have just found a bug-worthy issue here. Since noSuchMethod is being used to implement an abstract method, the runtime can actually do a type check on the arguments and return values. It appears from your example that it's not. Care to file a bug?
If this was addressed, then JsonObject could immediate read a field after setting it to cause a type check when decoding without mirrors, and it could do that check in an assert() so that it's only done in checked mode. I think that would be a nice solution.
I have a base class called Room and a subclass called Attic, and another called Basement.
I have a controller class that has an attribute called CurrentLocation which is type Room. The idea is I want to be able to put Attic or Basement in that property and get it back, then cast that to whatever type it is.
So if on the controller the content is of type Attic, I'm trying to figure out how to explicitly cast it. I thought I knew but its not working... Here's what I thought it would be, borrowing from Java:
var myAttic:Attic = (Attic) Controller.CurrentLocation;
This gives me a syntax error:
1086: Syntax error: expecting semicolon before instance.
So how do you cast implicitly? Or can you? I could swear I've done this before as as3.
Here are your options for casting in ActionScript 3:
Use as.
var myAttic:Attic = Controller.CurrentLocation as Attic; // Assignment.
(Controller.CurrentLocation as Attic).propertyOrMethod(); // In-line use.
This will assign null to myAttic if the cast fails.
Wrap in Type().
var myAttic:Attic = Attic(Controller.CurrentLocation); // Assignment.
Attic(Controller.CurrentLocation).propertyOrMethod(); // In-line use.
This throws a TypeError if the cast fails.
I am trying to switch from LINQ2SQL to EF ... I am getting the following error with some code that originally worked with LINQ2SQL and seems to compile correctly:
Csla.DataPortalException:
DataPortal.Fetch failed (LINQ to
Entities does not recognize the method
'MyApp.Logic.UserInfo
FetchUserInfo(MyApp.Data.User)'
method, and this method cannot be
translated into a store expression.)
---> Csla.Reflection.CallMethodException:
DataPortal_Fetch method call failed
---> System.NotSupportedException: LINQ to Entities does not recognize
the method 'MyApp.Logic.UserInfo
FetchUserInfo(MyApp.Data.User)'
method, and this method cannot be
translat...
This is the code:
var data = query.Select(row => UserInfo.FetchUserInfo(row));
this.AddRange(data);
I'm trying to read a list of data and load the entities into my class. I'm new to EF and just think I am overlooking something.
Any help would be appreciated!
For those interested, the solution was:
var data = query.AsEnumerable().Select(UserInfo.FetchUserInfo);
As far as I can see the problem is that Linq to Entities provider knows nothing about how to translate you custom method FetchUserInfo to ESQL.
If UserInfo is just a DTO and UserInfo.FetchUserInfo is a kind of Entity to DTO conversion method this would help
var data = query.AsEnumerable().Select(row => UserInfo.FetchUserInfo(row));
.AsEnumerable() invoke will result in materialization of query results to memory objects.
Does anyone know the real difference between the two ways of type casting in Flex 3?
var myObject1:MyObject = variable as MyObject;
var myObject2:MyObject = MyObject(variable);
I prefer to use the second method because it will throw an Error when type cast fails, whereas the first method will just return null. But are there any other differences? Perhaps any advantages to using the first method?
The second type of casting has different behaviour for top level(http://livedocs.adobe.com/flex/2/langref/) types, e.g. Array(obj) does not cast in the straightforward way you describe; it creates a new Array if possible from obj, even if obj is an Array.
I'm sure the times this would cause unexpected behaviour would be rare but I always use "as" for this reason. It means if I do
int(str)
I know it's a cast in the "attempt to convert" sense of the word not in the "I promise it is" sense.
ref: got some confirmation on this from http://raghuonflex.wordpress.com/2007/07/27/casting-vs-the-as-operator/
The as method returns null if cast fails.
The () method throws and error if the cast fails.
If the value of variable is not compatible with MyObject, myObject1 will contain null and you will be surprised by a null pointer error (1009 : cannot access a property or method of a null object reference.) somewhere later in the program when you try to access it. Where as if you are casting using the MyObject(variable) syntax, you will get a type coercion error (1034 : Type Coercion failed: cannot convert _ to _) at the same line itself - which is more helpful than getting a 1009 somewhere later and wondering what went wrong.
I think I read somewhere on this site that as is slighty faster than (), but I can't find the question again.
Beside that this question have been asked many times, you will find an more in-depth answer here.
I recently discovered the very useful [] tag when searching on StackOverflow, it allows to only search in questions tagged with the specified tag(s). So you could do a search like [actionscript-3] as vs cast. There are more search tips here: https://stackoverflow.com/search.
And no; the irony in that I can not find the question about performance and write about how to search is not lost on me ;)
I think as returns back the base class and not null when the casting fails and () throws an error.