joe
joe

Reputation: 11

Visual Basic 6 Dynamic Variables

I am doing a program using vb6 which is stored datas from mouseclick in term of coordinates. I succeeded doing the first stage which is displaying the clicked coordinates. My problem now is I need to save the coordinate in term of variables so i can call them back to use for another purpose for example to find distance between two points.

if its just two coordinates its easier to find the distance. but when it comes to many coordinates I am stuck. I tried to do an array to stored the data inside loop

 1. InputX(ListNum, 0) = Int(x)
 2. InputY(ListNum, 1) = Int(y) 
 3. ListNum=ListNum+1

when I try to call for InputX(2,0) = Text1.Text or Text1.Text=InputX(2,0) none of them working. It seems that the data will be erased after I do a mouseclick

Is there any way which I can set the dynamic variables which stored each my clicked coordinates such as Input1,Input2,Input3 ...InputN

I do this in VB6.

Upvotes: 1

Views: 1079

Answers (3)

joe
joe

Reputation: 11

it appears that the first method

Text1.Text=InputX(2,0)

is working. I just need to declare x and y As Single

Upvotes: 0

Stefanos Kargas
Stefanos Kargas

Reputation: 11053

Dynamic variables in VB6

First you declare the variable without giving size:

Dim InputX() As String

Then you give for the first time size to your array using ReDim:

ReDim InputX(5)

If you want to preserve whatever data is already in your array you use ReDim Preserve:

ReDim Preserve InputX(10)

I hope this is what you need.

Upvotes: 0

John
John

Reputation: 2792

The problem you're having is that you're using a two dimensional array there. A two dimensional array looks like a table. That's not what you want though. You want a list of pairs of points. So, create a structure with two integers in it, x and y, and make an array of those structures:

'Right underneath your Class Form1 declaration:

Structure Point
    Dim x As Integer
    Dim y As Integer
End Structure
Dim length As Integer = 10
Dim Points(length) As Point


'When you want to start using your points put this in the method:

Points(0).x = 10
Points(0).y = 10
Points(1).x = 20
Points(1).y = 40

Upvotes: 1

Related Questions