casting

Force casting one assembly type to another assembly type in c#

Is there a way to force-cast between different types of different assemblies? I need to execute a function, whose assembly has been loaded with Assembly.Load(ReadAllBytes(...)), but it fails in argument casting. So, is there any way to "reinterpret_cast" objects in c#? EDIT Most basic example of my casting problem is: Assembly ass = A...

XSLT Type Checking

Hi Folks Is it possible to check an elements ComplexType? i have this (simplified): complexType Record complexType Customer extension of Record complexType Person extension of Record <xsl:template match="/"> <records> <xsl:apply-templates /> </records> </xsl:template> <xsl:template match="!!! TYPECHECK FOR RECORD !!!" ...

C# 'is' type check on struct - odd .NET 4.0 x86 optimization behavior

Update: I have filed a bug report with Microsoft Connect, please vote for it! Update 2: Microsoft have marked the bug report as fixed Posted by Microsoft on 18/08/2010 at 17:25 This bug will be fixed in a future version of the runtime. I'm afraid it's too early to tell if that will be in a service pack or the next major...

LINQ .Cast() extension method fails but (type)object works.

To convert between some LINQ to SQL objects and DTOs we have created explicit cast operators on the DTOs. That way we can do the following: DTOType MyDTO = (LinqToSQLType)MyLinq2SQLObj; This works well. However when you try to cast using the LINQ .Cast() extension method it trows an invalid cast exception saying cannot cast type Lin...

casting doubles to integers in order to gain speed

Hello all, in Redis (http://code.google.com/p/redis) there are scores associated to elements, in order to take this elements sorted. This scores are doubles, even if many users actually sort by integers (for instance unix times). When the database is saved we need to write this doubles ok disk. This is what is used currently: snprin...

How I change a variable of a type to another one in C?

I want to do it: int main () { bla bla bla void *onetype; switch (USER_INPUT_TYPE) { CASE CONVERT_TO_CHAR: convert onetype VOID TO CHAR >>> HOW??? CASE CONVERT_TO_INT: convert onetype VOID TO INT >>> HOW??? LOT OF CASES... } } Yes, I know type casting, but type casting is a 'temporary'...

C++ casted realloc causing memory leak

I'm using a function I found here to save a webpage to memory with cURL: struct WebpageData { char *pageData; size_t size; }; size_t storePage(void *input, size_t size, size_t nmemb, void *output) { size_t realsize = size * nmemb; struct WebpageData *page = (struct WebpageData *)output; page->pageData = (char *)re...

SQL Server, Select CASE with different casting

I want to do a select that do a cast only for a specific ID but it doesn't seems to work. Example : SELECT CASE WHEN(@ID <> 1) THEN Code WHEN(@ID = 1) THEN Cast(Code AS int) END Code FROM .... Any Idea ? ...

Is private members hacking a defined behaviour ?

Hi, Lets say I have the following class: class BritneySpears { public: int getValue() { return m_value; }; private: int m_value; }; Which is an external library (that I can't change). I obviously can't change the value of m_value, only read it. Even subclassing BritneySpears won't work. What if I define the following ...

Java Convert 4 bytes to int

i was wondering if the solution for this documented here is still the solution or is there any other way getting an int from 4 bytes? thank you. EDIT: im getting the byte[] from sockets .read EDIT: int recvMsgSize = in.read(Data, 0, BufferSize); if recvMsgSize is -1 i know the connection has been dropped. how do i detect this when im...

Can 'iterator' type just subclass 'const_iterator'?

After another question about iterators I'm having some doubts about custom containers. In my container, iterator is a subclass of const_iterator, so that I get conversion from non-const to const "for free". But is this allowed or are there any drawbacks or non-working scenarios for such a setup? ...

Helper Casting Functions -- Is it a code smell?

I recently began to start using functions to make casting easier on my fingers for one instance I had something like this ((Dictionary<string,string>)value).Add(foo); and converted it to a tiny little helper function so I can do this ToDictionary(value).Add(foo); Is this a code smell? Also, what about simpler examples? For example...

How to cast a pointer of memory block to std stream

I have programed an application on windows XP and in Visual Studio with c++ language. In that app I used LoadResource() API to load a resource for giving a file in the resource memory. It returned a pointer of memory block and I wanna cast the pointer to the std stream to use for compatibility. Could anyone help me? ...

[nHibernate] casting string to bool using nHibernate Criteria

I have an nHibernate query using Criteria, and I am trying to cast a string to bool in the query itself. I have done the same with casting a string to int, and that works well (the "DataField" property is "1" as a string): var result = Session .CreateCriteria<Car>() .Add(Restrictions.Eq((Projections.Cast(NHibernateUtil.Int32, ...

Generic property- how to specify the type at run time

I was reading a question on making a generic property, but I'm a little confused by the last example from the first answer (I've included the relevant code below): You have to know the type at compile time. If you don't know the type at compile time then you must be storing it in an object, in which case you can add the follo...

Can .NET convert "Yes" & "No" to boolean without If?

You would think there would be a way using DirectCast, TryCast, CType etc but all of them seem to choke on it e.g.: CType("Yes", Boolean) You get: System.InvalidCastException - Conversion from string "Yes" to type 'Boolean' is not valid. ...

Typecast a ArrayList.toArray() in Java to normal array

I'm having some trouble to do the following: int[] tmpIntList = (int[])MyArrayList.toArray(someValue); MyArrayList contains only numbers. I get a typecast error since ArrayList only returns Object[] but why isnt it possible to turn it into a int[] ? ...

Generic List<T> as IEnumerable<object>

I'm trying to do cast a List to an IEnumerable, so I can verify that different lists are not null or empty: Suppose myList is a List < T > . Then in the caller code I wanted: Validator.VerifyNotNullOrEmpty(myList as IEnumerable<object>, @"myList", @"Clas...

Beginner Java Question about Integer.parseInt() and casting

so when casting like in the statement below :- int randomNumber=(int) (Math.random()*5) it causes the random no. generated to get converted into an int.. Also there's this method I just came across Integer.parseInt() which does the same ! i.e return an integer Why two different ways to make a value an int ? Also I made a search an...

PyArg_ParseTuple plus cast

I have a c function being called from python. Python gives the function an integer, but I would like to use it as another data type in C. I am currently using PyArg_ParseTuple (args, "i", &value) and then manually doing a cast on value. Is there a way to do this cast through PyArg? ...