473,320 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Inserting variable values into control names

I am creating a 'frogger' like game. Essentially there are ten lanes each with an image array associated with it. The arrays are named from imglane0() to imglane9(). I was creating a piece of code that moved the img boxes in the array by random amounts whilst checking for collisions etc. and it worked, for one lane. I designed a module that would theoretically allow the movement of every lane without having to constantly repeat lines of code. The form passes on the lane number, the width of the images associated with that array and the number of controls within the array into a public sub within the module. the module uses these variables to do the above mentioned moving etc. in the specified lane. The problem is this, i am able to use a variable in place of an index in a control array name however i do not know how to use variables in a similar way to dynamically specify a control name without using needless amounts of select case statements.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Sub movement(ilane as integer, icount as integer, iwidth as integer)
  3.  
  4. 'ilane = lane number, icount = number of controls in array, iwidth = width of control
  5.  
  6. For i = 1 To icount
  7. 'i 1 is effectively just the index number of the img box that is behind img box i
  8.     i1 = i
  9.         If i1 < 0 Then
  10.             i1 = icount
  11.         End If
  12. 'check for a collision between the images, what it does is irrelevant, only the syntax is troublesome
  13.     If imglane[ilane]([i1]).Left >= imglane[ilane]([i]).Left + iwidth Then
  14.         imglane[ilane]([i]).Left = imglane[ilane]([i]).Left - 30
  15.     End If
  16.     imglane[ilane]([i]).Left = imglane[ilane]([i]).Left - Rnd * 10 + 1
  17. Next i
  18. For i = 0 To icount
  19.     If imglane0([i]).Left <= 720 Then
  20.     imglane0([i]).Left = 8280
  21.  
  22.     End If
  23. Next i
  24. End Sub
  25.  
That which is in bold causes me trouble, expects then or goto. essentially i need the syntax so that if ilane = 1 then the left property of control imglane1([i1]) would be used. using the variables to specify the index in these cases works fine.
Aug 20 '07 #1
5 1739
hariharanmca
1,977 1GB
I am creating a 'frogger' ... variables to specify the index in these cases works fine.

Okay,
Expand|Select|Wrap|Line Numbers
  1. If imglane[ilane]([i1]).Left >= imglane[ilane]([i]).Left + iwidth Then

Can you post, what error it throwing. or just explain it in just 3 or 4 points.
(Unable to get such a long sentence).
Aug 20 '07 #2
Killer42
8,435 Expert 8TB
Hm... interesting question.

A reference using the name in a string like this is quite simple, using the Controls collection. However, I don't know how you'd go about using that in conjunction with a control array.

For example...
Expand|Select|Wrap|Line Numbers
  1. Debug.Print Me.Controls("imglane1").Left
This syntax can be used to refer to a single (non-array) control. We might have to play around with this, and see what we can come up with.
Aug 20 '07 #3
Killer42
8,435 Expert 8TB
Of course, one way to get around it would be to assign them as one big array, and simulate a two-dimensional array.

In fact, you could even create a two-dimensional array in code and set the elements to reference the controls in the 1D array on the form. Seems as though a 2D array would be more convenient to work with - you could just refer to it as imgPosition(LaneNumber, PositionInLane).

For example, let's say you had three lanes with ten possible positions in each. You can define them all as one big control array at design time (or runtime) then in your code, at startup, you could do something like this...

Expand|Select|Wrap|Line Numbers
  1. Dim LaneNum As Long, PlaceNum As Long
  2. ReDim LanePlace (1 To maxLanes, 1 To maxPlaces) As ImageBox
  3. Dim I As Long
  4. For LaneNum = 1 To maxLanes
  5.   For PlaceNum = 1 To maxPlaces
  6.     Set LanePlace(LaneNum, PlaceNum) = TheRealImageBox(I)
  7.     I = I + 1
  8.   Next
  9. Next
From then on, you would always use the two-dimensional LanePlace() array to refer to them.

What do you think?
Aug 20 '07 #4
Is the purpose of the code you posted to create and then populate the array based upon the original image box so that you can just use a loop on the giant array rather than worrying about individual 1d arrays?
Aug 21 '07 #5
Killer42
8,435 Expert 8TB
Is the purpose of the code you posted to create and then populate the array based upon the original image box so that you can just use a loop on the giant array rather than worrying about individual 1d arrays?
My code was written with the assumption that you had already created the imagebox controls as a control array at design time. It just sets up a 2D array, each element of which is given a reference to one of the "real" controls.

You're correct, the purpose of this is so that you can "just use a loop on the giant array rather than worrying about individual 1d arrays". I believe a 2D array, while requiring a little work to set up at program startup, will make your code much simpler.

If you like, you could create just a single control at design time (set it's Index property to 0) then Load the rest at runtime. Is that what you had in mind?
Aug 21 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Marko Poutiainen | last post by:
Situation: We had to make our SQLServer 2000 database multi-lingual. That is, certain things (such as product names) in the database should be shown in the language the user is using (Finnish,...
2
by: altergothen | last post by:
Hi there I am a newbie to ASP.Net - Please Help! I am trying to insert the values of my variables into a database. If I try the following it works perfectly: string insertQuery = "INSERT into...
6
by: 35th Ave Media | last post by:
Hello, I have about 60+ pages that I need to insert a MAILTO: tag so people can email the page using their email client. The body of the message is going to be the URL of that page. Using ASP,...
5
by: hfk0 | last post by:
Hi, I'm new to ASP.net, SQL Server and visual studio.net, and I'm having problem inserting and storing data from a web form to a SQL database. I created a simple ASP.NET web form, a simple SQL...
5
by: glenn | last post by:
Hi folks, The problem I have is that a query string works if hard-coded but if I pass a variable to it, it does not work as shown here. This works: querystring="SELECT * FROM USERS WHERE...
0
by: pd123 | last post by:
I'm new to C# and .net and I'm trying to create a form that will register users in a sql server database. I have the following code but when I run the code I get an error " The name 'Peter' is...
0
by: tom c | last post by:
I am going through "Walkthrough: Editing and Inserting Data in Web Pages with the DetailsView Web Server Control" found at http://msdn2.microsoft.com/en-us/library/sdba1d59.aspx I am using...
5
by: kannaworld | last post by:
Hai all I wrote a common piece of code for inserting the records in a table using c#, i stored the control names in a Database table with the type of control, and the table field name will be...
5
by: dos360 | last post by:
Hello, I have two tables, one is a list of activities, the other a list of participants. I want to insert one record in the activities table and then using its identity column as foreign key, I...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.