casting

What are the differences between asInstanceOf[T] and (o: T) in scala ?

I saw that there is 2 methods to cast an object : foo.asInstanceOf[Bar] (foo: Bar) When I tried I found that as asInstanceOf don't use the implicit conversions whereas the other one do. So what are the differences of behavior between this 2 methods ? And where it's recommended to use one over the other ? Thank you. ...

How to cast a Dictionary into a CollectionDataContractAttribute decorated class

Hi, this is my first WCF service. I defined a response message that derives from a Dictionary like this: [CollectionDataContract(ItemName = "Product", KeyName = "ProductNumber", ValueName = "ProductName")] public class GetAvailableProductsResponse : Dictionary<string, string> { } When I try to run the following code in a service opera...

C type casting with "const" keyword

I usually use C type casting in C/C++ code. My question is, does adding the "const" keyword in the casting type mean anything to the result? For example, I can think up several scenarios: const my_struct *func1() { my_struct *my_ptr = new my_struct; // modify member variables return (const my_struct *)my_ptr; // return my...

System.InvalidCastException: in ASP.NET MVC partial views

In the controller: public ActionResult Index() { ViewData["page"] = 0; return View(data); } public ActionResult More(long page = 0) { ViewData["page"] = page; return View(data); } So, I have two views: Index.aspx and More.aspx. I created a partial view (PartialView.ascx) that is used in both of the views. Inside the p...

Is a type-safe respond_to in scala possible?

Using a case construct for type-safe casting is easily done in scala. The following code ensures that square gets only called on objects which have an according type. class O class A extends O { def square(i: Int):Int = { i*i } } class B extends O { def square(d: Double):Double = { d*d } } class C extends O {} def square(o: O) ...

PHP function call not working - presumed casting problem?

Why does THIS not work when called via e.g. wd(1) (it always returns '-2')? $zoom=$user['zoom']; function wd($hrs){ return $hrs*$zoom-2; } But THIS works fine: function wd($hrs){ return $hrs*30-2; } Assuming this was a casting problem, I tried all sorts of variations like (int)$hrs * ((int)$zoom) or (int)$hrs * (float)$zoom...

Java generic cast by parameter type

Hi, what, if at all possible, would be a good solution to implement the following desired functionality where I want to: Cast the return type of a routine to the type of the routine parameter, e.g., // Foo and Bar are two types of Common interface Common{} interface Foo extends Common {} interface Bar extends Common {} // Examp...

Casting Eval("bitValue") as Bool

I have a list view with a HyperLink control within the ItemTemplate. I want to display the link if a returned value is 0 (false), and not display the link if it is 1 (true). So far I have this: <asp:HyperLink runat="server" ID="lnkReview" NavigateUrl='<%# Eval("EnquiryID", @"selectcompany.aspx?enq={0}")%>' Text="Review Enquiry" V...

overloaded cast operator or single argument constructor

If a class has a single argument constructor my understanding is it is implicitly convertible by the constructor to the type of the argument in appropriate contexts. Defining a conversion operator also makes a class convertible to another type. Questions Does the conversion operator ever get called implicitly? If both a single argume...

Pointer casting in C

Hi, static int write_stream(const void *buffer, size_t size, void *app_key) { char *stream = (char *)app_key; return 0; } what am I doing wrong here? Why pointer casting does not result in a copying of (size_t size) bytes from buffer into the stream defined by app_key? Thanks am calling this function as argument of anot...

How does foreach cast without explicit/implicit overrides?

So I'm just learning C#, and came across something that I find odd... I'm playing with delegates and have creates a delegate DelegateReturnsInt. Now, When I use a foreach loop, the book shows to use it like this: foreach(DelegateReturnsInt del in theDelegate.getInvocationList()) now I know that getInvocationList() returns an Array of ...

what C++ casting operator is equivalent to (MyStructType){1,2,3} ?

Is there an C++ casting operator (or combination thereof) equivalent to the old-style cast below: struct MyStruct { int i; int j; int k; }; void do_something_with_mystruct( MyStruct ms ) { ... }; int main( int argc, char** argv ) { do_something_with_mystruct( (MyStruct){1,2,3} ); }; ...

Java: Convert Primitive Class

Hi is there an easy in Java to convert primitive class objects into object class objects? Given a class Class cl, I want to convert it into a Class that has no primitives. Eg. Class<?> cl = int.class; ... if (cl.isPrimitive()) { cl = Object of primitive } ... cl == Integer.class I would like a method that does that for all primit...

Why ever cast reference types when you can use "as"?

Possible Duplicate: Casting: (NewType) vs. Object as NewType In C#, why ever cast reference types when you can use "as"? Casting can generate exceptions whereas "as" will evaulate to null if the casting fails. Wouldn't "as" be easier to use with reference types in all cases? eg: MyObject as DataGridView rather than, (...

Javascript Function instance is automatically casted to object and can't be called anymore

Hi, I'm working with a fairly complex Javascript program wich, at a given point, returns some nested anonymous functions. Sometimes, when I try to "apply" one of such anonymous functions ("f" in this example)... f.apply (this.context, args) ...I get an "f.apply is not a function" error. It's weird, because alert(f) displays the fun...

How to use id variable in objective-c?

I have an NSArray that contains two types of objects. Lets call them Apple and Orange. Apple *myApple = [self.searchQueryResults objectAtIndex:indexPath.row]; When I am building my cell's I don't know what type is in my array, apples or oranges. How can I use the generic id type to store the object, and then cast appropriately? ...

Quick question about casting basic return types

I find myself casting return types alot to silence compiler warnings and it always makes me feel like i'm doing something wrong. This example is Objective-c const char *strBuf = [anNString UTF8String]; [anOutputStream write:strBufr maxLength:len]; This goves me a compiler warning as -UTF8String returns const char * and -write:maxLe...

Pointer to the start of an object (C++)

Hello, I need a way to get a pointer to the start of an object in C++. This object is used inside a template so it can be any type (polymorphic or not) and could potentially be an object that uses multiple inheritance. I found this article which describes a way to do it (see the section called "Dynamic Casts") using typeid and a dynami...

Why do I need to cast before a method of an item of a NSArray can be called?

I am fairly new to Objective-C. Currently porting my own library from C#/Java to objective C. I now run into a very strange problem for me. I have a NSArray with several Note objects. I want to transpose on of these notes: //Note.h - (Note *) transpose: (int) semitones; //Main NSArray *notes = [get it from somewhere]; Note *tra...

Trouble with explicit casting

Hello. I have a product class in C# that inherits from another product class using ExternalAssemb.ProductA; public class MyProduct : ProductA { //.... } I'm trying to do an explicit cast from ProductA that is in a DLL I reference but it's telling me it's unable to cast MyProduct myProduct = (MyProduct)productAobject; Result::...