casting

Is there a way cast an object back to it original type without specifing every case?

I have an array of different type objects and I use a BinaryWriter to convert each item to its binary equivalent so I can send the structure over the network. I currently do something like for ( i=0;i<tmpArrayList.Count;i++) { object x=tmpArrayList[i]; if (x.GetType() == typeof(byte)) { wrt.Write((byte)x); } .......

Why does casting a null to a primitive(ie: int) in .net 2.0 throw a null ref exception and not a invalid cast exception?

I was going through some code and came across a scenario where my combobox has not been initialized yet. This is in .NET 2.0 and in the following code, this.cbRegion.SelectedValue is null. int id = (int)this.cbRegion.SelectedValue; This code threw a null reference exception instead of an invalid cast exception. I was wondering if an...

enum-int casting: operator or function

In the external code that I am using there is enum: enum En {VALUE_A, VALUE_B, VALUE_C}; In another external code that I am using there are 3 #define directives: #define ValA 5 #define ValB 6 #define ValC 7 Many times I have int X which is equal to ValA or ValB or ValC, and I have to cast it to the corresponding value of En (ValA...

Downcasting in Java

Upcasting is allowed in Java, however downcasting gives a compile error. The compile error can be removed by adding a cast but would anyway break at the runtime. In this case why Java allows downcasting if it cannot be executed at the runtime? Is there any practical use for this concept? public class demo { public static void mai...

Is this an inefficent use of casting?

Hi folks, this question is an extension to a previous question i asked (and was answered). I'm refactoring my code an playing around with / experimenting with various refactored solutions. One of the solutions i came up with (but wasn't happy with .. remember, i'm just experimenting with some personal coding styles) wsa the following c...

Why use TryCast instead of Directcast ?

Hi, When I am trying to cast Object obj to Type T, If it can not be cast then there is something wrong. And after I cast the object I will be looking for working with the casted object. Rather I will be expecting to get an exception at the place where I will be casting it than say where I will be using that object. In this sense ...

Change the class of an object at runtime

I'm working with a CMS, Joomla, and there's a core class which renders a set of parameters to a form, JParameter. Basically it has a render() function which outputs some table-laden HTML which is not consistent with the rest of my site. For issues of maintainability, and because I have no idea where else this is being used, I don't want...

Null literal issue

Hi Guys, I have a repeater that should show a bound field value only if it exists. Having read this post I decided to do it by using a literal within my repeater and using the OnItemDatabound trigger to populate my literal but my literal doesn't seem to be accessible from the c# code behind and I don't understand why! Heres the aspx pa...

Why C# compiler doesn't call implicit cast operator?

Suppose we have following type: struct MyNullable<T> where T : struct { T Value; public bool HasValue; public MyNullable(T value) { this.Value = value; this.HasValue = true; } public static implicit operator T(MyNullable<T> value) { return value.HasValue ? value.Value : default(T); ...

Casting error in ASP.NET

I have a class declared in the App_Code folder. The class contains a public shared method that returns a type Portfolio. When I try to call this method to initialize an object of type Portfolio in one of the ASCX controls, i get a "Value of type Jaguar.Portfolio cannot be converted to Jaguar.Portfolio" message. This is a "Website" proj...

Quick Java question: Casting an array of Objects into an array of my intended class

Just for review, can someone quickly explain what prevents this from working (on compile): private HashSet Data; ... public DataObject[] getDataObjects( ) { return (DataObject[]) Data.toArray(); } ...and what makes this the way that DOES work: public DataObject[] getDataObjects( ) { return (DataObject[]) Data.toArray( new D...

How does a Java compiler parse typecasts?

A simple expression like (x) - y is interpreted differently depending on whether x is a type name or not. If x is not a type name, (x) - y just subtracts y from x. But if x is a type name, (x) - y computes the negative of y and casts the resulting value to type x. In a typical C or C++ compiler, the question of whether x is a type or...

"Dynamic" Casting in Java

Hullo all, Wondering if there are any Java hackers who can clue me in at to why the following doesn't work: public class Parent { public Parent copy() { Parent aCopy = new Parent(); ... return aCopy; } } public class ChildN extends Parent { ... } public class Driver { public static void main(Stri...

Casting a char to an unsigned short: what happens behind the scenes?

Given this field: char lookup_ext[8192] = {0}; // Gets filled later And this statement: unsigned short *slt = (unsigned short*) lookup_ext; What happens behind the scenes? lookup_ext[1669] returns 67 = 0100 0011 (C), lookup_ext[1670] returns 78 = 0100 1110 (N) and lookup_ext[1671] returns 68 = 0100 0100 (D); yet slt[1670] returns ...

Python: unpack to unknown number of variables?

How could I unpack a tuple of unknown to, say, a list? I have a number of columns of data and they get split up into a tuple by some function. I want to unpack this tuple to variables but I do not know how many columns I will have. Is there any way to dynamically unpack it to as many variables as I need? Thanks for your help :) ...

C# - Problem with generics and inheritance

Hi, I've got a problem with inheritance and generics. This is the code that illustrates my problem: namespace TestApplication { public class MyClass<T> { private T field; public MyClass(T field) { this.field = field; } } public class MyIntClass : MyClass<int> { pu...

dynamic cast with interfaces

I have a class with implements 2 interfaces and inherits 1 class. So, generally it looks like this: class T : public A, public IB, public IC { }; There is one point in the code where I have an "IB *", but could really use an "A *". I was hoping that a dynamic cast would like this: IB *b_ptr = new T; // it's really more complicated, b...

Can I programmatically set the object type for a DirectCast command?

I'm helping a colleague develop a "catch all" type error handler for some controls his application. What he wants to do is pass the object that has the error, and the type of that object, such a TextBox or ComboBox, and then call the DirectCast method within his handler to properly address the Text attribute within it. In general, the me...

How do I add an integer value with javascript (jquery) to a value that's returning a string?

I have a simple html block like: <span id="replies">8</span> Using jquery I'm trying to add a 1 to the value (8). var currentValue = $("#replies").text(); var newValue = currentValue + 1; $("replies").text(newValue); What's happening is it is appearing like: 81 then 811 not 9, which would be the correct answer. What am I doing...

How to cast a pointer in C++

void foo(void **Pointer); int main () { int *IntPtr; foo(&((void*)IntPtr)); } Why do I get an error? error: lvalue required as unary ‘&’ operand Thanks ...