casting

How can I cast a Java class in Clojure?

I would like to cast a clojure Java object (assigned with let*) to another Java class type. Is this possible and if so then how can I do this? Update: Since I posted this question I have realised that I do not need to cast in Clojure as it has no concept of an interface, and is more like Ruby duck typing. I only need to cast if I need t...

C#: Convert T to T[]

I would like to convert T to T[] if it is an array. static T GenericFunction<T>(T t) { if (t == null) return default(T); if (t.GetType().IsArray) { //if object is an array it should be handled //by an array method return (T) GenericArrayFunction((T[])t); } ... } s...

How to cast an Object to an int in java?

How can I cast an Object to an int in java? ...

'Cast object of type' error - resolved by IIS recycle

3rd party ASP.Net web site and web service installed. The code appears not to log errors to server log or custom error log. User receives the following critical untrapped error message: "System.Web.Services.Protocols.SoapException: Unable to cast object of type 'System.Security.Principal.WindowsIdentity' to type 'Library.Security.Iden...

Why does this conversion doesn't work?

Hi, following code behaves strange (at least for me): int testValue = 1234; this.ConversionTest( testValue ); private void ConversionTest( object value ) { long val_1 = (long) (int) value; // works long val_2 = (long) value; // InvalidCastException } I don't understand why the direct (explicit) cast to long doesn't wo...

Casting string into decimal

I am attempting to cast a string gotten from a local database into decimal, but resulted in a "Cannot implicitly convert Type 'GlobalCurrencyConverter.CurrencyRateDataSet.rateDataTable' to decimal". Below is my code, protected decimal calRate_Click(object sender, EventArgs e) { CurrencyRateDataSetTableAdapters.rateTableAdapter rat...

cast class into another class or convert class to another

my question is shown in this code i have class like that public class maincs { public int a; public int b; public int c; public int d; } public class sub1 { public int a; public int b; public int c; } public void methoda (sub1 model) { maincs mdata = new maincs(){a = model.a , b = model.b , c= model.c} ; /...

Why does casting a Type[] to a Class[] throw a ClassCastException?

Today i was writing some heavy reflection-using code, and i came across this behavior i can't explain: why does Type[] types = ((ParameterizedType)m.getGenericReturnType()).getActualTypeArguments(); Class[] c = (Class[])types; Throw a ClassCastException, when iterating over that same array and casting every single element, i.e. for(T...

Why won't this cast?

public interface IMenuCollection<T> : ICollection<T> public class HTMLMenuCollection: IMenuCollection<HTMLMenu> This is my custom collection definition. I am trying to cast to it from another collection IList. IList<HTMLMenu> htmlMenuList = new List<HTMLMenu>(); ... HTMLMenuCollection tempColl = (HTMLMenuCollection)htmlMenuList; I ...

Old-school Pascal question about how to cast variant record function parameters properly

I am trying to create a function with a variant record-type parameter that allows inline-casting or assignment, as such: type rectype = ( VT_INT, VT_CHAR, VT_BOOL ); rec = record case t : rectype of VT_INT : ( i : integer ); VT_CHAR : ( c : char ); VT_BOOL : ( b : boolean ); end; procedure h...

How to cast a generic class to a generic interface in C#

according to below definitions interface myin { int id { get; set; } } class myclass:myin { public int id { get; set; } } [Database] public sealed class SqlDataContext : DataContext, IDataContext { public SqlDataContext(string connectionString) : base(connectionString){} public ITable<IUrl> Urls { get { retur...

How to cast INT32 to INT during a SQL select through a LINQ data context?

Hi, I have the following statement: someList = dc.ExecuteQuery<MyCustomType>(@"Select Id As ProductId, Name From ivProduct").ToList(); Id in the database is stored as int32, when my Id property of MyCustomType is INT. Is there a way to cast that int32 to int during a select? Thank you ...

How do I treat a plain-text message as a numerical value (for encryption algorithms)?

I am learning about cryptographic algorithms and in the process am attempting to implement some well known schemes. I understand the mathematical explanations behind RSA and El Gamal, but am currently unable to test my implementations of either. The underlying problem is that I cannot see the way to convert plain text into a manipulatabl...

Implicit conversion without assignment?

Preserved question - see Edit at the bottom I'm working on a small functional library, basically to provide some readability by hiding basic cyclomatic complexities. The provider is called Select<T> (with a helper factory called Select), and usage is similar to public Guid? GetPropertyId(...) { return Select .Either(TryToGe...

Subclass/Superclass - If a subclass is cast as its superclass, is there a way to use the overloaded properties of the subclass?

Sorry if the title isn't very clear. This is a VB.NET (2010) question I have a superclass called "Device" which has a number of subclasses that inherit it. Some of those subclasses also have subclasses. In particular, I have a class called "TwinCatIntegerDevice" which inherits "TwinCatDevice" which inherits "Device." The relevant pa...

How to compare percentages that are strings?

I have a string "+1.29%" or "-1.29%" How can I convert this into a float or double so I can compare the values? This is how my comparator class looks like: package org.stocktwits.helper; import java.util.Comparator; import org.stocktwits.model.Quote; public class PercentChangeComparator implements Comparator<Quote> { ...

A design issue with a tree of information in C++.

Sorry in advance for the lengthy explanation! I have a C++ application that uses a hash_map to store a tree of information that was parsed from a text file. The values in the map are either a child hash_map or a string. These values were parsed from a text file and then stored into the map. I wanted to avoid having to send the strings ...

C# value type casting: how it works?

Possible Duplicate: Why does this conversion doesn't work? Hi, i discovered a strange behaviour of the framework. This code throws an exception: byte a = 1; object b = a; Console.WriteLine(b.GetType()); Console.WriteLine((byte)b); Console.WriteLine((int)(byte)b); Console.WriteLine(Convert.ToInt32(b))...

Help using LINQ and XML with Enums

I am using C# with the .NET 3.5 framework to write an application. I have an enum like this, public static enum SettingOneType { FooA, FooB, FooC } I also have an XDocument that I load like this in a Load() method, LoadXML(){ ... XDocument SettingsDocument; if(File.Exists(path) { XElement SettingsElement = new X...

Entity Framework: ObjectSet and its (generics) variance

I use: EntityFramework + POCO Here is the thing: public interface IBaseType { int Id { get; set; } } public class BaseType : IBaseType { public virtual int Id { get; set; } } public class DerivedType : BaseType { } The problem: public class EntityFetcher<T> where T : BaseType { public object GetById(int id) { ...