generics

Delphi 2009 generics compilation problem

I'm checking out the Delphi 2009 Trial, but run into problems with the generics stuff right away. The following code does not compile, and I haven't the slightest idea why it's giving me E2015 for the Equals() method: type TPrimaryKey<T> = class(TObject) strict private fValue: T; public constructor Create(AValue: T); ...

Delegate calling generic method inside of a generic class pegs CPU during deserialization

Has anyone else run into this problem before? I've got a method that calls a generic method with a delegate, inside of a generic class. I've marked the class as Serializable, and it serializes without complaint. But, when I try to deserialize an object of this class, it pegs the CPU and hangs the machine. Code example: public delegate ...

Type parameters cannot be used as qualifiers

VB.Net2005 Simplified Code: MustInherit Class InnerBase(Of Inheritor) End Class MustInherit Class OuterBase(Of Inheritor) Class Inner Inherits InnerBase(Of Inner) End Class End Class Class ChildClass Inherits OuterBase(Of ChildClass) End Class Class ChildClassTwo Inherits OuterBase(Of ChildClassTwo) End Clas...

How can I databind to properties not associated with a list item in classes deriving List<T>

Previously, I had a class that wrapped an internal System.Collections.Generic.List<Item> (where Item is a class I created). The wrapper class provided several collection-level properties that provided totals, averages, and other computations on items in the list. I was creating a BindingSource around this wrapped List<> and another Bin...

call a Method with parameters got from generic Method

Hi, I have a class storing the name of a WS method to call and the type and value of the only parameter that service receives (it will be a collection of parameters but lets keep it simple for the example): public class MethodCall { public string Method { get; set; } public Type ParType { get; set; } public string ParValue { get; ...

TStringList vs. TList<string>

Hello, what is the difference in using a standard type sl: TStringList compared to using a generic TList type sl: TList<string> ? As far as I can see, both behave exactly the same. Is it just another way of doing the same thing? Are there situations where one would be better than the other? Thanks! Holger ...

how to pass an event from a child object in a generic list to the parent?

hi, here is my example code: Public Class Parent Private _TestProperty As String Private WithEvents _Child As IList(Of Child) Public Property Test() As String Get Return _TestProperty End Get Set(ByVal value As String) _TestProperty = value End Set End Property ...

Generically typed expression weirdness

The code below gives an error: Property 'Int32 Key' is not defined for type 'ConsoleApplication1.IKeyed`1[TKey]' when the expression e is created but is fine when func f is created, can anyone explain why and if there is a way to fix it? Module Module1 Sub Main() Dim g = New keyedThingGetter(Of KeyedThing, Integer) ...

Pattern for specialization of generic class in C#?

In C# I sometimes wish I could make special methods for certain "instantiations" of generic classes. UPDATE: The following code is just a dumb example of a more abstract problem - don't focus too much on time series, just the principles of "adding extra methods" for certain T. Example: class Timeseries<T> { ... TimeSeries<T>...

"Constraints for explicit interface implementation..."

Hi guys I can't figure out why the following wont work, any ideas?? public interface IFieldSimpleItem { } public interface IFieldNormalItem : IFieldSimpleItem { } public class Person { public virtual T Create<T>() where T : IFieldSimpleItem { return default(T); } } public class Bose : Person { ...

How to get a ReadOnlyCollection<T> of the Keys in a Dictionary<T, S>

My class contains a Dictionary<T, S> dict, and I want to expose a ReadOnlyCollection<T> of the keys. How can I do this without copying the Dictionary<T, S>.KeyCollection dict.Keys to an array and then exposing the array as a ReadOnlyCollection? I want the ReadOnlyCollection to be a proper wrapper, ie. to reflect changes in the underl...

Iterate though Generic List in C#

public class Item { private int _rowID; private Guid _itemGUID; public Item() { } public int Rid { get { return _rowID; } set { } } public Guid IetmGuid { get { retu...

Java: Using generics to implement a class that operates on different kinds of numbers.

So, let's say I want to write a class that operates on different kinds of numbers, but I don't a priori know what kind of numbers (i.e. ints, doubles, etc.) I will be operating on. I would like to use generics to create a general class for this scenario. Something like: Adder<Double> adder = new Adder<Double>(); adder.add(10.0d, 10....

Am I missing something, or do varargs break Arrays.asList?

private void activateRecords(long[] stuff) { ... api.activateRecords(Arrays.asList(specIdsToActivate)); } Shouldn't this call to Arrays.asList return a list of Longs? Instead it is returning a List<long[]> public static <T> List<T> asList(T... a) The method signature is consistent with the results, the varargs throws th...

Importance of closing SQLConnection objects when using the SQLDataReader object.

My present contract engagement is at a large E-Commerce company. Their code base which has origins going back to .Net 1.0 has caught me by surprise to contain many issues that raise the level of smell beyond the last crap I took. That notwithstanding and trying to diffuse my level of distraction from it, I go along merrily trying to ad...

Way to define List<> of two elements string array?

I want to build two-dimentional array of strings where length of one dimention is 2. Similar to this string[,] array = new string[,] { {"a", "b"}, {"c", "d"}, {"e", "f"}, {"g", "h"} } Doing List<string[]> list = new List<string[]>(); list.Add(new string[2] {"a", "b"}); list.Add(new string[2] {"c", "d"}); list.Add(new...

Casting problem in C# generic method

I'm having some trouble with a generic method I'm writing. It has the following signature; public static ThingCollection<T> GetThings<T>(...) where T : Thing There are several classes; ThingA, ThingB and ThingC that inherit from Thing; and I want to be able to have code something like this in the method. var things = new ThingCollec...

Introducing generics to Java code without breaking the build

The following code does not compile: public class GenericsTest { public static void main(String[] args) { MyList<?> list = new MyList<Object>(); Class<?> clazz = list.get(0); // Does not compile with reason // "Type mismatch: cannot convert from Object to Class" MyList list2 = new MyList...

Generics problem

Why EAccessViolation is raised? uses Generics.Collections; ... var list: TList<TNotifyEvent>; ... begin list := TList<TNotifyEvent>.Create(); try list.Add(myNotifyEvent); list.Remove(myNotifyEvent); // EAccessViolation at address... finally FreeAndNil(list); end; end; procedure myNotifyEvent(Sender: TObj...

ListView Shows Repeated Elements When the Underlying Generic List Does Not Contain Repeated Elements

I am binding a generic List to an <asp:ListView /> control to display a tag cloud. The elements in the list are Tag objects, where Tag is basically just something like: public class Tag { public string Name { get; set; } public int Total { get; set; } } I create the List<Tag> object and then bind it to a ListView but when the ...