casting

Why does Java implicitly (without cast) convert a `long` to a `float`?

Every time I think I understand about casting and conversions, I find another strange behavior. long l = 123456789L; float f = l; System.out.println(f); // outputs 1.23456792E8 Given that a long has greater bit-depth than a float, I would expect that an explicit cast would be required in order for this to compile. And not surprising...

Differences between VB TryCast and C# "as" operator when using LINQ

I have a LINQ query to retrieve the maximum value of an integer column. The column is defined as NOT NULL in the database. However, when using the MAX aggregate function in SQL you will get a NULL result if no rows are returned by the query. Here is a sample LINQ query I am using against the Northwind database to demonstrate what I am d...

How do I convert from int to long in Java?

I keep finding both on here and Google people having troubles going from long to int and not the other way around. Yet I'm sure I'm not the only one that has run into this scenario before going from int to Long. The only other answers I've found were "Just set it as Long in the first place" which really doesn't address the question. Ca...

reinterpret_cast to void* not working with function pointers

I want to reinterpret cast a function pointer into a void* variable. The type of the function pointer will be of type Class* (*)(void*). Below is the sample code, class Test { int a; }; int main() { Test* *p(void **a); void *f=reinterpret_cast<void*>(p); } The above code works well with Visual Studio/x86 compilers. B...

iPhone dev - How far down should I cast?

If I have a method that passes an argument of type void * (UIView animation did stop method, has to be a void pointer), or of type id, and I know that the argument is a UIBarButton item, and I need to disable it, [barbuttonitem setEnabled:NO];, should I cast the argument to a UIControl, which is as far as I need to be able to use setEnab...

Reliably convert a string to a double independent of regional settings in classic ASP?

I'm working on a legacy classic ASP interface between our client application and the server. This is not a website, just an intermediate layer in ASP (I know this is not the preferred way of doing things, told you it's legacy and it can - unfortunately - not be changed at this point). So I'm sending a double value as a parameter on the ...

How can I cast an Interface as it's type in c#?

Hello Everyone, I have a property that returns an interface. During debugging I can break on what was returned and while it is the interface, Visual Studio is smart enough to know the derived type that it actually is. I assume it's using reflection or something. I'm not sure. My question is, can I have that same info avaialble to me at r...

Casting C# out parameters?

Is it possible to cast out param arguments in C#? I have: Dictionary<string,object> dict; // but I know all values are strings string key, value; Roughly speaking (and if I didn't have static typing) I want to do: dict.TryGetValue(key, out value); but this obviously won't compile because it "cannot convert from 'out string' to 'o...

Automatically port conversion elements of C code to C++

Hi, Is there some tool that can automatically convert the following c style code A *a = b; to A *a = (A*)b; Thanks, James ...

[c#] passing a object[] to a params object[] not working

Hi, I've read the topic about passing an object[] to a params object[] but I don't know why it's not working with me. I have these too functions in a class: ... private void CallbackEvent(object source, CallbackEvetArgs e) { // Some event with e.Data as string ... string[] values = e.Data.Split('|'); DoSave("s...

selecting rows from sql server where a casting issue occurs

Hi everyone, i was just wondering if anyone knows how to select rows where a specified column will come under a casting issue. ie. SELECT * FROM ThisTable t WHERE 0 <> ( select cast(t.value as datetime) ) the 'select cast(t.value as datetime)' would ideally return the result of @@error to indicate the casting issue has oc...

When should I use implicit casting?

When is it safe to use implicit casting? Use Case: I'm working with a set of com objects that need to be taken care of specially (Marshal.ReleaseComObject). Is it OK to create a wrapper class that implicitly converts back to the actual com object wrapped? What are some situations when I shouldn't use implicit casting? ...

How to select column values which throw an error if cast to a new type.

Hi there, I need an sql query to give me the following: All the values in a column of type varchar(50) which will NOT convert or will throw an error if cast / converted to an int. E.g. row 1: '1' row 2: '2' row 3: '3a' row 4: '4.5' I need row 3....however there are tens of thousands of rows. Thanks! ...

c# cast byte[*,*,*] to byte[]

Hey there! I have an object with a Property of type byte[,,*] now i'd like to use System.Random::NextBytes() to fill this multidimensional array with random values. NextBytes however takes an argument of byte[] can i cast the multidimensional array somehow to the singledimensional one in order to pass it as an argument? thanks! ...

Type Casting an Object using a "Type" Object in C#

This one has proven to be a little tricky for me so far. I am wondering if it is possible to type cast an object using a System.Type object. I have illustrated below what I mean: public interface IDataAdapter { object Transform(object input); Type GetOutputType(); } public class SomeRandomAdapter : IDataAdapter { public ob...

Java Generics: casting to ? (or a way to use an arbitrary Foo<?>)

So, I have some code that looks approximately like (truncated for brevity - ignore things like the public member variables): public class GenericThingy<T> { private T mValue; public final T[] mCandidates; public GenericThingy(T[] pCandidates, T pInitValue) { mCandidates = pCandidates; mValue = pInitValue; ...

How to implement generic type-safe deep cloning in a Java class hierarchy?

I have a base class, say Base which specifies the abstract method deepCopy, and a myriad of subclasses, say A, B, C, ... Z. How can I define deepCopy so that its signature is public X deepCopy() for each class X? Right, now, I have: abstract class Base { public abstract Base deepCopy(); } Unfortunately, that means that if if I have...

How to CAST to a BIGINT in a query

When I try to get the sum of a column from a table I get the error 'Arithmetic overflow error converting expression to data type int' because the resulting number is to big for an INT. So I tried to CAST to a BIGINT using SELECT CAST(SUM(columnname) AS BIGINT) FROM tablename but I get the same error. Any ideas what i'm doing wrong? ...

Convert linq query to string array - C#

What is the most efficient way of converting a single column linq query to a string array? private string[] WordList() { DataContext db = new DataContext(); var list = from x in db.Words orderby x.Word ascending select new { x.Word }; // return string array here } ...

C#: Convert a string to an object reference.

Basically a button's tag property is the name of an existing combobox which I need to dynamically reference. It's a generic function to handle multiple buttons. Help private void SQLButton(object sender, EventArgs e) { magic(((Button)sender).Tag.ToString()); } private void magic(string currentcombo) { string CurrentText = (Comb...