TRH
TRH

Reputation: 574

VB.NET - Function Cannot Modify a Member Of An Array

I'm trying to pass an Array to a Sub so the Sub can modify one of the values of the array.

Something like this

Dim a As String = "STARTVALUE"

PopulateDataSet("Management", {a})

Public Sub PopulateDataSet(ByRef SomeRandomOtherVariable As String, ByRef ToBePopulatedVariables() As String)

    ToBePopulatedVariables(0) = "TheNewValue"

End Sub

When I run the code I can step through the Sub and see where ToBePopulatedVariables(0) - which is the variable A - has the value "STARTVALUE" and then that value is changes to "TheNewValue".

But when control is passed back to the calling code the value of a reverts back to "STARTVALUE".

I've tried everything I can think of. Any ideas?


Both @karl-anderson and Nadeem_MK suggest the same thing.

But

Dim a As String()
a(0) = "FirstVariable"
PopulateDataSet("Management", {a}) 

Returns “Object reference not set to an instance of an object.”

When assigning the value to a(0)

However

Dim a As String() = {"FirstVariable"}

Does run.

But I’m still stuck with the new value not being returned to the calling code. I've tried doing this ByVal and ByRef but for Arrays I don't think it matters. –

Upvotes: 2

Views: 522

Answers (3)

Nadeem_MK
Nadeem_MK

Reputation: 7689

Is this vb.Net? If so, I think your array declaration is wrong.
Try this;

Dim a(0) As String
a(0) = "STARTVALUE"

PopulateDataSet("Management", a)

Public Sub PopulateDataSet(ByRef SomeRandomOtherVariable As String, ByRef ToBePopulatedVariables() As String)

    ToBePopulatedVariables(0) = "TheNewValue"

End Sub

Upvotes: 1

sloth
sloth

Reputation: 101072

Think a moment about what you're actually doing here:

Dim a As String = "STARTVALUE"

PopulateDataSet("Management", {a})

You create a string called a with the value STARTVALUE. Then you create a new array ({a}) which contains a. You call PopulateDataSet, which replaces the first element of the array.

Why don't you see a change?

The variable a is not changed at all, it's still STARTVALUE. You don't have a reference to the array you created, so you won't be able to observe the change to that array (it could even been garbage collected already).

Also, PopulateDataSet does not return anything, it just changes ToBePopulatedVariables. There's also no need for ByRef.

You should simply pass a variable that holds an array:

Dim a As String() = {"STARTVALUE"}
PopulateDataSet("Management", a)

so you can access a (which is an array, not a string) after the call to PopulateDataSet.

Upvotes: 1

ElektroStudios
ElektroStudios

Reputation: 20464

In your last edit, you are trying to assing a value to an empty array so it should launch an Object reference not set to an instance of an object exception, you need to specify the array bounds when declaring:

Dim a(0) As String
a(0) = "FirstValue"

Or you can do it like this:

Dim a As String() = {"FirstValue"}

The first code that you posted on your question worked nice (fixing some syntax), and this next modification that I did is working too, if you still experiencing problems with the array bounds/values then possibly the error is in other part of your code. To be ensure that is not a variable problem, declare it out of any method like in this example.

Public Class Form1
Private a As String()

Private Sub Test() ' Handles MyBase.Shown
    PopulateDataSet("Hello", a) : MsgBox(a.First)
    PopulateDataSet("World!", a, 5) : MsgBox(a(5))
    PopulateDataSet("I've said Hello World!", a, 10) : MsgBox(a.Last)
End Sub

Public Sub PopulateDataSet(ByVal NewValue As String,
                           ByRef Array As String(),
                           Optional ByVal ItemIndex As Integer = 0)

    If Array Is Nothing Then
        System.Array.Resize(Array, 1)
    ElseIf Not Array.Count > ItemIndex Then
        Do Until Array.Count > ItemIndex
            System.Array.Resize(Array, Array.Count + 1)
        Loop
    End If

    Array(ItemIndex) = NewValue

End Sub

End Class

Upvotes: 1

Related Questions