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.
cmdolcetAsked:
Who is Participating?
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:

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.
0
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.
0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Can you show how you're creating the array?
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

it_saigeDeveloperCommented:
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))} }}")

		Console.ReadLine()
	End Sub
End Module

Open in new window

Which produces the following output -Capture.PNG
-saige-
0

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
                            Veldata.Add(tempArray(intloopVel))
                            Veltime.Add(tempArray(intlooptime))
                            x = Veltime(intloopVel)
                            x = 0.000001 * x
                            y = Veldata(intloopVel)
                            lstVelocityChartReadings.Items.Add(tempArray(intloopVel))
                            Me.ChartVel.Series(0).Points.AddXY(x, y)
                            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

Open in new window

0
it_saigeDeveloperCommented:
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.

-saige-
0
cmdolcetAuthor Commented:
It_Sage,

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

-saige-
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.