Search Results

Search found 1008 results on 41 pages for 'generics'.

Page 30/41 | < Previous Page | 26 27 28 29 30 31 32 33 34 35 36 37  | Next Page >

  • Generic Dictionary - Getting Conversion Error

    - by pm_2
    The following code is giving me an error: // GetDirectoryList() returns Dictionary<string, DirectoryInfo> Dictionary<string, DirectoryInfo> myDirectoryList = GetDirectoryList(); // The following line gives a compile error foreach (Dictionary<string, DirectoryInfo> eachItem in myDirectoryList) The error it gives is as follows: Cannot convert type 'System.Collections.Generic.KeyValuePair<string,System.IO.DirectoryInfo>' to 'System.Collections.Generic.Dictionary<string,System.IO.DirectoryInfo>’ My question is: why is it trying to perform this conversion? Can I not use a foreach loop on this type of object?

    Read the article

  • [C#] How do I make hierarchy of objects from two alternating classes?

    - by Millicent
    Here's the scenario: I have two classes ("Page" and "Field"), that are descended from a common class ("Pield"). They represent tags in an XML file and are in the following hierarchy: <page> <field> <page> ... </page> ... </field> ... </page> I.e.: Page and Field objects are in a hierarchy of alternating type (there may be more than one Page or Field to each rung of the hierarchy). Every Field and Page object has a parent property, which points to the respective parent object of the other type. This is not a problem unless the parent-child mechanism is controlled by the base class (Pield), which is shared by the two descended classes (Page and Field). Here is one try, that fails at the line "Pield child = new Pield(pchild, this);": class Pield<T> { private T _pield_parent; ... private void get_children() { ... Pield<Page> child = new Pield<Page>(pchild, this); ... } ... } class Page : Pield<Field> { ... } class Field : Pield<Page> { ... } Any ideas about how to solve this elegantly? Best, Millicent

    Read the article

  • Java generic function for performing calculations on integer, on double?

    - by Daniel
    Is this possible? Surely if you passed in a double, any sort of function implementation code which casts an object to an Integer would not be able to work unless the cast 'Integer' was specifically used? I have a function like: public static void increment(Object o){ Integer one = (Integer)o; system.out.println(one++); } I cant see how this could be made generic for a double? I tried public static <E> void increment(E obj){ E one = (E)o; system.out.println(one++); } but it didn't like it?

    Read the article

  • Using overloaded operator== in a generic function

    - by Dimitri C.
    Consider the following code: class CustomClass { public CustomClass(string value) { m_value = value; } public static bool operator==(CustomClass a, CustomClass b) { return a.m_value == b.m_value; } public static bool operator!=(CustomClass a, CustomClass b) { return a.m_value != b.m_value; } public override bool Equals(object o) { return m_value == (o as CustomClass).m_value; } public override int GetHashCode() { return 0; /* not needed */ } string m_value; } class G { public static bool enericFunction1<T>(T a1, T a2) where T : class { return a1.Equals(a2); } public static bool enericFunction2<T>(T a1, T a2) where T : class { return a1==a2; } } Now when I call both generic functions, one succeeds and one fails: var a = new CustomClass("same value"); var b = new CustomClass("same value"); Debug.Assert(G.enericFunction1(a, b)); // Succeeds Debug.Assert(G.enericFunction2(a, b)); // Fails Apparently, G.enericFunction2 executes the default operator== implementation instead of my override. Can anybody explain why this happens?

    Read the article

  • Generic factory of generic containers

    - by Feuermurmel
    I have a generic abstract class Factory<T> with a method createBoxedInstance() which returns instances of T created by implementations of createInstance() wrapped in the generic container Box<T>. abstract class Factory<T> { abstract T createInstance(); public final Box<T> createBoxedInstance() { return new Box<T>(createInstance()); } public final class Box<T> { public final T content; public Box(T content) { this.content = content; } } } At some points I need a container of type Box<S> where S is an ancestor of T. Is it possible to make createBoxedInstance() itself generic so that it will return instances of Box<S> where S is chosen by the caller? Sadly, defining the function as follows does not work as a type parameter cannot be declared using the super keyword, only used. public final <S super T> Box<S> createBoxedInstance() { return new Box<S>(createInstance()); } The only alternative I see, is to make all places that need an instance of Box<S> accept Box<? extends S> which makes the container's content member assignable to S. Is there some way around this without re-boxing the instances of T into containers of type Box<S>? (I know I could just cast the Box<T> to a Box<S> but I would feel very, very guilty.)

    Read the article

  • Suggestions on Working with this Inherited Generic Method

    - by blu
    We have inherited a project that is a wrapper around a section of the core business model. There is one method that takes a generic, finds items matching that type from a member and then returns a list of that type. public List<T> GetFoos<T>() { List<IFoo> matches = Foos.FindAll( f => f.GetType() == typeof(T) ); List<T> resultList = new List<T>(); foreach (var match in matches) { resultList.Add((T)obj); } } Foos can hold the same object cast into various classes in inheritance hierarchy to aggregate totals differently for different UI presentations. There are 20+ different types of descendants that can be returned by GetFoos. The existing code basically has a big switch statement copied and pasted throughout the code. The code in each section calls GetFoos with its corresponding type. We are currently refactoring that into one consolidated area, but as we are doing that we are looking at other ways to work with this method. One thought was to use reflection to pass in the type, and that worked great until we realized the Invoke returned an object, and that it needed to be cast somehow to the List <T>. Another was to just use the switch statement until 4.0 and then use the dynamic language options. We welcome any alternate thoughts on how we can work with this method. I have left the code pretty brief, but if you'd like to know any additional details please just ask.

    Read the article

  • Decrement all int values in Dictionary

    - by Jon
    I have a Dictionary<string,int> and I simply want to decrement the value in my dictionary by one. I have this but not sure if its best practice. foreach (KeyValuePair<string, int> i in EPCs) { EPCs[i.Key] = i.Value - 1; }

    Read the article

  • Finding the specific type held in an ArrayList<Object> (ie. Object = String, etc.)

    - by Christopher Griffith
    Say I have an ArrayList that I have cast to an ArrayList of objects. I know that all the objects that were in the ArrayList I cast were of the same type, but not what the type was. Now, if the ArrayList is not empty, I could take one of the objects in it and use the instanceof operator to learn what the actual type is. But what of the case where the ArrayList is empty? How do I determine what type Object actually is then? Is it possible?

    Read the article

  • How to return a value based on the type of the generic T

    - by Blankman
    I have a method like: public T Get<T>(string key) { } Now say I want to return "hello" if the type is a string, and 110011 if it is type int. how can I do that? typeof(T) doesn't seem to work. I ideally want to do a switch statement, and return something based on the Type of the generic (string/int/long/etc). Is this possible?

    Read the article

  • Why does Generic class signature requires specifying new() if type T needs instantiation ?

    - by this. __curious_geek
    I'm writing a Generic class as following. public class Foo<T> : where T : Bar, new() { public void MethodInFoo() { T _t = new T(); } } As you can see the object(_t) of type T is instantiated at run-time. To support instantiation of generic type T, language forces me to put new() in the class signature. I'd agree to this if Bar is an abstract class but why does it need to be so if Bar standard non-abstract class with public parameter-less constructor. compiler prompts following message if new() is not found. Cannot create an instance of the variable type 'T' because it does not have the new() constraint

    Read the article

  • (Action<T>).Name does not return expected values

    - by Tomas Lycken
    I have the following method (used to generate friendly error messages in unit tests): protected string MethodName<TTestedType>(Action<TTestedType> call) { return string.Format("{0}.{1}", typeof(TTestedType).FullName, call.Method.Name); } But when I call it as follows, I don't get the expected results: var nm = MethodName<MyController>(ctrl => ctrl.Create()); After running this code, nm contains "<Create_CreateShowsView>b__8", and not (as expected) "Create". How should I change the code to obtain the expected result?

    Read the article

  • How can I define multiple types with the same name and different type parameters using Reflection Em

    - by wawa
    How can I generate types like these using the System.Reflection.Emit libraries: public class Test<T> {} public class Test<T1, T2> {} When I call ModuleBuilder.DefineType(string) with the second type declaration, I get an exception because there is already another type in the module with the same name (I've already defined the type parameter on the first type). Any ideas?

    Read the article

  • Generic object comparison diff routine

    - by MicMit
    The question stems from database tables comparison. Let's say we put left row in the instance Left and the right one into instance Right of the same type. And we'got many tables and respective types. How to implement more or less generic routine resulting in a collection of diffs e.g. propertyName , leftValue , rightValue for each such a pair of instances of the same type.

    Read the article

  • implict type cast in generic method

    - by bitbonk
    why do I get a compiler error in the following code stating: Cannot implicty convert type SpecialNode to T even though T must derive from NodeBase as I defined in the where clause and even though SpecialNode actually derived from NodeBase? public static T GetNode<T>() where T : NodeBase { if (typeof(T) == typeof(SpecialNode)) { return ThisStaticClass.MySpecialNode; // <-- compiler error } if (typeof(T) == typeof(OtherSpecialNode)) { return ThisStaticClass.MyOtherSpecialNode; // <-- compiler error } ... return default(T); }

    Read the article

  • Call Generic method using runtime type and cast return object

    - by markpirvine
    I'm using reflection to call a generic method with a type determined at runtime. My code is as follows: Type tType = Type.GetType(pLoadOut.Type); MethodInfo method = typeof(ApiSerialiseHelper).GetMethod("Deserialise", new Type[] { typeof(string) }); MethodInfo generic = method.MakeGenericMethod(tType); generic.Invoke(obj, new object[] { pLoadOut.Data }); This works ok. However the generic.Invoke method returns an object, but what I would like is the type determined at runtime. Is this possible with this approach, or is there a better option? Mark

    Read the article

  • Is it possible in Scala to force the caller to specify a type parameter for a polymorphic method ?

    - by Alex Kravets
    //API class Node class Person extends Node object Finder { def find[T <: Node](name: String): T = doFind(name).asInstanceOf[T] } //Call site (correct) val person = find[Person]("joe") //Call site (dies with a ClassCast inside b/c inferred type is Nothing) val person = find("joe") In the code above the client site "forgot" to specify the type parameter, as the API writer I want that to mean "just return Node". Is there any way to define a generic method (not a class) to achieve this (or equivalent). Note: using a manifest inside the implementation to do the cast if (manifest != scala.reflect.Manifest.Nothing) won't compile ... I have a nagging feeling that some Scala Wizard knows how to use Predef.<:< for this :-) Ideas ?

    Read the article

  • Generic type parameter naming convention for Java (with multiple chars)?

    - by chaper29
    In some interfaces i wrote I'd like to name generic type parameter with more than one character to make the code more readable. Something like.... Map<Key,Value> Instead of this... Map<K,V> But when it comes to methods, the type-parameters look like java-classes which is also confusing. public void put(Key key, Value value) This seems like Key and Value are classes. I found or thought of some notations, but nothing like a convention from sun or a general best-practice. Alternatives i guesed of or found... Map<KEY,VALUE> Map<TKey,TValue>

    Read the article

  • Why does my WCF service return and ARRAY instead of a List <T> ?

    - by user193189
    In the web servce I say public List<Customer> GetCustomers() { PR1Entities dc = new PR1Entities(); var q = (from x in dc.Customers select x).ToList(); return q; } (customer is a entity object) Then I generate the proxy when I add the service.. and in the reference.cd it say public wcf1.ServiceReference1.Customer[] GetCustomers() { return base.Channel.GetCustomers(); } WHY IS IT AN ARRAY? I asked for a List. help.

    Read the article

  • List of Lists of different types

    - by themarshal
    One of the data structures in my current project requires that I store lists of various types (String, int, float, etc.). I need to be able to dynamically store any number of lists without knowing what types they'll be. I tried storing each list as an object, but I ran into problems trying to cast back into the appropriate type (it kept recognizing everything as a List<String>). For example: List<object> myLists = new List<object>(); public static void Main(string args[]) { // Create some lists... // Populate the lists... // Add the lists to myLists... for (int i = 0; i < myLists.Count; i++) { Console.WriteLine("{0} elements in list {1}", GetNumElements(i), i); } } public int GetNumElements(int index) { object o = myLists[index]; if (o is List<int>) return (o as List<int>).Count; if (o is List<String>) // <-- Always true!? return (o as List<String>).Count; // <-- Returning 0 for non-String Lists return -1; } Am I doing something incorrectly? Is there a better way to store a list of lists of various types, or is there a better way to determine if something is a list of a certain type?

    Read the article

< Previous Page | 26 27 28 29 30 31 32 33 34 35 36 37  | Next Page >