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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
You need the last two items in an array?

If so, to the the next to last one:


To get the last one:


If it's a multi-dimensional array, you'd have to use other methods, however.
cmdolcetAuthor 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.
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Can you show how you're creating the array?
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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
		'' 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

Open in new window

Which produces the following output -Capture.PNG

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trial
cmdolcetAuthor 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)
                            Me.ChartVel.Series(0).Points.AddXY(x, y)
                            If Veldata(intloopVel) >= PeakPoint Then
                                PeakPoint = Veldata(intloopVel)
                            End If
                            If value = 100 Then
                                value = value + 0.5
                                PrgGraphBar.Value = value
                            End If
                            intlooptime = intlooptime + 1
                        End If
                    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

Open in new window

Quite simply:
DistanceVar = tempArray(tempArray.Length - 2)
RateVar = tempArray(tempArray.Length - 1)

Open in new window

This of course assumes that tempArray.Length - 2 and tempArray.Length - 1 are both valid indices.

cmdolcetAuthor Commented:

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

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.