casting

Best way to determine how to cast an object to an appropriate type in C#?

I am currently developing an approval routing WCF service that will allow an user to create "rules" which determine how an request is routed. The route is determined by comparing the "ObjectToEvaluate" property of the Request class against the "ObjectToEvaluate" property of the "Rule" class. The "UnitOfMeasurement" enum determines how ...

Any way to cast with class operator only?

Kind of a random question... What I'm looking for is a way to express a cast operation which uses a defined operator of the class instance I'm casting from, and generates a compile-time error if there is not a defined cast operator for the type. So, for example, what I'm looking for is something like: template< typename RESULT_TYPE, ty...

Unable to cast object of type 'System.Object[]' to 'MyObject[]', what gives?

Scenario: I'm currently writing a layer to abstract 3 similar webservices into one useable class. Each webservice exposes a set of objects that share commonality. I have created a set of intermediary objects which exploit the commonality. However in my layer I need to convert between the web service objects and my objects. I've used re...

Type Casting in C++

May be the question will be a little bit noobie, but can anybody advice me some books/sites/articles about how and when it's better to use typecasting in C++ style and when it would be better to prefer C-style? I've read a few books (Shildt, Eckel) and haven't found good material on this theme though I think it's one of the strongest C+...

What's the best way to extract a one-dimensional array from a rectangular array in C#?

Say I have a rectangular string array (stringarray[rows, columns] - not a jagged array). What's the best way to extract a one-dimensional array from this (either a single row or a single column)? I can do this with a for loop, of course, but I'm hoping .NET has a more elegant way built in. Bonus points for converting the extracted str...

Fastest way to convert string to integer in PHP

Using PHP, what's the fastest way to convert a string like this: "123" to an integer? Why is that particular method the fastest? What happens if it gets unexpected input, such as "hello" or an array? ...

C++ casting programmatically : can it be done ?

Let's say I have a Base class and several Derived classes. Is there any way to cast an object to one of the derived classes without the need to write something like this : string typename = typeid(*object).name(); if(typename == "Derived1") { Derived1 *d1 = static_cast } else if(typename == "Derived2") { Derived2 *d2 = static_cas...

Using "ref" and/or "out" for Object type

I'm stuck with .Net 1.1 application (i.e. I can not use the generics goodies from 2.0 for now), and I was trying to optimize some parts of the code. As it deals a lot with runtime callable wrappers, which need to be released, I ended up to create a utility method which loops until all references are released. The signature of the method ...

Integer math in c#

I have a menu of product brands that I want to split over 4 columns. So if I have 39 brands, then I want the maximum item count for each column to be 10 (with a single gap in the last column. Here's how I'm calculating the item count for a column (using C#): int ItemCount = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(BrandCount) / ...

Casting in VB.Net

I would like to be able to cast a value dynamically where the type is known only on runtime. something like this myvalue = ctype (value, "String, Integer or Boolean") the string that contains the type value is passed as argument and also read from DB. And the value is stored as string in the DB. Is this possible ? Thanks in advance...

Determine value of object in C#

What would be the best way to determine if an object equals number zero (0) or string.empty in C#? EDIT: The object can equal any built-in System.Value type or reference type. Source Code: public void MyMethod(object input1, object input2) { bool result = false; object compare = new object(); if(input != null && input2 !=...

PHP array_intersect() - how does it handle different types?

If I've got an array of values that are basically zerofilled string representations of various numbers and another array of integers, will array_intersect() still match elements of different types? For example, would this work: $arrayOne = array('0003', '0004', '0005'); $arrayTwo = array(4, 5, 6); $intersect = array_intersect($arrayOn...

(int) ch vs. int(ch): Are they different syntaxes for the same thing?

In C++, is (int) ch equivalent to int(ch). If not, what's the difference? ...

How do I cast a bool to a BOOL ?

Am I safe in casting a C++ bool to a Windows API BOOL via this construct bool mybool = true; BOOL apiboolean = mybool ? TRUE : FALSE; I'd assume this is a yes because I don't see any obvious problems but I wanted to take a moment to ask only because this may be more subtle than it appears. Thanks to Dima for (gently) pointing out my...

Automatic casting to string in C# and VB.NET

I could do this in C#.. int number = 2; string str = "Hello " + number + " world"; ..and str ends up as "Hello 2 world". In VB.NET i could do this.. Dim number As Integer = 2 Dim str As String = "Hello " + number + " world" ..but I get an InvalidCastException "Conversion from string "Hello " to type 'Double' is not valid." I am a...

Data type support in ColdFusion querynew()

Does anyone know of a way to store values as NVARCHAR in a manually created query in ColdFusion using the querynew() function? I have multiple parts of a largish program relying on using a query as an input point to construct an excel worksheet (using Ben's POI) so it's somewhat important I can continue to use it as a query to avoid a r...

Casting problem in C# generic method

I'm having some trouble with a generic method I'm writing. It has the following signature; public static ThingCollection<T> GetThings<T>(...) where T : Thing There are several classes; ThingA, ThingB and ThingC that inherit from Thing; and I want to be able to have code something like this in the method. var things = new ThingCollec...

How do I convert a float to an int in Objective C?

Total newbie question but this is driving me mad! I try myInt = [myFloat integerValue]; and I get an error saying essentially integerValue doesn't work on floats. How do I do it? ...

C# casts differing between VS2008 and IIS6

I have a piece of C# code that add the values of an enum to a drop down list by type. It requires that it be called with T1 being an enum type, although I cannot specify this as a type constraint because enums are special case in which this isn't possible. This is not a major concern as this is only used internally and is documented. De...

Int to Char in C#

What is the best way to convert an Int value to the corresponding Char in Utf16, given that the Int is low enough? Boaz ...