# Issues with determining Array Length

How to determine a variable length of an array where I am looking for the last two value in the array. Those last two values are always start with "vad" or  "rate" but the array length size can be variable.
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infotrakker SoftwareCommented:
You need the last two items in an array?

If so, to the the next to last one:

YourArray(YourArray.Length-1)

To get the last one:

YourArray(YourArray.Length)

If it's a multi-dimensional array, you'd have to use other methods, however.
Author Commented:
Given your last statement is true which it is how could I pull out the value in the array.length-1 and array.length?

I need the actual value in the slot and not the index.
Infotrakker SoftwareCommented:
Can you show how you're creating the array?
DeveloperCommented:
Few different ways you can do this -
``````Module Module1
Sub Main()
Dim array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Peter", "Paul", "Mary"}
'' With a loop
For i = array.Length - 1 To array.Length - 2 Step -1
Console.WriteLine(array(i))
Next
'' Using the indexed location
Console.WriteLine(\$"{{ {array(array.Length - 2)}, {array(array.Length - 1)} }}")
'' Using LINQ
Console.WriteLine(\$"{{ {String.Join(", ", array.Skip(array.Length - 2).Take(2))} }}")

End Sub
End Module
``````
Which produces the following output -
-saige-

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
So I am looking at how to discover the values ( DistanceVar,RateVar)
The TempArray is array I am working with but how do I assign the DistanceVar and the RateVar which is always the last two slots in the array.

``````  If tempArray.Length > 0 Then
Me.ChartVel.ChartAreas(0).AxisX.Minimum = 0
Me.ChartVel.ChartAreas(0).AxisX.Title = "Time"
Me.ChartVel.ChartAreas(0).AxisY.Title = "m/s"
For intloopVel = 0 To tempArray.Length - 2
If intloopVel < 255 Then
x = Veltime(intloopVel)
x = 0.000001 * x
y = Veldata(intloopVel)
If Veldata(intloopVel) >= PeakPoint Then
PeakPoint = Veldata(intloopVel)
End If
If value = 100 Then
Else
Application.DoEvents()
value = value + 0.5
PrgGraphBar.Value = value
End If
intlooptime = intlooptime + 1
End If
Next
VelErrorChartCounter = 0
PrgGraphBar.Value = 0
DistanceVar = tempArray(?)
RateVar = tempArray(?)
DistanceVar = DistanceVar.Remove(0, 6)
RateVar = RateVar.Remove(0, 7)
lblPeakValue.Text = PeakPoint
lblVelDistance.Text = DistanceVar
lblVelocityRateData.Text = RateVar
EndIf
``````
DeveloperCommented:
Quite simply:
``````DistanceVar = tempArray(tempArray.Length - 2)
RateVar = tempArray(tempArray.Length - 1)
``````
This of course assumes that tempArray.Length - 2 and tempArray.Length - 1 are both valid indices.

-saige-
Author Commented:
It_Sage,

What do you mean?
DeveloperCommented:
Say tempArray has a length of 1, tempArray.Length - 1 = 0, where as tempArray.Length - 2 = -1.  -1 is an invalid index.

-saige-
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.