1

Curious issue I am experiencing.

I iterate through an array that was created from a string split. On iteration, I make modifications to the items in the array. (Add characters to the item (string, in this regard)

The changes are affected IN the for loop, but when using this array directly after the for loop, the changes seems to be "dropped", and the original array, as before the changes, is used.

It is probably a byRef, byVal issue... but I am not specifically passing it anywhere.

Maybe someone can shed some light on this behavior. I have since built a list inside the for loop, and add to it as I make the string changes. I then use the list in my select statement. This works, but I am curious as to why the array drops its changes.

Regards

Dim descriptionSplit() As String
descriptionSplit = Split(unit.itemDescription, "[")

'add the cut "[" back to the strings . "[" was cut from the strings when it was split  ON "["
For Each splitSection As String In descriptionSplit.
'add back the '[' char
splitSection = "[" & splitSection
Debug.Print(splitSection)
Next

'look for and find TAGS
For Each splitSection As String In descriptionSplit
Select Case True
'Look for #UNIT# TAG

'######## HERE, the array has reverted to the original copy....
Case splitSection.Contains("[U]")

5 Answers 5

4

If you use For Each, the splitSection variable is not a reference to an array item, but a copy of the array item. Thus the array itself is never changed.

Iterate the array using For and an index variable and access the array directly to make changes.

I'm not too up to date on my Visual Basic, but in C# it should somewhat like this:

for (int i = 0; i < descriptionSplit.Length; i++)
    descriptionSplit[i] = "[" + descriptionSplit[i];

I'll shamelessly nick the code that James posted and also post it here for the sake of completeness and to conceal my inability to write VB.NET from memory :-)

For index As Integer = 0 To descriptionSplit.Length-1
    descriptionSplit(index) = "[" & descriptionSplit(index)
Next
Sign up to request clarification or add additional context in comments.

5 Comments

Does this apply ONLY to arrays? Or to any object used in a For Each loop? I am sure I have made changes to objects using for each before?
@LouisvanTonder as I stated in my answer, when using For Each with value types (e.g. String/Integer etc.) the item is a copy, when using reference types you get the original reference.
As @James is saying, this is only a problem for array of value types. If, for example, you have an array of objects, this is not a problem, because objects are not copied, but referenced.
Just to be extra clear (and correct me if I'm wrong), but it will only work for modifying objects, like for each item in array item.value = "new value" will modify the object in the array but for each item in array item = new Object() will still not change the array, correct?
That should be correct. As opposed to array(i) = new Object(), which again will change the array itself.
1

You never change your array. What you do is the following:

You assign a new value to your loop variable.

That's all. This doesn't change the value inside your array.

Comments

1

You aren't actually modifying the array at all here, when you do a For Each on a value type you get a copy of each item therefore

splitSection = "[" & splitSection

Is only applicable within the context of the loop, those changes will not be reflected in the array.

If your goal is to modify the items as you process them then you should use a basic for loop where you can index into the array e.g.

For index As Integer = 0 To descriptionSplit.Length-1
    descriptionSplit(index) = "[" & descriptionSplit(index)
Next

5 Comments

Thanks, I will be accepting the other answer due to its addition of a solution. Thank you for this reply.
@LouisvanTonder my answer has a solution - and it's in the correct language.
Sorry, it did not when I posted my comments, or did I miss it?
@James Your code is not correct. It should read descriptionSplit(index) = "[" & item
@ThorstenDittmar Good spot, had an extra item in there, updated. Was actually able to get rid of the temp variable as well given the item is only ever needed once.
0

This is because of this code:

For Each splitSection As String In descriptionSplit
Select Case True
 ... 

Your splitSection becomes a string of the descriptionSplit, and that split array is array of original strings.

for you need to change array of original split.

I'm not an expert in VB, but considering that you wrote in tags C#:

for(var i=0; i< descriptionSplit.Length; i++) {
    descriptionSplit[i] = "[" + descriptionSplit[i];
}

Comments

0

Ok, from what I've seen so far:

You're only assigning a new reference to your variable splitSection, which only exists within the loop's scope - so the reference within the array will never change.

Solution:

Dim descriptionSplit() As String
descriptionSplit = Split(unit.itemDescription, "[")

For i As Integer = 0 To descriptionSplit.Length - 1 
    descriptionSplit(i) = "[" & descriptionSplit(i)
Next

Hope this helps!

1 Comment

This won't even compile, splitSelection & splitSection are never defined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.