VB: Declaring variables in For loop - vb6

I have been trying to declare variables inside a For loop for quite some time now, and I just haven't been able to find a way.
I'm attempting to create a new image (tile) for every time a certain number is encountered in a two-dimensioned array (Measuring 32x16). I may need to add in that I am using Visual Basic 6.
Currently I'm using the following code:
Option Explicit
Dim wCount As Integer
Dim hCount As Integer
Dim arrTiles(31, 15) As Integer
Private Sub Form_Load()
For wCount = 0 To 31 Step 1
For hCount = 0 To 15 Step 1
' -Declare variables
' -I.E. Dim NAME As Image
Next
Next
End Sub
However, the above code (Using Dim tile1 As Image) gives me an error whenever trying to access one of the properties of the newly added image (Such as tile1.Width).
Is there any way to declare a variable this way at run-time?
Sincerely
- Birjolaxew

You must assign a valid Image object to the Image variable before you try to access any properties of it. For example, this works fine:
For wCount = 0 To 31 Step 1
For hCount = 0 To 15 Step 1
' -Declare variables
Dim tile1 As Image
tile1 = Image.FromFile("c:/test.png")
Dim width = tile1.Width
Next
Next

Related

Assigning CSV values to structure

I'm creating what should be a simple program but I'm having some difficulty assigning values from a file into a structure and it's variables. Visual Basic.
Structure:
Public Structure Teams
Dim teamName As String
End Structure
Function:
Function getAvailableTeams() As Teams()
Dim rec As Teams
Dim index As Integer
Dim recCount As Integer = 0
'Count how many teams exist
FileOpen(1, "teamConfig.csv", OpenMode.Input)
Do Until EOF(1)
LineInput(1) 'Read document line by line
recCount += 1 'Increment team count by 1
Loop
'store team names in array
Dim teamNames(recCount - 1) As Teams
index = 0
Do Until EOF (1)
Input(1, rec.teamName)
teamNames(index).teamName = rec.teamName
index +=1
Loop
FileClose(1)
Return teamNames
End Function
Simple subroutine to test values are available and being picked up.
Dim availableTeams() As Teams
availableTeams = getAvailableTeams()
lbltest.text = availableTeams(1).toString
The file is stored as a CSV file and there are 11 available team names.
team1 \r\n
team2 \r\n
etc...
I appreciate this is probably something simple but I can't work out where I'm going wrong with this.
One of the comments was on the right track. You need to close and re-open the file for input to start at the beginning again. Since you were already at end-of-file, the second attempt fails immediately unless you re-start from the beginning.

Iterate over all selected rows of SSDBGrid to fill an array

I need to iterate over all of the selected rows in an SSDBGrid. Then, I need to get the value in the current row and populate the relevant place in the array with this value.
I've been attempting to do this with the code below:
Dim i As Integer
i = 0
Dim nomCode(Grd_Nominal.SelBookmarks.Count) As String ' This is my array.
Do While Grd_Nominal.SelBookmarks <> 0
nomCode(i) = Grd_Nominal.SelBookmarks(0)
If Grd_Nominal.SelBookmarks.Count > 0 Then
Grd_Nominal.SelBoomarks(0).Remove
End If
i = i + 1
Loop
However, nomCode(i) is always being filled as nomCode(i) = "??"
Why is it inserting "??", and how can I fix this to insert the value of the current row?
You need to first of all re-think how you're declaring your array.
Dim nomCode() As String
ReDim nomCode(Grd_Nominal.SelBookmarks.Count - 1) As String
This is because when declaring an array you need to pass in a constant as the length. ReDim doesn't, so this will go partly towards solving the issue.
Dim x As Integer
Dim bk As Variant
For x = 0 to Grd_Nominal.Rows.Count - 1
bm = Grd_Nominal.SelBookmarks(x)
nomCode(x) = Grd_Nominal.Columns("Your_Column").CellValue(bm)
Next
This should sort the rest of the issue, I think.

I am trying to get 6 separate input numbers from the user for my program using one textbox

I have been trying to use a textbox in VB6 to acquire 6 different random numbers chosen by the user to use in part of my program. I have tried many different approaches in different areas of the code. Best I have gotten so far is in the keypress section. I set up an array to store the 6 entries buy fail to be able to get past storing one number in the first element. I'm using a for next loop to cycle through the elements but the textbox.setfocus will not work properly in the loop. I clear the box then setfocus after I assign the number to an element in the array. The only other way I'm thinking this would work is to hide the single box and show 6 hidden ones and then they can tab or I can hide each one again as they fill up.
Here's a snip of that code so one should get an idea what I'm trying to do here.
Private Sub Inbox_Keypress(KeyAscii As Integer)
Select Case KeyAscii ' Determine keypress
Case vbKey0 To vbKey9 'Only the numbers
Case vbKeyBack, vbKeyClear, vbKeyDelete ' Accept these keys
Case vbKeyLeft, vbKeyRight ' Arrow keys
Case 13 ' Enter
If GenOpt(5).Value = True Then
For c = 1 To 6
Gen6_user(c) = Inbox.Text
'Select Case KeyAscii
' Case 13
Talkbox.Caption = "Please enter the next user number for Generator 6"
'Case Else
' MsgBox Msg, style, title
'End Select
If c = 6 Then Exit For
Do
Inbox.Text = ""
Inbox.SetFocus
Loop Until KeyAscii = 13
Next
For c = 1 To 6
Msg6.Print Gen6_user(c)
Next
Else
User_number = Inbox.Text ' Assign any final value to User_number variabl)
Generate.SetFocus
'Generate_Click ' Call generate function
End If
Any help would be appreciated
Journey
I am at work and I don't have VB6 handy here... but, this should work (maybe with a few little tweaks, I am sure you can figure those out):
Dim x As Integer = 1
Dim myArray(6) As Integer
Private Sub TextBox1_KeyUp(KeyCode As Integer)
If KeyCode = 13 Then
myArray(x) = TextBox1.Text
x = x + 1
TextBox1.Text = ""
End If
If x = 6 Then
Debug.Print("Here you check your numbers?")
End If
End Sub

Non repeating positive random numbers, over a given range

am generating a game using vb 6.0, at one face i need to generate non repeating random numbers between 1 and 100. am using the following code for generating the random numbers
dim n(10) as integer
for i=0 to 9
n(i)=round(rnd*100)
next i
the code is within a for loop hence it generate 10 Nos. randomly, but it includes repeating numbers and also '0', is their any suggestion to avoid repeating numbers and '0'
then output of my code is:
n()={42,14,10,22,5,42,12,0,59,72}
the numbers 42 is appear two times in the array and 0 cannot be avoided
thanks in advance
Here is a simple technique
Dim n(10) As Integer
Dim choice(100) As Integer
' Create a list of possible numbers
For i = 0 To 100
choice(i) = i
Next
' Populate the array with unique numbers
For i = 1 To 10
lastix = 101 - i
' Find one that has not been selected
ix = Round(Rnd * lastix)
' Assign it
n(i) = choice(ix)
' Replace with one that has not been used
choice(ix) = choice(lastix)
Next
You can use the same technique for shuffling cards.
To avoid 0, multiply by 99 and add 1. To avoid duplicates, keep track of what you generated and retry if you get a duplicate. (Since you need only a few numbers. If you need many, shuffle an array of all possible outcomes and take the initial members.)
the solution below is not the fastest, but makes up by that for being easy ...
it uses a hidden listbox control which contains the required values and retreives a random one every time
the values in this example are just the squared numbers of the index
run the project and click the command button to see what happens. it should show messageboxes with the square numbers in random order
'1 form with:
'1 listbox control : name=List1
'1 command button : name=Command1
Option Explicit
Private Sub Command1_Click()
Dim intIndex As Integer
'generate a new random sequence every time
Randomize Timer
'loop through the list and retreive 1 random value at a time
With List1
Do While .ListCount > 0
intIndex = Int(Rnd * .ListCount)
MsgBox .List(intIndex)
'remove the used item from the list
.RemoveItem intIndex
Loop
End With 'List1
End Sub
Private Sub Form_Load()
Dim intIndex As Integer
'hide listbox
List1.Visible = False
'fill listbox with values (squares)
List1.Clear
For intIndex = 1 To 10
List1.AddItem CStr(intIndex * intIndex)
Next intIndex
End Sub
btw i am just using 10 numbers so you dont have to click through 100 messageboxes :)

How to get current code row number in vbs?

For example, I have a next code:
1. Dim a
2. Dim b
3. Dim c
4. Dim currentRow
5. a = 5
6. b = 9
7. c = a + b
8. currentRow = ??? 'There i need to get code row
9. MsgBox currentRow
So I need to get 8
The language does not provide this kind of introspection/reflection. So your options are:
Use a different kind of unique location identifier (Keyword, description, global counter)
Preprocess your .VBS files to insert/replace the current line number where needed
Execute your code in the Microsoft ScriptControl and make use of the .Line property of the Error object

Resources