casting

How bad is dynamic casting?

We often hear/read that one should avoid dynamic casting. I was wondering what would be 'good use' examples of it, according to you? Edit: @Sam: yes, I'm aware of that other thread: it is indeed when reading one of the first answers there that I asked my question! ...

Cast int to Enum in C#

What's a quick and easy way to cast an int to an enum in c#? ...

How should I cast in VB.NET?

Are all of these equal? Under what circumstances should I choose each over the others? var.ToString() CStr(var) CType(var, String) DirectCast(var, String) EDIT: Suggestion from NotMyself TryCast(var, String) ...

Cast List<int> to List<string>

Does anyone know if you can cast a List<int> to List<string> somehow? I know I could loop through and .ToString() the thing but a cast would be awesome. I'm in c# 2.0 (so no linq) ...

C# Casting vs. Parse

This may seem rudimentary to some, but this question has been nagging at me and as I write some code, I figured I would ask. Which of the following is better code in c# and why? ((DateTime)g[0]["MyUntypedDateField"]).ToShortDateString() or DateTime.Parse(g[0]["MyUntypedDateField"].ToString()).ToShortDateString() Ultimately, is it ...

In C++, why use static_cast<int>(x) instead of (int)x?

I've heard that, in C++, the static_cast function should be preferred to C-style or simple function-style casting. Is this true? Why? ...

I need a helper method to compare a char Enum and a char boxed to an object

I have an enum that looks as follows: public enum TransactionStatus { Open = 'O', Closed = 'C'}; and I'm pulling data from the database with a single character indicating - you guessed it - whether 'O' the transaction is open or 'C' the transaction is closed. now because the data comes out of the database as an object I am having a h...

Automatic casts

I am currently suffering a brain fart. I've done this before but I can't remember the exact syntax and I can't look at the code I wrote because I was working at another company at the time. I have this arrangement: class P { // stuff }; class PW : public P { // more stuff }; class PR : public P { // more stuff }; class C { public: ...

Why is a SQL float different from a C# float

Howdy, I have a DataRow pulled out of a DataTable from a DataSet. I am accessing a column that is defined in SQL as a float datatype. I am trying to assign that value to a local variable (c# float datatype) but am getting an InvalidCastExecption DataRow exercise = _exerciseDataSet.Exercise.FindByExerciseID(65); _AccelLimit = (float...

Automatic Casts redux

After I messed up the description of my previous post on this I have sat down and tried to convey my exact intent. I have a class called P which performs some distinct purpose. I also have PW which perform some distinct purpose on P. PW has no member variables, just member functions. From this description you would assume that the code...

Direct casting vs 'as' operator?

Consider the following code: void Handler(object o, EventArgs e) { // I swear o is a string string s = (string)o; // 1 //-OR- string s = o as string; // 2 // -OR- string s = o.ToString(); // 3 } What is the difference between the three types of casting(okay, 3rd one is not a casting, but you get the intent... ), and ...

Linq output as an Interface?

Here's the code that I'm attempting to do: public IList<IOperator> GetAll() { using (var c = new MyDataContext()) { return c.Operators.ToList(); } } Operator implements IOperator, but I'm getting the following compilation error: Cannot implicitly convert type 'System.Collections.Generic.List<MyProject.Core...

implicit operator using interfaces

I have a generic class that I'm trying to implement implicit type casting for. While it mostly works, it won't work for interface casting. Upon further investigation, I found that there is a compiler error: "User-defined conversion from interface" that applies. While I understand that this should be enforced in some cases, what I'm tr...

c# downcasting when binding to and interface

Is there a better way of binding a list of base class to a UI other than downcasting e.g: static void Main(string[] args) { List<Animal> list = new List<Animal>(); Pig p = new Pig(5); Dog d = new Dog("/images/dog1.jpg"); list.Add(p); list.Add(d); foreach (Animal a in list) { DoPigStuff(...

Converting a pointer into an integer

I am trying to adapt an existing code to a 64 bit machine. The main problem is that in one function, the previous coder uses a void* argument that is converted into suitable type in the function itself. A short example: void function(MESSAGE_ID id, void* param) { if(id == FOO) { int real_param = (int)param; // ... ...

How to cast a number to a byte?

In C and C++ you can tell the compiler that a number is a 'long' by putting an 'l' at the end of the number. e.g long x = 0l; How can I tell the C# compiler that a number is a byte? ...

Proper nullable type checking in C#?

Ok, my actual problem was this: I was implementing an IList<T>. When I got to CopyTo(Array array, int index), this was my solution: void ICollection.CopyTo(Array array, int index) { // Bounds checking, etc here. if (!(array.GetValue(0) is T)) throw new ArgumentException("Cannot cast to this type of Array."); // Handl...

Casting in Informix

In Informix, how can I cast a char(8) type into a money type, so that I can compare it to another money type? ...

How to check in Java using JIntegra if COM object implements an interface ?

I'm working with JIntegra java com bridge. I have an object of type Object which true coclass is unknown. I need to check if that object can be casted to specific COM interface (which has a proxy class generated by JIntegra). ...

Compiler fails converting a constrained generic type.

Hello, I have a class that has a Generic type "G" In my class model i have public class DetailElement : ElementDefinition Let's say i have a method like this public void DoSomething<G>(G generic) where G : ElementDefinition { if (generic is DetailElement) { ((Detai...