load UITextFields using a loop? - objective-c

I am trying to populate a number of UITextFields with data. Currently, I am doing it line-by-line.
myFirstName.text = originalPerson.firstname ;
myLastName.text = originalPerson.lastname;
myAddress1.text = originalPerson.address1;
Is there a way to do it programatically, in a loop?

You could add your text fields to an array (or IBOutletCollection), and your keys to another array, and then in your loop assign the value for the key in your array to the equivalent text field in your other array, but by the time you've done and maintained and debugged all that, I don't think you'll have saved yourself much.

Related

Referring to a variable in a variable

This is probably quite simple to do but I am very new to visual basic so please bear with me.
I know similar questions have been asked, but none of them seem to answer my question. I have an array created earlier, and want to randomly choose one value and change it.
I have two random number generators ('GeneratorP1' and 'GeneratorP2'), which are added to some text a create a variable name which is stored in Loc1:
Dim Loc1 As String
Loc1 = "th" & GenerateP1() & "(" & GenerateP2() & ")"
eg: th5(4) is stored in Loc1
How would I go about changing the value of th5(4)?
EDIT: I have 5 arrays (th1, th2, th3, th4, th5), with indexes up to 4
If I'm understanding your question correctly (which I'm not sure I am), you're wanting to use the string stored in variable Loc1 as a variable name?
You said you created an array earlier, so why do you need to randomly generate the variable (GenerateP1) and the index (GenerateP2)? If you have a single array, can't you use randomly pickthe index, then assign whatever value you want to it?
th5(GenerateP2()) = "my value"
If you had multiple arrays (th1, th2, th3, etc.) I would suggest using a Dictionary to randomly pick which array to use. Alternatively you could use a multidimensional array and generate both indexes
th(GenerateP1(), GenerateP2()) = "my value"
If possible, you should consider using a List instead of an Array.
Instead of having multiple arrays, use a 2-dimensional array
Dim th(5,4)
So instead of trying to reference th1(3), you could use th(1,3) or th5(2) would be th(5,2)

Changing the value of an array through a foreach statement VB.net

I am making a program that automates the seperation of a csv file. We read the csv in through and then assign the "line" using a split command to an array. After that we go through each "cell" in the array and put an = in front because this causes leading zeros not to be lost. Here's the code.
arLine = line.Split(replace)
For Each cell As String In arLine
cell = cell.Replace(",", "")
cell = String.Format(cellFormat, cell)
Next
arLine is the array and replace is the delimiter, in this case a pipe not that it matters.'
When it goes through, the arLine is correct but the values in each cell are not changing, any thoughts? newer to VB.net and need direction
Try with this.
arLine = line.Split(replace)
For x as Integer = 0 To arLine.Lenght - 1
arLine(x) = arLine(x).Replace(",", "")
arLine(x) = String.Format(cellFormat, arLine(x))
Next
You are looping using For Each and trying to modify the value returned by the iterator, but this creates a new string for your cell var, so you are not referencing the original array.
Instead, using a traditional for - loop, you could update directly the values in the arLine array
Strings in .NET are immutable. When you modify a string, you actually create a brand-new string.
So, cell no longer refers to the original array element. Instead, try a for loop rather than a foreach loop and assign the modified value back to arLine(i).
Use a for loop rather than foreach so that you have a handy index into the collection.

Visual Basic Array Push

I am currently iterating through a loop and redimensionalising my array every time I need to add a value (as you can imagine this takes some time for a redim every loop) is there a way I can implement a push similar to ruby or java? This would need to save the processing time needed to redimensionalise the array every time I need to add a value to it.
Cheers
Martin
You'd be better off using a List (Of Type). Then you can just call the Add method.
For example:
Dim foo As New List(Of String)
foo.Add("Bar")
You can concat the array, with a array containing only the new item, or multiple.
Array.Concat({Item}).ToArray

Total Nos. of items in array variable

I have an array variable (string type). It contains certain no. of items, that I donot know how many they are.
I need to run a loop for that many nos. that the array contains. I tried LBound and UBound loop but it says my array is not a system array.
How can I know how many items my array contains?
Thanks
Furqan
You can use the Length property of the array object.
From MSDN (Array.Length Property):
Gets a 32-bit integer that represents the total number of elements in all the dimensions of the Array.
Read about arrays in VB.NET and the Array class for better understanding of arrays in VB.NET and the .NET framework.
Update:
However, for looping over an array you should simply use a For Each loop (as an array is treated like any other collection in .NET) - this way you will not make any silly mistakes with array bounds and off by ones:
For Each item As arrayItemType in MyArray
' do stuff with item
Next
See the example on this page.
You look at the length:
To get the number of items in the first dimension: arrayName.GetLength(0)
If you need the index, use GetUpperBound(0)
Some helpful examples here.
Like Oded said, you can use the Length-propery of the Array. This would look something like this:
Dim data As String() = {"one", "two", "three", "four"}
For i = 0 To data.Length - 1
Console.WriteLine(data(i))
Next
If you just want to loop all strings in your array, you can use For Each as well:
For Each s As String In data
Console.WriteLine(s)
Next
If the compiler is telling you that your variable is not a system array, then chances are, it's not an array. If it's not an array, you won't be able to get its bounds through any means.
Inspect the variable in the locals window and verify that your variable is of the type that you think it is. It's probably not an array after all.

for loop in Flex 3

i want to create a loop withing my mxml code to create a variable number of input fields based on an integer value , this value is the result of a call which gets the number of columns in a database table.
i have tried to use the repeater component however it needs an array, and my call is an int.
Is there something in flex mxml whcih can take an int and then loop as necessary?
I'd recommend creating a function that builds the array you need based on the integer, and feeding the resulting array to the repeater.
I hope that helps, no time for more right now ;)