casting

Why does casting text as varchar without specifying a length truncate the text at 30 characters?

I was searching an MS-SQL Server 2005 database to make sure that a certain text field was always a multiple of 8 in length. When I ran the following query, I got that several rows were of length 30. But when I investigated those records, I found that they were longer than that. select distinct len(cast(textfield as varchar)) from tabl...

C# and Interfaces - Explicit vs. Implicit

In C#, if a class has all the correct methods/signatures for an Interface, but doesn't explicitly implement it like: class foo : IDoo {} Can the class still be cast as that interface? ...

Java: Cyclic generic type relation doesn't allow cast from supertype (javac bug).

Hello! I encounter a totally strange behavior of the Java compiler. I can't cast a supertype to a subtype when cyclic generic type relation is involved. JUnit test case to reproduce the problem: public class _SupertypeGenericTest { interface ISpace<S extends ISpace<S, A>, A extends IAtom<S, A>> { } interface IAtom<S extends ISpac...

Why does a Linq Cast<T> operation fail when I have an implicit cast defined?

I've created two classes, with one of them having an implicit cast between them: public class Class1 { public int Test1; } public class Class2 { public int Test2; public static implicit operator Class1(Class2 item) { return new Class1{Test1 = item.Test2}; } } When I create a new list of one type and try t...

Auto-cast Spring Beans

Is there a way to auto-cast Spring beans to the class defined in the application context XML? I'd like to avoid putting type information about the beans in 2 places.... in the xml configuration file and also in the code as a cast. For instance, given this config file <bean id="bean-name" class="SimpleSpringBean" scope="prototype"> ...

Enum evaluation in c#

Say I have enum as follows (taken from MSDN example): enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri}; I can then use is in the code as follows: int today = (int)Days.Sun; Question: Can I evaluate enums? Say I have a variable Day whose value is "Sun". Will Days.Day evaluate to Days.Sun? Thanks! Please don't get hung up on the d...

Removing non-integers from a string in C++

There was a passing comment in a book of mine about people inputting commas into integers and messing up your program, but it didn't elaborate. That got me thinking, so I tried writing a little algorithm to take an std::string and remove all non-integer characters. This code compiles but skips over output. Why isn't anything being assign...

Upcasting in java

In Java, suppose I have 3 classes, C extends from B which extends from A. And I have one method: public void f(A a); If I do something like this: C c = new C() B b = (B) c; f(b); f accepts b as type C since C and B both extend from A. I wanted f to receive b as type B and not type C. Is there anyway to force this upcasting? ...

Casting an array of unsigned chars to an array of floats

Hi. What is the best way of converting a unsigned char array to a float array in c++? I presently have a for loop as follows for (i=0 ;i< len; i++) float_buff[i]= (float) char_buff[i]; I also need to reverse the procedure, i.e convert from unsigned char to float (float to 8bit conversion) for (i=0 ;i< len; i++) char_buff[i]...

SQL Server, where clauses comparisons with different types & default casting behaviour

Right... this one had me baffled for a while today so maybe one of you SQL Server bright sparks can shed some light on this behaviour. We have a table Phones. In it, the phone numbers are stored as nvarchars and it contains numbers in International format, in only numeric format... so a US number +1-(212)-999-9999 is stored as 121299999...

Implicit operator?

I need some help. I am creating a SelectItem class like this: public class SelectItem<T> where T : class { public bool IsChecked { get; set; } public T Item { get; set; } } I would like the following code to be valid SelectItem<String> obj = new SelectItem<String> { Item = "Value" }; obj.IsChecked = true; String objValue = ...

Recursive cast from SimpleXMLObject to Array

I need to recursively cast a PHP SimpleXMLObject to an array. The problem is that each sub element is also a PHP SimpleXMLElement. Is this possible? ...

Problem with dynamic loading of a dll into my program.

I'm trying to add plugins to my program, and this looks good, except that I can't cast the correct type from the dll. I Have a solution with several projects on it. One of the project is a country Layer, that actually holds a CountryBase (defined as public abstract class CountryBase : CountryLayers.ICountryBase ) The Interface (public i...

How would std::ostringstream convert to bool?

I stumbled across this code. std::ostringstream str; /// (some usage) assert( ! str ); What does ostringstream signify when used in a bool context? Is this possibly an incorrect usage that happens to compile and run? ...

What cast occurs when there is a signed/unsigned mismatch?

When a compiler finds a signed / unsigned mismatch, what action does it take? Is the signed number cast to an unsigned or vice versa? and why? ...

How does the Java cast operator work?

I am trying to debug an issue involving a ClassCastException in Java. In the interest of solving the issue I need to know what is going on when I cast from Object to a specific type. Can anyone explain to me how the Java cast operator works at the Java level and the JVM level? ...

Fixing BeanNotOfRequiredTypeException on Spring proxy cast on a non-singleton bean?

I'm having an issue with pulling a Spring bean from an application context. When I try; InnerThread instance = (InnerThread) SpringContextFactory.getApplicationContext().getBean("innerThread", InnerThread.class); I get; org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'innerThread' must be of type [com.g...

C-Style upcast and downcast involving private inheritance

Consider the following piece of code :- class A {}; class B : private A {}; B* bPtr1 = new B; // A* aPtr1 = bPtr1; // error // A* aPtr2 = static_cast<A*>(bPtr1); // error A* aPtr3 = (A*)bPtr1; B* bPtr2 = (B*)aPtr3; The C-style cast discards the private inheritance while both the implicit and static_cast fail (also dynamic_cast). Why...

Int in a simulated memory array of uchar

In C, in an Unix environment (Plan9), I have got an array as memory. uchar mem[32*1024]; I need that array to contain different fields, such as an int (integer) to indicate the size of memory free and avaliable. So, I've tried this: uchar* memp=mem; *memp=(int)250; //An example of size I want to assign. I know the size of an int i...

Marshalling and casting. Are them related?

Last week a young student ask me if marshalling is the same as casting. My answer was definetly no. Marshalling is seralization, the way to transform a memory representation of an objet into bytes in order to be transmitted to a network whereas casting is related to type convertion / coercion. Later on, rethinking the question I was tho...