Yugal Jindle
Yugal Jindle

Reputation: 45666

Can any of these VB.Net snippets be reduced to 1 line?

I have :

Is there some construct or method that can do these in 1 line?

Note:
All the snippets are different and should not be clubbed. These are the situations where I think there might be a smarter way of doing these simple tasks.

Upvotes: 0

Views: 170

Answers (3)

Yugal Jindle
Yugal Jindle

Reputation: 45666

So, found no way to reduce any template.

Used the : trick at some places.. Thanx for that !

Upvotes: 0

Thorarin
Thorarin

Reputation: 48486

Using some LINQ could possibly make some of them use fewer lines, but most of them would not be very readable. You should be writing code for maintainability, not just for getting the job done. If you're thinking about performance: using as few lines as possible is not really a good indication for optimization.

Snippet 1 by itself could be reduced to a single line, assuming wbSheetNames is a List of String:

wbSheetNames.AddRange(WB.Sheets.Select(Function(x) x.Name))

Improvement? Not really. You could remove the list allocation not part of the snippet by using something like:

wbSheetNames = WB.Sheets.Select(Function(x) x.Name).ToList()

This is arguably a slight improvement over the original.

That being said: it looks like you might be able to combine snippet 1 and 2, saving the allocation of a temporary list. The exact intend of the code is not clear to me however. Somewhere along the way you are adding 2 sheets? Otherwise I would think that wb.Sheets(i + 2) will go out of range.

Upvotes: 1

Amr
Amr

Reputation: 1995

You can use the ForEach extension method.

Upvotes: 0

Related Questions