An alternative to AddRange for a LIST( Of T ) .... where T can be anything you like of course such a

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi ALL,
Here is an extension method you may like for use with VB.Net 2008 / VB.Net 2010 and onwards.
Instead of doing the following:>>

<div style="color:black; background-color:white
<pre> <span style="color:blue Dim newList <span style="color:blue As <span style="color:blue New List(Of <span style="color:blue Integer)
newList.AddRange(list1)
newList.AddRange(list2)
newList.AddRange(list3)
newList.AddRange(list4)

[/code]


<br/>
as you can not write:>>
newList.AddRange(list1).AddRange(list2).AddRange(list3).AddRange(list4)
because AddRange is a SUB not a Function.

Here is a way you can do that in one line:>>

<div style="color:black; background-color:white
<pre> newList = newList.AppendRange(list1).AppendRange(list2).AppendRange(list3).AppendRange(list4)

[/code]


To use the <span style="text-decoration:underline AppendRange Extension method below you could add the MODULE code into a separate module.

From the PROJECT menu select ADD MODULE. Type <span style="text-decoration:underline MyExtensions.Vb in the NAME box
Click on ADD


<br/>
Alternatively, if you want to try this quickly, <span style="text-decoration:underline
paste ALL of the following code to your Form1 code window.
Add the following to your Form1 first please;

1 Button 1 RichTextBox



<div style="color:black; background-color:white
<pre><span style="color:blue Option Strict <span style="color:blue On
<span style="color:blue Option <span style="color:blue Explicit <span style="color:blue On
<span style="color:blue Option Infer <span style="color:blue Off
<span style="color:blue Imports System.Environment

<span style="color:blue Public <span style="color:blue Class Form1

<span style="color:blue Private <span style="color:blue Sub Button1_Click(<span style="color:blue ByVal sender <span style="color:blue As System.Object, <span style="color:blue ByVal e <span style="color:blue As System.EventArgs) <span style="color:blue Handles Button1.Click

<span style="color:blue Dim list1 <span style="color:blue As List(Of <span style="color:blue Integer) = (<span style="color:blue New <span style="color:blue Integer() {1, 3, 5, 7, 9}).ToList
<span style="color:blue Dim list2 <span style="color:blue As List(Of <span style="color:blue Integer) = (<span style="color:blue New <span style="color:blue Integer() {0, 2, 4, 6, 8}).ToList
<span style="color:blue Dim list3 <span style="color:blue As List(Of <span style="color:blue Integer) = (<span style="color:blue New <span style="color:blue Integer() {11, 22, 33, 44, 55}).ToList
<span style="color:blue Dim list4 <span style="color:blue As List(Of <span style="color:blue Integer) = (<span style="color:blue New <span style="color:blue Integer() {111, 222, 333, 444, 555}).ToList

<span style="color:blue Dim newList <span style="color:blue As <span style="color:blue New List(Of <span style="color:blue Integer)

newList = newList.AppendRange(list1).AppendRange(list2).AppendRange(list3).AppendRange(list4)

<span style="color:blue Dim sb <span style="color:blue As <span style="color:blue New System.Text.StringBuilder
<span style="color:blue For index <span style="color:blue As <span style="color:blue Integer = 0 <span style="color:blue To newList.<span style="color:blue Count - 1
sb.Append(newList.Item(index).ToString & NewLine)
<span style="color:blue Next

RichTextBox1.Text = sb.ToString

<span style="color:blue End <span style="color:blue Sub

<span style="color:blue End <span style="color:blue Class

<span style="color:blue Public <span style="color:blue Module MyExtensions

<System.Runtime.CompilerServices.Extension()> _
<span style="color:blue Public <span style="color:blue Function AppendRange(Of T)(<span style="color:blue ByVal aList <span style="color:blue As List(Of T), <span style="color:blue ByVal listToAppend <span style="color:blue As List(Of T)) <span style="color:blue As List(Of T)

aList.AddRange(listToAppend)
<span style="color:blue Return aList

<span style="color:blue End <span style="color:blue Function

<span style="color:blue End <span style="color:blue Module
[/code]


<br/>
You could take this one step further and have an extension method to add;


a List of lists a List of arrays an Array of lists an Array of arrays
or whatever collection types you use often, maybe call it <span style="text-decoration:underline AppendRanges

:-) ;-) :-D

<
<
<h5>Regards,<br/>
<br/>
John</h5>
<
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/33bf2119-5f56-40b2-a689-d437ec09e550" target="blank Click this link to see how to insert a picture into a forum post. <br/>
<br/>
http://www.fortypoundhead.com/showcontent.asp?artid=20502" target="blank Installing VB6 on Windows 7 <br/>
<br/>
http://blogs.msdn.com/b/lisa/archive/2011/03/28/xna-is-coming-to-visual-basic.aspx" target="blank XNA is coming to VB.Net <br/>
<br/>
http://forums.create.msdn.com/forums" target="blank App Hub forums <br/>
<br/>
<
<br/>
<br/>
<br/>

View the full article
 
Back
Top