Visual Basic 6 Dynamic Variables - 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.

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

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.

it appears that the first method
Text1.Text=InputX(2,0)
is working. I just need to declare x and y As Single

Related

Adding array to Chart only plots first value

VB.net - How come only one point is plotted?
Dim ReceivedValue As String ="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20"
Dim myArray = Array.ConvertAll(ReceivedValue.Split(","c), AddressOf Convert.ToDouble)
Chart1.Series(0).Points.Add(myArray)
PS. I got the plot array idea from here
I get the same results as you - one point on the chart. Upon inspecting the chart's Points object, it looks like this:
Showing an array with one X=0, and an array of all the Ys.
If you do it in a loop like this, it works
For Each point In myArray
Chart1.Series(0).Points.Add(point)
Next
Now there is an actual series of points
I'm going to add what I think is more correct, because this results in actual x, y pairs with real x values (you can make them whatever you want)
For i = 0 To myArray.Count - 1
Chart1.Series(0).Points.Add(New DataPoint(i, myArray(i)))
Next

ListBox or Combobox ADODB imported Decimal items invisible in the list

I have encountered this problem several times already and have been able to work around it till now. Also the almighty search engines didn't help me.
The problem is that when I have populated a listbox or combobox from a ADODB recordset all Decimal data elements are not visible in the box, for example with the following (conn is a ADODB connection):
Private Sub GetFilteredRecords()
Dim strSQL As String
Dim arr As Variant
'create the SQL
strSQL = "SELECT * FROM vwStandard_Fee2"
'execute the SQL and fill the rs ( rsFiltered )
Set rsFiltered = conn.Execute(strSQL)
'Apply recordset to the listbox on the form
If Not (rsFiltered.EOF = True And rsFiltered.BOF = True) Then
arr = rsFiltered.GetRows()
With lbDeeper
.ColumnCount = rsFiltered.Fields.Count
.List = TransposeArray(arr)
End With
With cbDeeper
.ColumnCount = rsFiltered.Fields.Count
.List = TransposeArray(arr)
End With
End If
End Sub
Above contains 6 columns of Ids (all show Type = Variant/Decimal), of which the containing values are all not "shown" for some strange reason. Only the String and Date columns are shown normally, the Decimals are there but empty!
Here some snippets:
Now in case of a combo box I can get one column's value shown if their column the BoundColumn when I select that listitem, but only in the value fo the combobox (so still not in the list).
My initial workaround was to convert them into String values before adding to the Listbox/Combobox, in this case however I want to directly link the query result to the Box.List without looking at the details. And thus I am looking for a solution in stead of a work around.
In short: my numerical field items are invisible BY DEFAULT for some strange reason. Workaround was to make the items String values. I am now looking for a solution for this bug/problem instead:
What is causing this?
How to solve it?
So all string data is appearing? And,only numerics don't appear?
Then you may want to convert your numerics to strings and pass it to your list, combo boxes.
Which you have already done I noticed.
Now for any reason if your max number of rows and length of array/recorders row count doesn't match it could also cause an issue. However it seems your setting rows of combobox using recordset row count. Instead of using an array can you try to iterate over the recordset to populate the combobox? yes this is not performance friendly, buy guess what we need it to work without bugs before optimizing. ;-)
Have you bound your combobox to the recordset? Can you confirm if your array is single dimension and it has data to feed to the box?
You may try to populate the listbox using a saved query in the DB to if the issue still persists.
However, list boxes and combo boxes based on SQL statements are slower than
list boxes and combo boxes based on saved queries.
So can you try the following to set rowsource property? Make sure to test on both number,and test columns. As well as on old combo box and new one.
Rowsource->build query->
sqlview copy to rowsource property box->
delete or don't save that above built query since you already have SQL statement.
Just wanted you to try out possibilities to narrow down the issue.
UPDATING ANSWER WITH MOST POSSIBLE ISSUE AND SOLUTIONS
As per my comments, they mainly given assuming you had issues populating listbox/combobox
I forgot to ask something very very important, have you declared
Option Base 1 to make sure to avoid losing one of the array's column
values if you are dumping 2D array...? because you do not have any
explicit declartion for the array you are using to dump data into the
listbox.......... :)
Make sure your Listbox is enabled to show multi column data.
*So you have three choices, *
Option Base 1
ReDim your array and do looping to fill it and dump it into .list.
Since ReDim array need you to anyway loop through, you may just as well
use the recorset iself to add the data.
You seem to have a dimension issue with the array which is not declared but transposed from recordset and then to listbox/combobox. So your undeclared array is not populating multi-columns properly. That could be the reason it works when you declare array proeprly.......
Infact in your comment you have said so,
When I create an array in my code and populate it in my code (entry by
entry) it will show without any problem – K_B 14 mins ago
OK after going through various possible causes it seems to be the case that:
VBA has no Decimal Variant Type of its own.
VBA can handle Decimal from within a variable declared as Variant (thus becoming a Variant/Decimal)
This normally doesn't stop your program from working, but in Controls like Listbox and Combobox the Type Variant/Decimal is not interpretable and thus wont draw that specific entry.
For example populate a listbox called lbHigher with this:
Private Sub ListBoxProblem()
Dim tempArray(2, 2) As Variant
tempArray(0, 0) = "A"
tempArray(0, 1) = 1
tempArray(0, 2) = 1.1
tempArray(1, 0) = "B"
tempArray(1, 1) = CStr(CDec(5.2))
tempArray(1, 2) = 2.3
tempArray(2, 0) = "C"
tempArray(2, 1) = DateSerial(2012, 12, 13)
tempArray(2, 2) = 100
tempArray(3, 0) = "D"
tempArray(3, 1) = -1
tempArray(3, 2) = CDec(5.2)
lbHigher.ColumnCount = 3
lbHigher.List = tempArray
End Sub
Everything works fine except for the CDec(5.2). The CStr(CDec(5.2)) works fine as well as VBA will first have converted the Decimal to String before the Listbox gets to get it.
So either: Dont let the SQL generate any Decimal output OR convert any Decimal output to Single/Double/String/Integer/Long in VBA before handing it to the Listbox.

Change the color for each line

I got a console application that draws line in a 3D CAD program. Now to make this more clear i want to change these lines in different colors.
My code read variables from a text-file and calculates data from it and then makes a lines from this calculated data.
This process gets repeated with every line in the text file wich contains data.
Now i want visual basic to change the color everytinme a new line is drawn, so i get different colored lines.
I tried using a For.. To.. Step method, but this didnt work. I also tried to use the variables from my text file( these are different so when a new line got read the RGB code will change) but this will geve me only a lot of blue colors.
Any suggestions?
EDit:
This is what i use to draw the curves, the RGB code has to cahnge everytime when a line with new data is made:
' Creating a Curve2d object by using the above defined points
objLineString = objLineStrings.AddByPoints(PointCount:=points, points:=dataArray)
objGeometricStyle = objLineString.Style
color = objGeometricStyle.LinearColor
objGeometricStyle.LinearColor = RGB(0,0,0)
What about :
Dim rand As New Random() ' Used to generate random numbers
Dim colors(100) as Integer
' Create the colors
For i as Integer = 0 to 100 Step 1
colors(i) = RGB(rand.Next(256), rand.Next(256), rand.Next(256))
Next
For i As Integer = 0 To 100 Step 1 ' Adjust to your needs
' Creating a Curve2d object by using the above defined points
objLineString = objLineStrings.AddByPoints(PointCount:=points, points:=dataArray)
objGeometricStyle = objLineString.Style
color = objGeometricStyle.LinearColor
objGeometricStyle.LinearColor = colors(i Mod 100) ' Mod returns the remainder of i / 100, so it's always less than 100.
Next
This won't always give you "pretty" colors, but they'll be different for each line. If you want control over the generated colors, you could set up an array of predefined colors and use these in your iteration.

Resizing a two dimensional Array

How can I resize the two dimensional array size without affecting its value?
Use ReDim with the Preserve modifier. VB.NET will make sure the original values are untouched. Didn't read the documentation right. ReDim Preserve will only allow you to change the length of the last dimension of the array.
You need to allocate a new array (with the correct size) and manually copy the elements from the first array to the second.
As Adam said, you can't resize 2D arrays dynamically. You can easily copy the existing array into a bigger one like so:
Dim smaller(1, 1) As Byte
Dim bigger(2, 2) As Byte
Array.Copy(smaller, bigger, smaller.length)
Try using array.resize if you are on .net 2 framework or above.
For example:
Dim MyArray() as string
Array.Resize(myarray,12)

Size of array in Visual Basic?

I've tried this code in VB:
Dim a(1) As Byte
Console.WriteLine(a.Length)
The output is "2". Anyone any idea why?
If you are used to C/C++/C# languages you are used that when declaring an array to initialize it with the number of elements in the array.
C# : byte a[] = new byte[1]
will declare a byte array with 1 element (upperBound = 0)
The behavior is different in VB where, when declaring an array the parameter used in initialization represents the UpperBound of the array.
VB.NET: Dim a(1) As Byte
will declare a byte array with 2 elements (upperBound = 1)
In Visual Basic, the size of an array is declared with the array's upper bound, where most languages declare the size of an array by specifying the number of elements in the array. If you aren't aware of this, then your Visual Basic arrays end up being 1 element longer than you expected:
VB.NET:
Dim a(1) as Byte ' under the hood, translated to byte[2]
Console.WriteLine("{0}", a.Length) ' output 2
a(0) = 7 ' No error, element exists
a(1) = 7 ' No error, element exists, array length is 2
a(a.Length) = 7 ' error: Index was outside the bounds of the array.
C#:
byte[] a = new byte[1];
Console.WriteLine("{0}", a.Length); // output 1
a[0] = 7 // No error, element exists
a[1] = 7 // error: Index was outside of bounds of the array. (because array length is 1)
a[a.Length] = 7; // error: Index was outside the bounds of the array.
The reason why Microsoft designed VB.NET to size arrays based on upper bound rather than array length is to make it easier to port code from VB6 to VB.NET. The initial index of a VB6 array is 1, unless you declare Option Base 0. It was common to loop through an array of size N using For i = 1 To N. By designing VB.NET to interpret an array's sizing argument as an upper bound rather than the number of elements in the array, old VB6 code that looped from 1 to N could be ported directly to VB.NET. The array in VB.NET will have one extra element compared to what the array had in VB6 (the element at index 0) but otherwise behaves as it did in VB6.
You'll sometimes see people claim that Visual Basic creates a "wasted" element. This is only true when porting legacy VB6 code which didn't expect an element at index 0. When writing new code, you just have to remember what the sizing parameter means (upper bound, not element count), and declare your arrays accordingly. Just reduce your sizing parameters by one compared to what you'd see in C#. The resulting array will have elements from a(0) to a(a.Length-1), just like a C# array.
Array starts from position 0. You are defining two positions.
If you want only 1 position, then:
Dim a(0) As Byte
and you will get a.Length as 1.
Dimension Length The index of each
dimension is 0-based, which means it
ranges from 0 through its upper bound.
Therefore, the length of a given
dimension is greater by 1 than the
declared upper bound for that
dimension.
Array Size in Visual Basic
The previous answers each have pieces of the correct answer, but not the full correct answer.
When you declare an array (Like with your code: Dim a(1) As Byte) the number you put in the array declaration (in this case, 1) is NOT a declaration of how many entries in the array, it is a declaration of the upper boundary of the array.
So, in your declaration, you're creating an array with 2 entries: a(0) and a(1)