cast

[Java] Type safety: Unchecked cast from Object

Hi, I try to cast an object to my Action class, but it results in a warning: Type safety: Unchecked cast from Object to Action<ClientInterface> Action<ClientInterface> action = null; try { Object o = c.newInstance(); if (o instanceof Action<?>) { action = (Action<ClientInterface>) o; } else { // TODO 2 Auto-generated catch bloc...

Is this fix to "PostSharp complains about CA1800:DoNotCastUnnecessarily" the best one?

This question is about "is" and "as" in casting and about CA1800 PostSharp rule. I want to know if the solution I thought is the best one possible or if it have any problem that I can't see. I have this code (named OriginaL Code and reduced to the minimum relevant). The function ValidateSubscriptionLicenceProducts try to validate a Sub...

How can we cast Frame.Content to a Page type in WPF

Hi; Basically; I need to invoke a method on my WPF page, from a WPF frame where I load the WPF Page. I get the loaded page using Frame.Content property, and cast it to my page type since Content property returns Object type. The project builds successfully, however it throws InvalidCastException at runtime. //This line throws InvalidCas...

Casting between classes that share the same interface

I have two interfaces IHeaderRow, and IDetailRow I then have an object that implements both RawRow:IHeaderRow, IDetailRow I then need to cast it to HeaderRow which implements IHeaderRow. But when I try, it ends up being null or giving an exception. I can cast ObjectRawRow to either interface IHeaderRow, or IDetailRow var ObjectIHead...

Why does this cast to Base class in virtual function give a segmentation fault?

I want to print out a derived class using the operator<<. When I print the derived class, I want to first print its base and then its own content. But I ran into some trouble (see segfault below): class Base { public: friend std::ostream& operator<<(std::ostream&, const Base&); virtual void Print(std::ostream& out) const { ou...

C#'s equivalent to VB.Net's DirectCast?

This has probably been asked before, but if it has, I can't find it. Does C# have an equivalent to VB.Net's DirectCast? I am aware that it has () casts and the 'as' keyword, but those line up to CType and TryCast. To be clear, these keywords do the following; CType/() casts: If it is already the correct type, cast it, otherwise look f...

From varchar(36) to UNIQUEIDENTIFIER

I am trying to cast an AuctionId that is a UNIQUEIDENTIFIER to an varchar(36) and then back to an UNIQUEIDENTIFIER. Please help me. CAST((SUBSTRING(CAST([AuctionId] as VARCHAR(36)), 0, 35) + '1') AS UNIQUEIDENTIFIER) But I keep getting this error: Msg 8169, Level 16, State 2, Line 647 Conversion failed when converting from a c...

Iphone UITextField only integer

I have a UITextField in my IB and I want to check out if the user entered only numbers (no char)and get the integer value. I get the integer value of the UITextField like that : int integer = [myUITexrtField.text intValue]; When I put a character ( , ; . ) it return me 0 and I don't know how to detect that it is not only numbers. Ho...

How to cast C struct just another struct type if their memory size are equal?

I have 2 matrix structs means equal data but have different form like these: // Matrix type 1. typedef float Scalar; typedef struct { Scalar e[4]; } Vector; typedef struct { Vector e[4]; } Matrix; // Matrix type 2 (you may know this if you're iPhone developer) // Defines CGFloat as float for simple description. typedef float CGFloat; s...

IEnumerable.Cast<>

If I can implicitly cast an integer value to a double (and vice versa), like: int a = 4; double b = a; // now b holds 4.0 Why can I not do this: int[] intNumbers = {10, 6, 1, 9}; double[] doubleNumbers2 = intNumbers.Cast<double>().ToArray(); I get a "Specified cast is not valid" InvalidCastException exception. Doing the op...

C#: how to construct strings

Which of these will achieve the correct result: (1) int X = 23; string str = "HELLO" + X.ToString() + "WORLD"; (2) int X = 23; string str = "HELLO" + X + "WORLD"; (3) int X = 23; string str = "HELLO" + (string)X + "WORLD"; EDIT: The 'correct' result is for str to evaluate to: HELLO23WORLD ...

Casting a container of shared_ptr

Hi all, I have a method void foo(list<shared_ptr<Base>>& myList); Which I'm trying to call with a two different types of lists, one of DerivedClass1 and one of DerivedClass2 list<shared_ptr<DerivedClass1>> myList1; foo(myList1); list<shared_ptr<DerivedClass2>> myList2; foo(myList2); However this obviously generates a compiler ...

Service Broker message_body error when casting binary data to xml in C#

I am using Message Broker with Sql server 2008, and designing an External Activator service to consume messages from my target queue. My Problem: Cant cast the returned message body from the SqlDataReader object: "WAITFOR (RECEIVE TOP(1) conversation_handle, message_type_name, message_body FROM [{1}]), TIMEOUT {2}" operation, I cant c...

template; operator (int)

Hi, regarding my Point struct already mentioned here: http://stackoverflow.com/questions/2794369/template-class-ctor-against-function-new-c-standard is there a chance to replace the function toint() with a cast-operator (int)? namespace point { template < unsigned int dims, typename T > struct Point { T X[ dims ]; //umm??? ...

Android : Cannot cast from View to Button

I just started working with the Android, but seem to have come across a problem that I simply can't find the answer to. I get the error "Cannot cast from View to Button" on this line : Button myButton = (Button)findViewById(R.id.my_button); I've tried many different things to get it going, and I've searched for the answer, but for so...

Android: Cannot cast from View to MapView

I'm trying to run the Android MapView example, and am getting a 'Cannot cast from View to MapView' error in Eclipse. My layout is as follows <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mainlayout" android:orientation="vertical" android:layout_width=...

Correctly assigning value to a Core Data attribute with an integer data-type

I'm missing something here, and feeling like an idiot about it. I'm using a UIPickerView in my app, and I need to assign the row number to a 32-bit integer attribute for a Core Data object. To do this, I am using this method: -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { ...

Cast LINQ Function Result to Domain Object

Hello, I have a table valued function to perform full text search on SQL server. The result type of my full text search function in LINQ is a special autogenerated type which includes KEY and RANK in addition to my regular domain object properties. So, if my regular domain object is PERSONS (with properties FirstName, LastName etc.), ...

Cast into new Object, Copy values by Property Name

Hello, I have the following: Class 1 (Text, State, Level) Class 2 (Text, State, Level, Ident) Is there a way for me to cast an object of Class 2 into into Class 1, and not having to do the usual cast code (Text = c.Text, State = c.State etc.)? Possibly by identifying the property names of each class and copying the value over? ...

What OOP pattern to use when only adding new methods, not data?

Hello eveyone... In my app, I have deal with several different "parameters", which derive from IParameter interface, and also ParamBase abstract base class. I currently have two different parameter types, call them FooParameter and BarParameter, which both derive from ParamBase. Obviously, I can treat them both as IParameters when I ne...