type-conversion

Convert System.Drawing.Image to System.Windows.Controls.Image?

Is there a way in C# to do this conversion and back? I have a WPF app which has a Image control. I'm trying to save the image in that control to a SQL Database. In my Entity Model, the datatype of the picture column in my database is a byte[]. So I found a method to convert a System.Drawing.Image to a byte[] and back. But I haven't ...

C++: implicit conversion between 3rd party types

There are two classes: A and B. There are algorithms for converting from type A to type B and back. We cannot touch the source code of them. Can I write an implicit conversion between the two types? Example code which should work: B CalculateSomething(double x) { A a(x); a.DoSomethingComplicated(); return a; } ...

Why can't I use interface with explicit operator?

Hi, I'm just wondering if anyone knows the reason why you are not allowed to use interfaces with the implicit or explicit operators? E.g. this raises compile time error: public static explicit operator MyPlayer(IPlayer player) { ... } "user-defined conversions to or from an interface are not allowed" Thanks, ...

TypeConverter prevents ApplyPropertyChanges in EntityFramework

I ran into an interesting problem (hopefully, interesting not just for me :) I am running Entity Framework 1 (.NET 3.5) and ASP.NET MVC 2. I have a Customer class that has many-to-one relationship with Country class (in other words, Country is a lookup table for customers - I described more in this post: http://stackoverflow.com/questio...

Flash type casting gone wrong

Sorry, I'm new to flash I have this line of code: BaseEntry( _entryList[i] ).topTeamName = ((Team)(teamList.getNameAtIndex( i*2 ))).Name; and I get the error: TypeError: Error #1034: Type Coercion failed: cannot convert "[object Team]" to ncaa.Data.Team. What do I need to do to fix it? ...

Convert Option[Object] to Option[Int] Implicitly

I'm working with legacy Java code which returns java.lang.object. I'm passing it into a function and I'd like to do some implicit conversions as such: implicit def asInt( _in:Option[Object] ) = _in asInstanceOf[ Option[Int] ] implicit def asDouble( _in:Option[Object] = _in asInstanceOf[ Option[Double] ] private def parseEntry( _name:S...

How do I handle editing of custom types in a C# datagridview?

I have a datagridview in which one column contains a custom class, which I have set using: dgvPeriods.Columns[1].ValueType = typeof(ExDateTime); It is rigged up to display correctly by handling the CellFormatting event, but I'm unsure what event to handle for cell editing. In the absence of doing anything I get a FormatException as th...

Python, a smarter way of string to integer conversion

Hello I have written this code to convert string in such format "0(532) 222 22 22" to integer such as 05322222222 . class Phone(): def __init__(self,input): self.phone = input def __str__(self): return self.phone #convert to integer. def to_int(self): return int((self.phone).replace(" ","").repla...

How to store an interger value of 4 bytes in a memory of chunk which is malloced as type char

I have allocated a chunk of memory of type char and size is say 10 MB (i.e mem_size = 10 ): int mem_size = 10; char *start_ptr; if((start_ptr= malloc(mem_size*1024*1024*sizeof(char)))==NULL) {return -1;} Now I want to store the size information in the header of the memory chunk.To make myself more clear, let's say: start_ptr = 0xaf86...

Cannot initialize non-const reference from convertible type

Hi, I cannot initialize a non-const reference to type T1 from a convertible type T2. However, I can with a const reference. long l; const long long &const_ref = l; // fine long long &ref = l; // error: invalid initialization of reference of // type 'long long int&' from expression of type ...

C# newbie problem with variable types

int newWidth = 100; int newHeight = 100; double ratio = 0; if (img1.Width > img1.Height) { ratio = img1.Width / img1.Height; newHeight = (int)(newHeight / ratio); } else { ratio = img1.Height / img1.Width; newWidth = (int)(newWidth / ratio); } Image bmp1 = img1.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero); ...

Why does this implicit type conversion in C# fail?

Background: Let's assume I've got the following class: class Wrapped<T> : IDisposable { public Wrapped(T obj) { /* ... */ } public static implicit operator Wrapped<T>(T obj) { return new Wrapped<T>(obj); } public void Dispose() { /* ... */ } } As you can see, it provides an implicit type conversion ope...

How do I change the class of an object to a subclass of its current class in C++?

I have an array of pointers to a base class, so that I can make those pointers point to (different) subclasses of the base class, but still interact with them. (really only a couple of methods which I made virtual and overloaded) I'm wondering if I can avoid using the pointers, and instead just make an array of the base class, but have ...

C# Reading and Writing a Char[] to and from a Byte[] - Updated with Solution

Hi, I have a byte array of around 10,000 bytes which is basically a blob from delphi that contains char, string, double and arrays of various types. This need to be read in and updated via C#. I've created a very basic reader that gets the byte array from the db and converts the bytes to the relevant object type when accessing the pro...

c++ floating point to integer type conversions

What are the different techniques for a floating point type to an integer type conversion, in c++? ...

Confused about type conversion in C++

In C++, the following lines have me confused: int temp = (int)(0×00); int temp = (0×00int); What is the difference between those 2 lines? ...

.NET Type Conversion Issue: Simple but difficult

Well, the question is kinda simple. I have a object defined as: public class FullListObject : System.Collections.ArrayList, IPagedCollection And when i try to: IPagedCollection pagedCollection = (IPagedCollection)value; It don't work... value is a FullListObject... this is my new code trying to get around a issue with the "is" op...

How to make safe cast using generics in C#?

I want to implement a generic method on a generic class which would allow to cast safely, see example: public class Foo<T> : IEnumerable<T> { ... public IEnumerable<R> SafeCast<R>() where T : R { return this.Select(item => (R)item); } } However, the compiler tells me that Foo<T>.SafeCast<R>() does not d...

C# - Converting a float to an int... and changing the int depending on the remainder

Hi, this is probably the really newbie question (well, I'm pretty sure it is), but I have a float that's being returned and I need a quick and efficient way of turning it into an int. Pretty simple, but I have an exception. If the remainder of the float is anything other than .0 then I want to increment the int. Some quick examples: F...

changing the serialization procedure for a graph of objects (.net framework)

Hello I'm developing a scientific application using .net framework. The application depends heavily upon a large data structure (a tree like structure) that has been serialized using a standard binaryformatter object. The graph structure looks like this: <Serializable()> Public Class BigObject Inherits List(Of SmallObject) End Class <...