casting

How to use the CAST function correctly in a MySql SELECT statement?

I'm converting an MSSQL DB to MySQL DB and I have a stored procedure that is using a cast function to convert from a datetime datatype to a varchar datatype. Whether or not this matters in php/mysql since php isn't strongly typed (and I dont know if it would matter or not) I really want to keep the SP a close to the orginal as possible s...

C# Casting and Generics

Hello all, I've been struggling with a piece of C# code and although I have found a solution to the problem, it is by no means ideal (see DoSomething_WorksButNotIdeal() below). What I would like to do is instead of having the if, else statement (which is potentially massive depending on what types I want to support) just have a generic...

C++ Template for safe integer casts

I am trying to write a C++ template function that will throw a runtime exception on integer overflow in casts between different integral types, with different widths, and possible signed/unsigned mismatch. For these purposes I'm not concerned with casting from floating-point types to integral types, nor other object-to-object conversions...

Casting to Enum

I have the following code. private Enum MyEnum { VALUE1=5, VALUE2=4, VALUE3=3, VALUE4=2, VALUE5=1 } protected void Page_Load(object sender, EventArgs e) { Session["EnumValue"] = "VALUE1"; MyEnum test = (MyEnum) Session["EnumValue"]; } In the page load, after the casting i have the value of the variable 'test' = 'VALUE2'. ...

Most significant 32 bits lost when casting pointer to int64_t

Can someone explain the following strange behavior? I run the following program on a 64-bit Intel platform: include <stdio.h> #include <stdint.h> int main(void) { int x; int *ptr = &x; printf("ptr = %p\n", ptr); printf("sizeof(ptr) = %d\n", sizeof(ptr)); int64_t i1 = (int64_t) ptr; printf("i1 = 0x%x\n", i1); printf("si...

WCF DataContract Upcasting

I'm trying to take a datacontract object that I received on the server, do some manipulation on it and then return an upcasted version of it however it doesn't seem to be working. I can get it to work by using the KnownType or ServiceKnownType attributes, but I don't want to roundtrip all of the data. Below is an example: [DataContrac...

Is it possible in C# to overload a generic cast operator in the following way?

Hi, Just wondering if there is anyway to represent the following code in C# 3.5: public struct Foo<T> { public Foo(T item) { this.Item = item; } public T Item { get; set; } public static explicit operator Foo<U> ( Foo<T> a ) where U : T { return new Foo<U>((U)a.Item) } } Thanks ...

Polymorphism Not Working Like I Thought It Would?

I have a superclass that handles car decal information. I have a subclass that handles specific decal information for a transaction. I have a special override in the transaction decal that is used to check for certain things before a decal # can be set. The problem I'm having is that sometimes I need to grab information about a generi...

c# how to convert Int/Decimal to float

This is one of those silly intro questions, but how does one convert from an int or a decimal to a float in C#. I need to use a float for a third-party control but I don't use them in my code and I'm not sure how to end up with a float. ...

Best practices for getting around DB differences with an IDataReader

I have a simple value object which I populate with data from an IDataReader (could be the result of a query to either an Oracle or MS SQL database). The factory method for this object looks something like the following: internal static SomeClass CreateFromDataReader(IDataReader data) { string message = data.GetString(0); short i...

How can I get away from casting in C++?

In C++ you can cast a couple ways, C-style casting or C++ casts. Bjarne Stroustrup and a host of other C++ experts say that a good design should have no casting. Can you help me out here with redesigning the code below to get rid of the cast? void CProgressBar::SetPosition( int nPos ); //unable to change void CSaveDialog::UpdatePosit...

A better CDate for VB6

We have a a VB6 app (in a COM component) which uses CDate() to take a string and cast it to a Date, for storing in a database. Depending on if we want the application to talk in dd/MM/yy or MM/dd/yy for example, we have to change the regional settings of the identity user for the COM application. (Right now the only option we have is a...

C# explicit cast string to enum

Hi, I would like to have an explicit cast between from a string to an enum in c# in order to have this : (MyEnum) Enum.Parse(typeof(MyEnum),stringValue) I would like to deport this into an explicit cast operator, I did this but didn't work : public static explicit operator (MyEnum)(value stringValue){ return (MyEnum) Enum.Pars...

MyObject may not respond to message '-Foo'. (Noob) Objective-C question.

Yet another C#/.NET guy trying to learn Objective-C with big dreams of making millions with an iPhone app :) Ok, I'm sure this question stems from me being so used to static typed languages, and therefore am having a tough time adjusting, but here's my issue. Let's assume I have a class called MyObect: MyObject.h @interface MyObject :...

Does casting an Object in C# always return a Reference to the intial object

I'm currently doing a project in C# working with windows forms. During the course of it, I did the following void HideButtons(object sender, EventArgs e) { Button hider = ((Button)sender); foreach(Button tohide in hider.Parent.Controls) tohide.Hide(); hider.Show(); hider.Text = "U...

Casting as sub-class in Actionscript

I have the following classes in ActionScript: public class A { } public class B extends A { } and these variables (in another class): public var InstanceOfA:A; public var InstanceOfB:B; How do I cast an instance of A as a class B? I tried: InstanceOfA = new A(); InstanceOfB = InstanceOfA as B; trace(InstanceOfB); I get an obj...

Can you pass a "type" as an argument?

I want to do something like the following in VB.NET, is it possible? Function task(value as Object, toType as Type) Return DirectCast(value, toType) End Function ...

Why is casting faster then reflection in .NET?

I have an event handler that needs to determine a type and execute code if it matches a specific type. Originally we cast it to an object and if it wasn't null we executed the code, to speed it up I used reflection and it actually slowed it down and I don't understand why. here is a code sample Trace.Write("Starting using Reflection"...

Why can't I pull a ushort from a System.Object and then cast it as a uint? (C#)

I'm manipulating the items in a list that's a System.Management.ManagementObjectCollection. Each of these items is a System.Management.ManagementObject which contains properties indexed by string. See: foreach (ManagementObject queryObj in searcher.Get()) { string osversion = (string)queryObj["Version"]; string os = (string)quer...

Question about Java polymorphism and casting

I have a class C. Class E extends it. E e = new E(); C c = new C(); Why is e = (E) c; Upon further review: though numeric conversions have the same syntax as casting objects, some confusion arose. At any event, the above does not give a compilation, but rather a runtime error - so a class can be casted to subclass in some instances...