-
March 21st, 2011, 07:01 PM
#1
[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
-
March 22nd, 2011, 10:52 AM
#2
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);
}
-
March 22nd, 2011, 05:23 PM
#3
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.
-
March 23rd, 2011, 12:10 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|