CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2002
    Posts
    1,798

    [RESOLVED] Another 'var' question

    If you read my previous post, the keyword 'var' is not recognized by my server. After much reading, I have concluded that using 'var' all over the place is a lousy idea. Therefore, I am attempting to eliminate 'var' and substitute the proper type.

    But I am not a very experienced Asp.NET C# programmer, so I have had some difficulty in determining what type should be substituted. Mousiing over the statement sometimes helps, but not always.

    Here is an example of the kind of problem I've run into:
    Code:
            var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);
            //StringReader parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);
            foreach (var htmlElement in parsedHtmlElements)
                document.Add(htmlElement as IElement);
    Ive tried all the obvious types like ArrayList<string>, List<string>, HTMLWorker, and just plain string, but nothing compiles .

    Can anyone please show me how to eliminate the 'var' keyword in the above example and substitute a proper type ?
    mpliam

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Another 'var' question

    Try using GetType() to determine the variables type. Or hover the mouse over the variable in a debugger.

    var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);

    var type1 = parseHtmlElements.GetType( );

    //StringReader parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);

    foreach (var htmlElement in parsedHtmlElements)
    {
    var type2 = htmlElement.GetType( );
    document.Add(htmlElement as IElement);
    }

  3. #3
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Another 'var' question

    Just find out what the methods return (from the docs or hover the mouse as Arjay said), and type in that in instead.

  4. #4
    Join Date
    May 2002
    Posts
    1,798

    Re: Another 'var' question

    Thankyou for your kind help. I wasnt hovering directly over the 'var'. If one does, the Intellisense tells you exactly what the type is. Pretty cool.

    This works:
    Code:
            //var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);
            ArrayList parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);
            //foreach (var htmlElement in parsedHtmlElements)
            foreach (Object htmlElement in parsedHtmlElements)
                    document.Add(htmlElement as IElement);
    Thanks again.
    mpliam

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured