1

I know templates are common within c/c++ however I am currently trying to translate an old VB code our company uses up to a c# equivalent which brings me to my problem....

VB is an type unsafe language so we come across things like

Public Elements As New Collection

so given the line above I need to translate that to a List.. Given that a Collection can be anything from a List to a map I need to template this as efficiently as possible. I was thinking of for first draft ignoring the map option entirely and just making it a List however from my understanding Templates don't truly exist within C#... The below code is what I came up with so far and while it compiles (I have not gotten to a testing point in the rest of the code yet) I don't know if it would actually work...

Public List<ValueType> Elements = new List<ValueType>();

can anyone offer input on if this would work as a generic list where type is determined at input or if I need to change this so the constructor would look more like

Public List<ValueType> Elements = new List<typeof(foo)>();

if the above is confusing I am sorry it is for me as well, and I will try and clarify as questions come in.

this question is no longer relevant, I was able to go into the source of the calling code and determine what variable types that the lists need to support.

asuppa
  • 571
  • 1
  • 11
  • 27
  • If you want an untyped collection, why don't you use something like `List` ? – Lucas Trzesniewski Jul 02 '14 at 15:55
  • 1
    Is your actual problem is that you can only determine the type of T for a desired `List` object/type during the run-time of your program (based on user input)? If so, then you might want to look here: [Pass An Instantiated System.Type as a Type Parameter for a Generic Class](http://stackoverflow.com/questions/266115/pass-an-instantiated-system-type-as-a-type-parameter-for-a-generic-class) –  Jul 02 '14 at 15:55
  • `type is determined at input` - what input? can you clarify? and yes. C# has full support for generics. – Federico Berasategui Jul 02 '14 at 15:56
  • List would take anything, but if you know that it is of type "foo" just make a List. Could you provide a more specific example with other code for context? – BradleyDotNET Jul 02 '14 at 15:58
  • @asuppa - it would be nice if you could provide some clarification. – antiduh Jul 02 '14 at 22:11
  • @antiduj updated... however no longer relevant I was able to obtain concrete datatypes by pulling apart the original code I am translating and tracing it down. not how I wanted to spend an afternoon but it works. – asuppa Jul 03 '14 at 18:53

0 Answers0