Retrieving Sub Array...

JaedenRuiner

Well-known member
Joined
Aug 13, 2007
Messages
340
Programming Experience
10+
Okay,

I've looked, but the only ones I see don't quite do the same thing. This is what I came up with in my code that does exactly what I want and the way I want it. is there a Parallel that will do this but just directly from an array:

VB.NET:
dim cols() as string = GetSomeStringArray(<blah>)

ThisFunctionNeedsArrayInput(Split(String.Join(",", cols, 0, cols.Length - 1), ","))
The Function I'm calling needs an Array of strings, that I get from the GetSomeStringArray() function. However, I need ALL of the Cols() array for other operations before and after this call, but for this specific call i want the sub array (all items except the last one). The Array.Copy/CopyTo require another parameter etc, etc, and frankly I don't feel like using a temp variable for just this one call. is there anyway to do a subarray similar to the String.Substring(), or how the String.Join() can take a start/length parameter, and just have an array do Array.SubArray(myarray, 0, 3)

Thanks
 
The List GetRange method can be used:
VB.NET:
Dim range = (New List(Of String)(stringarray)).GetRange(index, count).ToArray
You can also create a GetRange Extension method for arrays that use this functionality:
VB.NET:
Module extensions
    <System.Runtime.CompilerServices.Extension()> _
    Function GetRange(Of T)(ByVal items() As T, ByVal index As Integer, ByVal count As Integer) As T()
        Return (New List(Of T)(items)).GetRange(index, count).ToArray
    End Function
End Module
VB.NET:
Dim range = stringarray.GetRange(index, count)
 
System.Runtime.CompilerServices.Extension()
Doesn't exist.

The Extension() attribute is not found, and I've tried both Importing the System.Runtime.CompilerServices or putting in as above directly. I found this attribute on the msdn help as well, but why vb 2008 express on .net 3.5 is not seeing it I don't know.

<edit>

Okay, i got it now. It's that System.Core.dll file that needs to attached. The funny thing is, is that I'm doing this all in an external assembly for my application. There are quite a few things in that repository, and for some reason, most of the Extensions don't work. Like Array.ToList(), and string.First() I don't know what happened between the App project and the Class Library Assembly project, or whatever, but I am apparently missing something.

thanks
 
Last edited:
No, the extension works fine in a new default class library, Core is included by default. Forms projects only add Forms+Drawing+Deployment references compared to class lib projects. You can create a new project and check with the References & Imports page in project properties to see which are default if you happened to remove some by accident.
 
Back
Top