473,408 Members | 2,442 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,408 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 1741
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.