Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Declare list variable and set type later? 1

Status
Not open for further replies.

hnrg

Programmer
Jul 19, 2010
5
US
I am trying to create a list variable and set its type later.

So, Is something like this possible?

List<IList> lst;

switch (file.FileName.ToLower())
{
case "datsal.xls":
lst = new List<MyType1>();
break;
case "logorg.xls":
lst = new List<MyType2>();
break;
case "datcst.xls":
lst = new List<MyType3>();
break;
}
 
no, generic types are declared at compile time, not run time.
you could do this
Code:
var list = new List<object>();
list.Add(1);
list.Add(2);
list.Add(3);
var numbers = list.Cast<int>();

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Thanks,

Maybe I should write my own compiler :)
 
or just use a dynamic language like php, ruby or phyton.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Ah, I would, if companies would allow me. Most companies use .net or java and I am currently stuck with a .net language. so C# it is.
 
as an aside. if the types are so different you cannot extract a super class then i would solve the problem using completely separate objects. you can always use the command pattern to encapsulate exactly what is happening. example
Code:
interface Command
{
   void Execute();
}
class ProcessDatSal : ICommand
{
// processs datal.xls
}
class ProcessLogOrg : ICommand
{
// processs logorg.xls
}
class ProcessDatCst : ICommand
{
// processs datcst.xls
}
this removes the need for the switch statement and isolates the details. this meets a couple good design principles.
1. encapsulate what changes
2. separation of concern
3. open/closed principle

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Thanks,

I have a question,

What would be the best way to do the following validation with items in a list perhaps against an array?

List ("A")
List ("B")
List ("C")

The list must contain the 3 items "A" "B" "C" (there cannot be any duplicate names)

I add filenames to a list from a file upload control, which gets processed later....
 
in some form or another....
list.Count == 3;
list.Distinct() == list;
I like to create Rules
Code:
interface Rule<T>
{
   string Message {get;}
   bool IsValidFor(T item);
}
class ListHasProperCount : Rule<IEnumerable<string>>
{
   public string Message {get return "must have 3 items";}
   public bool IsValidFor(IEnumerable<string> item)
   {
      return item.Count() == 3;
   }
}
class ListIsDistinct : Rule<IEnumerable<string>>
{
   public string Message {get return "list cannot contain duplicates";}
   public bool IsValidFor(IEnumerable<string> item)
   {
      return item.Count() == item.Distinct().Count();
   }
}
[code]
validation becomes
[code]
var Rules = new[]{array of rules};
var messagesForBrokenRules = rules
   .Where(rule=>rule.IsValidFor(list) == false)
   .Select(rule = rule.Message)
   .ToArray();
//display messages to user.
I would validate the item isn't in the list before adding it as well.
Code:
if(list.Contains(filename)) 
{
   //notify user the item already exists.
   return;
}
list.Add(filename);
//notify user the item was added.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top