|
-
Aug 21st, 2015, 01:09 PM
#1
Use LINQ to Initialize 2d Array
I know that I'm able to use LINQ to initialize a 1D array by doing this:
Code:
Dim foo() As Boolean = Enumerable.Range(0, 9).ToList().ConvertAll(Function(i) True).ToArray()
But is there a way that I could do the same thing for a 2D array too?
-
Aug 21st, 2015, 01:35 PM
#2
Re: Use LINQ to Initialize 2d Array
1D:
Code:
Dim foo() As Boolean = Enumerable.Repeat(True, 9).ToArray
2D:
Code:
Dim bools(3, 4) As Boolean
Buffer.BlockCopy(Enumerable.Repeat(True, bools.Length).ToArray, 0, bools, 0, bools.Length)
' proof is in the pudding:
Dim sb As New StringBuilder
For row As Integer = 0 To 3
For col As Integer = 0 To 4
sb.Append(bools(row, col)).Append(" ")
Next
sb.Append(vbCrLf)
Next
MsgBox(sb.ToString)
miiiight be a bit of a cheat. Check the docs for Buffer.BlockCopy and see what you think.
-
Aug 21st, 2015, 02:21 PM
#3
Re: Use LINQ to Initialize 2d Array
I like your 1D example much better than mine, and the 2D.... wow I would've never thought of that!
-
Aug 21st, 2015, 07:03 PM
#4
Re: Use LINQ to Initialize 2d Array
Is LINQ the best approach here? Best as in clearest, easiest to maintain, and fastest? Just wondering.
-
Aug 21st, 2015, 07:26 PM
#5
Re: Use LINQ to Initialize 2d Array
It's a cool trick, but I think I'd use Enumerable.Repeat().ToArray() for 1D and good old fashioned loops for 2D. It certainly wins code golf, but might not pass a code review.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
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
|