locked
Returning a var type variable RRS feed

  • Question

  • User1302700350 posted

    Hello,

    I am unable to return a var type variable from a method I created. In the method I used a IEnumerable<string> to return to the calling method. I also tried with IEnumerable<files> What is the correct way to return a var type variable. Your help would be much appreciated.

    Thanks,

    Karl

    public static IEnumerable<string>  PutFilesInOrder(string fileDir)
    {
          var files = new DirectoryInfo(HttpContext.Current.Server.MapPath(fileDir)).GetFiles()
                                       .Where(f => f.LastWriteTime <= DateTime.Now)
                                       .OrderBy(f => f.LastWriteTime)
                                       .Select(f => new { f.Name });
          return files;
    }

    Saturday, March 30, 2013 8:54 PM

Answers

  • User-1910946339 posted

    Your Select query should be

    .Select(f => f.Name);

    The code you have is trying to return an IEnumerable<anonymous type>, not  IEnumerable<string>.  The code I have shown will return a IEnumerable<string>.

    By the way, the title of your question shows a fundamental misunderstanding.  There is no such thing as a 'var type variable'.  Think of 'var' as shorthand for 'I don't want to bother typing the correct type, please compiler work it out for me and put it in for me'.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 31, 2013 2:46 AM

All replies

  • User1751268424 posted

    Hi,

    Try this: change return files; to return files.ToList();

    public static IEnumerable<string>  PutFilesInOrder(string fileDir)
    {
          var files = new DirectoryInfo(HttpContext.Current.Server.MapPath(fileDir)).GetFiles()
                                       .Where(f => f.LastWriteTime <= DateTime.Now)
                                       .OrderBy(f => f.LastWriteTime)
                                       .Select(f => new { f.Name });
          return files.ToList();
    }

    Hope can help

    Saturday, March 30, 2013 9:04 PM
  • User-434868552 posted

    @ kiekar01

    check the type of your varialbe files, then change your method signature to match that type.

    g.

    P.S.:  imho, it's bad form to use var on the receiving side of an assignment statement ... once you determine the type for "files", change var to that type.

    Saturday, March 30, 2013 9:05 PM
  • User1302700350 posted

    Hi Jsiahaan,

    Thanks for your reply but unfortunatley your suggestion did not work. I'm still receiving the same error ouput.

    (local variable) IEnumerable <'a> files

    Annoymous types 'a is new { string name}

    Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<string>'. An explicit conversion exists (are you missing a cast?).

    Saturday, March 30, 2013 9:31 PM
  • User-434868552 posted

    @ kiekar01

      var files = new DirectoryInfo(@"c:\Windows\Temp");
      Console.WriteLine (files.GetType());
    
      var files2 = new DirectoryInfo(@"c:\Windows\Temp").GetFiles();
      Console.WriteLine (files2.GetType());
    
      var files3 = new DirectoryInfo(@"c:\Windows\Temp").GetFiles()
                                       .Where(f => f.LastWriteTime <= DateTime.Now) 
                                       .OrderBy(f => f.LastWriteTime) 
                                       .Select(f => new { f.Name }); 
      Console.WriteLine (files3.GetType());

    output:

    typeof (DirectoryInfo)
    typeof (FileInfo[])
    typeof (IEnumerable<>)

    try changing

    public static IEnumerable<string>  PutFilesInOrder(string fileDir) 

    to this:

    public static IEnumerable<>  PutFilesInOrder(string fileDir) 

    and let us know if that helps.

    g.

    Saturday, March 30, 2013 9:51 PM
  • User1302700350 posted

    Hi Gerry,

    Thanks for your help. Unfortunately your suggestion IEnumerable<> caused an error, "Type expected"

     

    Saturday, March 30, 2013 10:43 PM
  • User-1910946339 posted

    Your Select query should be

    .Select(f => f.Name);

    The code you have is trying to return an IEnumerable<anonymous type>, not  IEnumerable<string>.  The code I have shown will return a IEnumerable<string>.

    By the way, the title of your question shows a fundamental misunderstanding.  There is no such thing as a 'var type variable'.  Think of 'var' as shorthand for 'I don't want to bother typing the correct type, please compiler work it out for me and put it in for me'.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 31, 2013 2:46 AM
  • User-134477160 posted

    kiekar01

    Hi,

    Agree with 

    Paul Linton

     What is the correct way to return a var type variable.

    No directly way, "var" cannot be used as a return type.

    But u can:

    1) return object.

    2) return dynamic.

    Sunday, March 31, 2013 3:50 AM
  • User1291589676 posted

    Try as below,

    public static IEnumerable<string> PutFilesInOrder(string fileDir)
    {
         var files = (from v in (new System.IO.DirectoryInfo(fileDir).GetFiles()) where v.LastWriteTime <= System.DateTime.Now orderby v.LastWriteTime select v.Name).ToList();
         return files;
    }



    Sunday, March 31, 2013 4:09 AM
  • User-434868552 posted

    @ ToughMan

    ToughMan

    No directly way, "var" cannot be used as a return type.

    Technically, Karl (kiekar01) is not returning a var .... his return type is IEnumerable<String>:

    public static IEnumerable<string>  PutFilesInOrder(string fileDir)
                  ^^^^^^^^^^^^^^^^^^^

     Paul Linton's sharp eyes have seen the key to solving Karl's issue, new is the cause of Karl's pain:

    .Select(f => new { f.Name });
                 ^^^

    watch what happens when we drop the new:

    var files3 = new DirectoryInfo(@"c:\Windows\Temp").GetFiles()
                                       .Where(f => f.LastWriteTime <= DateTime.Now) 
                                       .OrderBy(f => f.LastWriteTime) 
     /* now, nothing "new" here ==> */ .Select(f =>  f.Name ); // <=== the "new" is gone
    Console.WriteLine (files3.GetType());

    here is the output now:

    typeof (IEnumerable<String>)

    so, now that we know the we get IEnumerable<String>, we SHOULD change var to IEnumerable<String>:

    IEnumerable<String> files3 = new DirectoryInfo(@"c:\Windows\Temp").GetFiles()
    ^^^^^^^^^^^^^^^^^^^

    @ ToughMan:  to clarify, Karl (kiekar01) was expecting that his variable files  through the magic of the c# compiler would match his return type IEnumerable<String>, however, as Paul pointed out, it matched IEnumerable<>.

    g.

    Sunday, March 31, 2013 4:39 AM
  • User-134477160 posted

    gerrylowry

    Many thanks!

    ;)

    Sunday, March 31, 2013 5:30 AM
  • User-1910946339 posted

    While you are fixing the code you can get rid of the files variable altogether.

    return new DirectoryInfo( ... etc ... .Select(f => f.Name);

    If you want to keep the variable leave it as var.  There is no 'should' related to using var or not.  There is personal preference, people can and will argue strongly for both sides.  If you do not know what the type actually is then you can hover your mouse over the word var and let Visual Studio tell you.  But you don't actually have to do that, you know that the type satisfies IEnumerable<string>, otherwise it wouldn't compile!  Typing IEnumerable<string> instead of var is a good idea if you are paid by the keystroke.

     

    Sunday, March 31, 2013 11:11 PM
  • User1302700350 posted

    Thank you all for your expertice.  This forum has such great individuals to take there time to help out others.Smile

    Karl

    Tuesday, April 2, 2013 3:59 PM
  • User-1137493631 posted

    what is the exact data type that DirectoryInfo holding ? instead of assing the values to var you can set the exact data type that DirectoryInfo object holding.

    Tuesday, April 2, 2013 4:03 PM
  • User-434868552 posted

    thirumaran00...

    thirumaran007

    what is the exact data type that DirectoryInfo holding ? instead of assing the values to var you can set the exact data type that DirectoryInfo object holding.

    one technique for determining the exact data type is shown in this thread, above.

    http://forums.asp.net/post/5348102.aspx and also here http://forums.asp.net/post/5348201.aspx.

    the .GetType() method is used.

    Console.WriteLine (files3.GetType());

    once the type is discovered, change var to the actual type.

    g.

    Friday, April 5, 2013 12:39 PM
  • User-1910946339 posted

    Another technique is to hover you mouse over the word var.

    Friday, April 5, 2013 7:53 PM
  • User-134477160 posted

    Another technique is to hover you mouse over the word var.

    Over the variable instead of over the "var";)

    Friday, April 5, 2013 9:13 PM
  • User-1910946339 posted

    Nope., over the var.

    Friday, April 5, 2013 9:16 PM