casting

Bitwise-or seems to have an implicit cast to long. Can this be avoided?

I'm a Java programmer trying to migrate to C#, and this gotcha has me slightly stumped: int a = 1; a = 0x08000000 | a; a = 0x80000000 | a; The first line compiles just fine. The second does not. It seems to recognise that there is a constant with a sign bit, and for some reason it decides to cast the result to a long, resulting in th...

Casting and interface inheritance

Each item has an interface, IItem. As well as this, there is a interface known as IDrawableItem which inherits from Item. The code below, is trying to draw a drawable item, but cannot as the collection this class stores accepts only IItem. You can add anything that inherits from IItem to this class, but using other methods can only be a...

DynamicObject implicit casting

I have a subclass of DynamicObject and I would like to implement implicit casting for primitive types similarly as DO's explicit casting method TryConvert; that is, without writing multiple implicit operator [type] functions. Usage: dynamic myDynamicObject = new MyDynamicObject("1"); int sum = 1 + myDynamicObject; // instead of int i =...

How do I cast a string to integer and have 0 in case of error in the cast with PostgreSQL?

In postgres I have a table with a varchar column. The data is supposed to be integers and I need it in iteger type in a query. Some values are empty strings. The following: SELECT myfield::integer FROM mytable yields ERROR: invalid input syntax for integer: "" How can I query a cast and have 0 in case of error during the cast in pos...

Casting method arguments of a Generic Class...

I've made a generic class that saves and queries objects from a flat file. I keep having to change the method arguments to objects so I can cast them and I'm wondering if I'm going about this the right way... 'T' will always inherit 'FlatFileRecord' This does not compile: public class FlatFile<T> { public void Save(T record) { ...

Creating a service-only subclass of a WCF DataContract class

Is the following concept possible, or will I have trouble serializing this to the Client. Assuming that all comms are only dealing with BaseContractClasses but the Server uses the special sub-class to collate extra server-only data. [DataContract] public class BaseContractClass { [DataMember] public int valueToTransmit; } public cl...

Do derived classes inherit differently?

System.Web.UI.WebControls.UI.TableHeaderCell derives from System.Web.UI.WebControls.UI.TableCell So a method with the signature foo(TableCell tc){} Will accept instances of TableHeaderCell. However if i create two new classes and derive one from TableCell and the other from TableHeaderCell then my new TableHeaderCell class will ob...

Determining casting of an object

Hi , I have the code below RssFeedReader rss = (RssFeedReader)this.ParentToolPane.SelectedWebPart; My problem is only at run time do I know if 'this.ParentToolPane.SelectedWebPart' is of type RssFeedReader or of type 'RssCountry' How would I check the object type and cast it appropriatley? Many Thanks, ...

C# Type-casting oddity - interface as the generic type

I've just run into what I think is an oddity in type-casting. I have code similar to the following: interface IMyClass { } class MyClass: IMyClass { } class Main { void DoSomething(ICollection<IMyClass> theParameter) { } HashSet<MyClass> FillMyClassSet() { //Do stuff } void Main() { HashSet<MyClass> classSet = Fi...

OOC: What is the difference between ToList() and casting to List<T> in .NET?

OOC: Out Of Curiosity So, as a little exercise and for the sake of learning, I decided to check if I was able to implement a very basic recursive function that would return a List<int>, but with the following restrictions: 1- The result should be returned by the function itself (as opposed to passed as an argument to a void function). ...

Casting/Type Conversion Performance

I have the following extension method public static T Field<T>(this DataRow row, string columnName) { return (T)Convert.ChangeType(row[columnName], typeof(T)); } It works, but I'm trying to speed it up. Is there a way to speed that up? With a case statement and then type specific conversions? I've tried a few things like using ...

SQL [Conversion to bool]

C++Builder ADOQuery SQLServer I'm using a stored procedure with this select SELECT Name, COALESCE( ( SELECT TOP 1 0 FROM TbUserParam WHERE TbUserParam.ID_User = @ID_User AND TbUserParam.ID_Param = CfgListParIzm.ID_ListParIzm ), 1) Visi FROM CfgListParIzm WHERE ...

Casting and Types Question

I am bubbling events in my application and so therefore using the bubble events method. As this method handles all sorts of bubbled events their is a switch or if statement within it to determine what sort of event we're dealing with. I was wondering if I could get around this by creating different versions of the event args class. So...

What happens if I cast a function pointer, changing the number of parameters

I'm just beginning to wrap my head around function pointers in C. To understand how casting of function pointers works, I wrote the following program. It basically creates a function pointer to a function that takes one parameter, casts it to a function pointer with three parameters, and calls the function, supplying three parameters. I ...

What's the best way to create a percentage value from two integers in C#?

I have two integers that I want to divide to get a percentage. This is what I have right now: int mappedItems = someList.Count(x => x.Value != null); int totalItems = someList.Count(); (int)(((double)mappedItems /(double) totalItems) * 100) This gives the right answer. But that is a lot of casting to do something as simple as get a ...

java: how can i do dynamic casting of a variable from one type to another?

Hiya. i would like to do dynamic casting for a java variable, the casting type is stored in a different variable. this is regular casting: String a = (String) 5; this is what i want: String theType = 'String'; String a = (theType) 5; is it possible? and if so how? thanks! update I'm trying to populate a class with a hashMap ...

what is that mean reinterpret_cast<char *&>(a) = cp;

Yes this is not very portable, I wonder why one would want to do something like this:` char *cp ; reinterpret_cast<char *&>(a) = cp; ` and what it means? Thx ...

Why is the C# "as" operator so popular?

In development blogs, online code examples and (recently) even a book, I keep stumbling about code like this: var y = x as T; y.SomeMethod(); or, even worse: (x as T).SomeMethod(); That doesn't make sense to me. If you are sure that x is of type T, you should use a direct cast: (T)x. If you are not sure, you can use as but need to ...

How to (efficiently) convert (cast?) a SqlDataReader field to its corresponding c# type?

First, let me explain the current situation: I'm reading records from a database and putting them in an object for later use; today a question about the database type to C# type conversion (casting?) arose. Let's see an example: namespace Test { using System; using System.Data; using System.Data.SqlClient; public enum ...

How to cast a double to an int in Java?

I need to cast a double to an int in Java, but the numerical value must always round down. i.e. 99.99999999 -> 99 Any ideas? :D ...