0

Say I have a list of variables like so, ranging from x0 to x1:

x0 = 0.0
x1 = 1.0
x2 = 3
x3 = 9
x4 = 18
x5 = 20
x6 = 30

There is a function within a software package I'm writing the code for which selects a point on a given co-ordinate (the actual function doesnt really matter).

call view.selectCircle(x, y, z, "Set", "Point")

What I want to do is write a loop function which runs this function for all my values of x0-x1. I've tried this but have failed to get it to work...

For i = 1 To 6

call view.selectCircle("x" & i, 0.0, 0.5, "Set", "Point")

Next 

Sorry if this is an extremely basic question as I'm pretty new to programming!

Thanks for any help.

2
  • 1
    Yes you need an array. Do you know how to declare and fill one? Commented Mar 23, 2017 at 14:05
  • You are probably not using VBA but VB.NET. If so, remove the tag VBA and add tag VB.NET so you can get more relevant answers. Commented Mar 23, 2017 at 14:10

1 Answer 1

1

You don't currently have an array, you have 7 separate variables. If you want an array you have two options:

x = Array(0.0, 1.0, 3, 9, 18, 20, 30)

For Each point In x
    Call view.selectCircle(point, 0.0, 0.5, "Set", "Point")
Next

Dim x(0 To 6) As Double

x(0) = 0.0
x(1) = 1.0
x(2) = 3
x(3) = 9
x(4) = 18
x(5) = 20
x(6) = 30

For i = LBound(x) To UBound(x)
    call view.selectCircle(x(i), 0.0, 0.5, "Set", "Point")
Next

Note that the different types of loop aren't relevant to the type of array, I've just used two examples to give you an option - they are interchangeable as far as these solutions are concerned.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, this was very helpful. I have a lot to learn it seems !
@Crescend0 no problem - I'd advise having a read of this for more information. Also don't forget to mark as the answer if it solved your problem (green tick on the left)
Note that if the OP is using VB.NET, using .Length is preferable to LBound and UBound.
@Comintern good point, didn't see the visual-studio tag there which could suggest it isn't VBA. I just came to the question from a VBA feed...
FWIW, it should work in .NET if the Microsoft.VisualBasic namespace is imported.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.