0

How can I return different lists of objects at one method? I have some methods, that returns some types of Lists, and I don't know what Parse method should return

public static [Something here (IList???)] Parse(string filePath)
{
    string[] lines = File.ReadAllLines(filePath);

    for (int i = 0; i < lines.Length; i++)
    {
        string recordId = lines[i].Substring(0, 2);
        switch (recordId)
        {
            case "00":

                return Parse00(lines[i]); //public static List<mainInfo> Parse00(string line);

            case "11":

                return Parse11(lines[i]); //public static List<param> Parse11(string line);

          …  
          …

        }
    }
}
Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44
AlIon
  • 347
  • 5
  • 25
  • Does `mainInfo` inherit from `param`, or vice versa, or do they inherit from some common type? – Lasse V. Karlsen Jul 27 '17 at 09:11
  • @LasseV.Karlsen, no there are different tables at DB – AlIon Jul 27 '17 at 09:14
  • What do you want to do? In your example you are returning only the first line. I assume, that you want to parse all lines, right? – Ben Jul 27 '17 at 09:15
  • You can return `List` or `List` then (or any other collection type), that's about it. Your code isn't altogether clear, however. The first line that matches a case in your switch statement will return something and abort the loop, is this code really representative for what you want to accomplish? – Lasse V. Karlsen Jul 27 '17 at 09:15
  • @LasseV.Karlsen: Your question about inheritance is somewhat irrelevant here. Even if they inherit, a `List` cannot implicitly be cast to a `List`. [See this SO answer for clarification](https://stackoverflow.com/questions/1420581/inheritance-on-a-constrained-generic-type-parameter). I know this is not something you've explicitly stated in your comment, but I infer that that's where you were taking this train of thought to (as would other readers, I assume) – Flater Jul 27 '17 at 09:38
  • What's the problem with IList? – MetaColon Jul 27 '17 at 18:00

4 Answers4

1

In order for this to work, you need a base type that all "records" inherit from. So call it Record:

public class Record
{
    // some shared properties
}

public class ParamRecord : Record
{
    // param-specific properties
}   

public class MainInfoRecord : Record
{
    // main-specific properties
}   

Now you can return a List<Record>.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
1

I would just like to point out to you that you would save some memory if you read lines like this.

var lines = File.ReadLines(fileName); foreach (var line in lines) // Process line

You can learn more about this on What's the fastest way to read a text file line-by-line?.

Furthermore, I would rename your method to ParseFile to avoid confusion and use @CodeCaster approach with creating class hierarchy. Once you have class hierarchy you can write a Factory that would do the parsing for you.

BOR4
  • 610
  • 4
  • 9
0

Use List<dynamic> as a return type. Ex:

    public static List<dynamic> Parse(string filePath)
{
    string[] lines = File.ReadAllLines(filePath);

    for (int i = 0; i < lines.Length; i++)
    {
        string recordId = lines[i].Substring(0, 2);
        switch (recordId)
        {
            case "00":

                return Parse00(lines[i]).ToList<dynamic>; //public static List<mainInfo> Parse00(string line);

            case "11":

                return Parse11(lines[i]).ToList<dynamic>; //public static List<param> Parse11(string line);

          …  
          …

        }
    }
}
tech2avinash
  • 73
  • 2
  • 11
0

You need your mainInfo and param-classes to inherit the same base-class or interface:

interface MyInterface { ... }
class Param : MyInterface { ... }
class MainInfo : MyInterface { ... }

Now you can return lists of the desired types:

string recordId = lines[i].Substring(0, 2);
switch (recordId)
{
    case "00":
        return Parse00(lines[i]); //public static List<mainInfo> Parse00(string line);
    case "11":
        return Parse11(lines[i]); //public static List<param> Parse11(string line);

However your methods need to return Enumerable<MainInfo> or IEnumerable<Param> as List isn´t covariant which means you can´t assign a List<MainInfo> to List<MyInterface>, however you can assign the former to IEnumerable<MyInterface> as IEnumerable is covariant.

Finally as both possible return-values are assignable to IEnumerable<MyInterface> you could use that as return-type for your Parse-method.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111