casting

C# inheritance casting one child to another

I have this simple structure: 1 parent, and two different childs. public class Parent{} public class ChildA : Parent{} public class ChildB : Parent{} I have an object objA of type ChildA, which I want to cast to ChildB. My naive approach says: ChildA objA = new ChildA(); ChildB objB = (ChildB)objA; But this is not directly possi...

Why is there a performance warning on cast pointer to bool?

Extends. I thought I was being cool when I did something like: bool hasParent() { return this->parentNode ; } Even with a (bool) cast, the warning still doesn't go away. Where this->parentNode is NULL when there is no parent node. But I'm getting: warning C4800: 'Node *' : forcing value to bool 'true' or 'false' (performance w...

Preventing cast errors

I would like to correctly cast a value in a grid to an integer. The problem is that the value in the grid can sometimes be equal to an empty string which will cause a cast error. Is there a sophisticated way to try the cast without erroring on non-numeric values, or should I just do check beforehand? The cast code below is the code which...

Is it safe to cast generics in Delphi?

I need to implement a function which returns a TDictionary, without specifying the exact types. The returned value could be a TDictionary<string,Integer>, TDictionary<string,string> or TDictionary<string,Boolean> Could I declare the function with TDictionary as result parameter: function GetMap: TDictionary; and then cast the return ...

How does Casting work in PHP?

What doesn't this work: (int)08 == (int)09==0 But this and this does? (int)07==7 (int)06==6 ...

Exception: Specified cast is not valid

public class UserLoginInfo { public UserRole Role; public string Username; public static UserLoginInfo FetchUser(string username, string password) { using (var connection = Utils.Database.GetConnection()) using (var command = new SqlCommand("SELECT [Username], [Password], [Role] FROM [Users] WHERE [Userna...

portable signed/unsigned byte cast,C++

I am using signed to unsigned byte(int8_t) cast to pack byts. uint32_t(uint8_t(byte)) << n This works using GCC on Intel Linux. Is that portable for other platforms/compilers, for example PowerPC? is there a better way to do it? using bitset is not possible in my case. I am using stdint via boost ...

casting via void* instead of using reinterpret_cast

Hi, I'm reading a book and I found that reinterpret_cast should not be used directly, but rather casting to void* in combination with static_cast: T1 * p1=... void *pv=p1; T2 * p2= static_cast<T2*>(pv); instead of: T1 * p1=... T2 * p2= reinterpret_cast<T2*>(p1); However, I can't find an explanation why is this better than the dire...

why unsigned int 0xFFFFFFFF is equal to int -1?

perhaps it's a very stupid question but I'm having a hard time figuring this out =) in C or C++ it is said that the maximum number a size_t (an unsigned int data type) can hold is the same as casting -1 to that data type. for example see http://stackoverflow.com/questions/1420982/invalid-value-for-sizet Why?? I'm confused.. I mean, (t...

Is it possible to block/deny a cast conversion in Java?

I have the code of a simple game, where an AgentInterface must be implemented in order to create an agent controller for one of the characters in the game. GameState is a class the implements GameStateInterface, and an object that implements this interface can be passed to the agent, so the agent can read and analyze the data from game s...

Casing an Array with Numeric Keys as an Object

I was poking around PHPs casting mechanism, and ran into an odd case when casting an array as an object $o = (object) array('1'=>'/foo/bar'); $o = new stdClass(); var_dump($o); As I understand it, PHP properties need to be declared with the same rules as PHP variables. That is A valid variable name starts with a letter or underscore...

NULL pointer compatibility with static_cast

Q1. Why does using NULL pointers with static_cast cause crashes while dynamic_cast and reinterpret_cast give a NULL pointer in return? The problem occurred in a method similar to the one given below: void A::SetEntity(B* pEntity, int iMyEntityType) { switch (iMyEntityType) { case ENTITY1: { Set1(static_cast<C...

C++/CLI : Casting from unmanaged enum to managed enum

What is the correct way of casting (in C++/CLI) from a native code enum to a managed code enum which contain the same enum values? Is there any difference with using the C# way of casting like for example (int) in C++/CLI. ...

Difference between casting in C# and VB.Net

The next code works fine in C#: Int32 a, b; Int16 c; a = 0x7FFFFFFF; b = a & 0xFFFF; c = (Int16)b; But this code crash with a OverflowException in VB.Net. Dim a, b As Int32 Dim c As Int16 a = &H7FFFFFFF b = a And &HFFFF c = CType(b, Int16) Both codes seems the same to me. What is the differ...

Inherit from a template parameter and upcasting back in c++

Hello, I have tried to use this code in VS2008 (and may have included too much context in the sample...): class Base { public: void Prepare() { Init(); CreateSelectStatement(); // then open a recordset } void GetNext() { /* retrieve next record */ } private: virtual void Init() = 0; virtu...

F# Unit of Measure, Casting without losing the measure type

Is there built in version of the type casting functions that preserves units and if not how would I make them? So for example with this code how would I cast intWithSecondsMeasure to a float without losing the measure or multiplying by 1.0<s>? [<Measure>] type s let intWithSecondsMeasure = 1<s> let justAFloat = float intWithSecondsMeasu...

Haskell: How to type cast

C#: static int F(object x) { return x is string ? 1 : 2; } Haskell? The tricky bit seems to me that Haskell does not have a root type object. Edited: I do not care about converting to string. I want to know how to typecast (for example to see if an object is a Customer or an Order. ...

f# casting and generics

Trying to learn some f# and I've run into a couple of hangups. Here's the code: #light module HtmlModule type HtmlString(text:string) = override x.ToString() = text type HtmlAttribute(key:string, value:string) = inherit HtmlString(key + "=\"" + value + "\""); type HtmlElement(tag: string, ?contents:list<'a> when 'a :> HtmlSt...

Implicit VB performance question

Sometimes I have to implement an interface or inherit a virtual (MustInherit) that the base method expects an object, whilst I know that the value I will be passing will always be an Integer for example. What should be the best performance from the examples below: Public Sub DoSomething(ByVal obj As Object) 'option 1: Dim x As ...

make datatypes of different frameworks compatible

Hi! use in a project two libraries. Irrlicht and OpenMesh. Both libraries feature a Vector3 format. Irrlicht has vector3df which stores three floats and OpenMesh has VectorT OpenMesh has got some functions which return a VectorT - i know that N is 3 and the scalar is of type float. How can I make the types compatible such that i can c...