casting

Linq dynamic select casting issue

I want to run a query to get a string array of distinct "logName" items for a logType. The following works great: Dim stringArray() As String = (From item In dc.Vw_Logs Where item.LogType = [Passed in logType] Select item.LogName Distinct).ToArray() However, this only works when a specific LogType is set. I would like the ...

Cast objects of different classes but same types

I have two classes generated by LINQ2SQL both from the same table so they have the exact same properties. I want to convert / cast a object to the other class. what's the easiest way to do this? I know I can just manually assign each property, but that is a lot of code. ...

PHP: Converting a 64bit integer to string

I'm trying to use a hardcoded 64bit integer in a string variable. Simplfied I want to do something like this: $i = 76561197961384956; $s = "i = $i"; Which should result in s being: i = 76561197961384956 This does obviously not work as PHP cast big integers to float, therefore s is: i = 7.65611979614E+16 While several other meth...

Class implicit conversions

I know that I can use implicit conversions with a class as follows but is there any way that I can get a instance to return a string without a cast or conversion? public class Fred { public static implicit operator string(Fred fred) { return DateTime.Now.ToLongTimeString(); } } public class Program { static void...

Fastest casting in VB.NET with a known type?

When I have many controls on a Form (i.e. Label, Button etc) that do almost the same thing, I often use one method to handle all the controls Click, MouseDown, MouseUp events. But to know which of the controls throwing the event and access the properties of that control I need to cast the "sender" object to the correct type. The thing ...

What other useful casts can be used in C++

C++ comes with four built-in casts. static_cast dynamic_cast const_cast reinterpret_cast Not to meantion the frowned upon C (style*)cast. Additionally boost supplies a lexical_cast, are there any other useful casts that you use or would like to exist? ...

How to convert object[] to List<string> in one line of C# 3.0?

ok I give up, how do you do this in one line? public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { //List<string> fields = values.ToList<string>(); //List<string> fields = values as List<string>; //List<string> fields = (List<string>)values; List<string> f...

Working with Dynamic Types

I am using entity model with a SQL DB, and I have a set of about 30 lookup tables which are in the same 3 column format, ID,Text,IsActive. But rather than create 30 forms I would like to create one form and depending on the string name of a type given, then populate a gridview and allow ability to Insert,Update or Delete records of that ...

Help converting type - cannot implicitly convert type 'string' to 'bool'

Hi all, I am fairly new to c# and asp.net and am having an issue converting type; My situation is that I have a search engine that has a textbox for the search text and two radio buttons for the search location ( IE City1 or City2 ) When I recieve the search text and radio buttons they are in the form of a string IE string thesearcht...

Why does my converter giving an invalid cast error?

I created a Converter to convert from double to integer. But the line "return (int)value;" always gets a "specified cast is not valid." What do I have to do so that my Converter successfully converts a double and sends back an integer? Converter: namespace TestChangeAngle { [ValueConversion(typeof(double), typeof(int))] class...

Telerik RadGrid Cast exception when populated with an array of objects by their parent type

I have just come across a Casting Exception while using the Telerik RadGrid. It occurs during the DataBind event if I have an array of objects as the datasource radgrid1.DataSource = new BaseObject[] { new ChildObject1(), new ChildObject2() }; where the classes ChildObject1 and ChildObject2 both inherit from the class BaseObject. ...

How to up-cast to a generic object?

I have an array of Castle windsor registration components of type IRegistration[] In this case ComponentRegistration<T> : IRegistration For each element in my array, if it can be upcast to ComponentRegistration<> I would like to upcast it back to ComponentRegistration<T> and process it. How exactly would I do this? I got as far as ...

To return a double, do I have to cast to double even if types are double in c#?

To return a double, do I have to cast to double even if types are double? e.g. double a = 32.34; double b = 234.24; double result = a - b + 1/12 + 3/12; Do I have to cast (double) ? ...

How to cast an object to a nullable of type struct?

In the code base I'm working in there have a method that has the signature Public Sub SetDropDownValue(Of T As Structure)(ByVal target As ListControl, ByVal value As Nullable(Of T)) The method I am writing is passed a parameter of type object. How can I cast the object into something that can be passed into the SetDropDownValue metho...

For reference types how does using IEquatable<T> reduce the use of casting?

I've read in several articles that for reference types using IEquatable reduces the use of casting Can someone kindly provide a convincing example. Thanks Clarry ...

Type safety: Unchecked cast between Class-Objects

I am quite comfortable with generics and such, but in this special case I have a question concerning the "Type safety: Unchecked cast from .. to .." warning. Basically I have a List of Class objects and now I want to get a subset of these that implement a special interface but the resulting List should also have that special Type: ... ...

Cast object to T

I'm parsing an XML file with the XmlReader class in .NET and I thought it would be smart to write a generic parse function to read different attributes generically. I came up with the following function: private static T ReadData<T>(XmlReader reader, string value) { reader.MoveToAttribute(value); object readData = reader.ReadCon...

Flex loads SWF as Sprite. How do I load it as a Movieclip instead?

Hi, I have a problem converting SWFLoader.content into a MovieClip instance while following (TheFlashCanon's excellent tutorial) on making a SWF communicate with Flex. The SWF loaded in question is compiled using Flash CS3 (using actionscript 3). However, when I try to get the content of the SWFLoader and convert it into a MovieClip ins...

Ensure number is cast correctly in .NET

I know that I can assign a value specifically to a float by doing float y = 4.5f; I want to do the same thing, except as a byte. How do I do this? I've checked the MSDN documentation and cannot find anything related to this. Also, what is this called? Thanks, [Edit] For clarity the code I'm using this on is byte myByte = a==b?1:0;...

Can someone explain "ClassCastException" in Java?

I read some articles written on "ClassCastException" but I couldn't get a good idea on that. Can someone direct me to a good article or explain it briefly. ...