I have a textbox called txtBox1, a second textbox called txtbox2, a Label and a Button. I need to create a function that
- accepts 2 integer parameters and returns a string.
- creates a 2 dimensional array based on the integers passed in those parameters. The first integer parameter will represent txtbox1, the second integer parameter will represent txtbox2.
- use nested for loops to fill the array elements with incrementing values starting at 1
-
As part of your looping structure, keep track of all element values in a string variable and separate them with a comma. For example, if the user enters 3 in txtbox1 and 5 in txtbox2 and clicks the button, we would get an array like so:
:Length=15 (0,0): 1 (0,1): 2 (0,2): 3 (0,3): 4 (0,4): 5 (1,0): 6 (1,1): 7 (1,2): 8 (1,3): 9 (1,4): 10 (2,0): 11 (2,1): 12 (2,2): 13 (2,3): 14 (2,4): 15And the values populated in the elements would be 1,2,3,4,5,6,7,8,9,10,11,12,13,14 and 15.
- The string passed back will be in the format “array is 3 x 5 and the values in the array elements are 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15”.
- Populate the Label with this string value.
Here is what I have so far…
Shared Function myArray(int1 As Integer, int2 As Integer) As String
Dim Array(int1 - 1, int2 - 1) As Integer
Dim i As Integer
Dim j As Integer
Dim counter As Integer = 1
For i = 0 To int1 - 1
For j = 0 To int2 - 1
Array(i, j) = counter
counter += 1
Next
Next
Return Array(i, j)
End Function
Ok, so you almost had it. I am assuming you are stuck on only #4. And please excuse me if I type the incorrect syntax, I have not done VB in a LONG time and I am doing this from memory.
let’s take a look at what you had:
Here you are using
counterto populate the array with incremental values. You can just use this variable in another string variable, and just add commas between them:first: add a string variable to the top:
Then, use that variable in your loop:
last, change your
returnto sendoutputinstead of an element in your arrayIf you want to format the string so that the last “, ” is not shown, look at the Strings.Left function. It would go something like this:
Ok, hope that helps. Feel free to ask for clarification or something.