casting

explicit cast on XAttribute value

I recently wrote a piece of code that looked a bit like this: IEnumerable<DTO.Employee> xEmployee = from e in xDoc.Descendants("Employee") where int.Parse(e.Attribute("Id").Value) == emp.Id select new DTO.Employee { Id = (int)e.Attribute("Id"), LastName = (string)e.Element("LastName"), FirstName =...

How do I safely cast a System.Object to a `bool` in C#?

I am extracting a bool value from a (non-generic, hetrogeneous) collection. The as operator may only be used with reference types, so it is not possible to do use as to try a safe-cast to bool: // This does not work: "The as operator must be used with a reference type ('bool' is a value type)" object rawValue = map.GetValue(key); bool ...

Unable to cast from object array to a string array

This is my function. public Dictionary<string, string> ArrayToDictionaryConverter(object [] objArray) { string[] strArray = new string[objArray.Length]; strArray =(string[])objArray; Dictionary<string, string> dictionary = null; try { dictionary = new Dictionary<string, string>();...

Need help in translating code from C++ to Java

What is the Java equivalent of following C++ code? float f=12.5f; int& i = reinterpret_cast<int&>(f); ...

Why does ConvertTo work, but casting fail in some cases?

I was always curious. Why does this work: double Number = Convert.ToDouble(TextBox1.Text); But this doesn't: double Number = (double)TextBox1.Text; ...

Why am I getting this ClassCastException?

I have two very simple classes, one extends the other: public class LocationType implements Parcelable { protected int locid = -1; protected String desc = ""; protected String dir = ""; protected double lat = -1000; protected double lng = -1000; public LocationType() {} public int getLocid() { ...

PHP - How to check that a string is an int, but not a double, etc.?

PHP has an intval() function that will convert a string to an integer. However I want to check that the string is an integer beforehand, so that I can give a helpful error message to the user if it's wrong. PHP has is_int(), but that returns false for string like "2". PHP has the is_numeric() function, but that will return true if the n...

Java casting problem

Here is the simplified version of the problem: SomeClass c = (SomeClass) obj.getSomeClassParent() not always but it happens sometimes to trigger exception org.somepackage.SomeClass can't be cast to org.somepackage.SomeClass How is this possible ? I suppose it has something to do with the fact that JAI imageio is native lib,...

C# (+WPF) Problems casting generic class to non-generic.

I must honestly say that I do not really understand much about casting, but I thought this would work. I have a Generic Class Test: public class Test<T,U> { T variable1; U variable2; //etc. } I need to use this class in a WPF view, and since you can't create generic views in WPF (how lovely) I thought: lets just use a...

c# casting question

If I have the following classes: public class MyItems : List<MyItem> { .. } public class MyItem : Item { .. } How could I go about casting an instance of MyItems back down to List<Item>? I've tried doing an explicit cast and I get an exception. ...

C#: Filter the members of an object

I have a model public class User : EntityObject { public int Id { get; set; } public string Username { get; set; } public string Password { get; set; } } and I would like to return to the client a JSON result containing only the Username and Password members of the User. I am currently doing this like so return Json(new...

When you unbox an object, where is the objects datatype stored so the compiler can perform the cast?

Internally speaking, when you cast an object to a strongly typed object like: User u = (User)o; Where is the type of the object stored? is it stored in another location or within the memory block where the actual object is stored? (I don't know much about this topic, so I could very well be asking a question that doesn't make comple...

What happens in C++ when an integer type is cast to a floating point type or vice-versa?

Do the underlying bits just get "reinterpreted" as a floating point value? Or is there a run-time conversion to produce the nearest floating point value? Is endianness a factor on any platforms (i.e., endianness of floats differs from ints)? How do different width types behave (e.g., int to float vs. int to double)? What does the l...

Casting List<MyObject> to IEnumerable<MyInterface>

I'm still learning some of this c# stuff, and I couldn't find an answer to this question. Assuming that I have a list of MyObject implementing MyInterface public class MyObject : IMyInterface { ...} public List<MyObject> MyObjectList; How can I return an IEnumerable<IMyInterface> with the contents of MyObjectList? I mean, right now...

Casting an Integer to a Double in XPath 1.0

I am currently building a XML based Jasper Report in which I am trying to add a tax percent onto the cost. A problem occurs when I use sum(//net-total) to value calculate the total cost. When the summed total has no decimal places it returns and Integer and I need a double. I have tried sum(//net-total) * 1.0 but it doesn't seem to wor...

Casting numeric data types in C

I saw this question on SO about casting, and the answer specifically mentioned numeric types. I thought I understood this till now, when I have to do it. I have two int's I want to add, divide by two and save to another int. So I create a temporary variable called 'bucket' that's one datatype bigger, add the two ints and shift right b...

[C++] Overlaying a struct onto a u_char pointer.

My professor has instructed me that we can lay a struct over (casting) the pointer in memory we are getting in order to more easily interpret the data. I asked about this in class today and this is what he said would work. This is not compiling complaining about how it can't cast it. What am I doing wrong? I am about to resort to parsi...

Portably determining whether string is castable to integer

I'd like to cast a VARCHAR to a SQL INTEGER, supplying a default value if some value in the field would not convert properly. Something like: sql> SELECT str FROM tbl; -- CREATE TABLE tbl (str VARCHAR(12), ...) str ======== 12345 -1 foo sql> SELECT CAST((CASE WHEN ... THEN str ELSE '-9999' END) AS INTEGER) AS "int" FRO...

C# Cast Entire Array?

I see this Array.ConvertAll method, but it requires a Converter as an argument. I don't see why I need a converter, when I've already defined an implicit one in my class: public static implicit operator Vec2(PointF p) { return new Vec2(p.X, p.Y); } I'm trying to cast an array of PointFs to an array of Vec2s. Is the...

Why is my return value wrong?

Hi all, I have a vector class in C# (a fragment below). My issue is that when I call GetMagnitude(), it always returns 0.0f - even with the debugger running and I check that Sq has a valid value, as soon as it gets passed back into other function (eg: Normalize() ), it has return 0.0f. Can someone explain this and help me fix it? My gue...