How to reassign object to array in a loop

I have a function that returns an object array, lets say called oAryFunc, that I pass parameters.
I can't figure out how to assign it's output to an object in a loop
It keeps saying "Object reference not set to an instance of an object." on the second run of the loop
How do I reuse the "xAry" object?


For Each param In paramlist
Dim xAry As Object = oAryFunc(param)
Next param

Function oAryFunc(ByVal param As String) As Object
'create array
Return oArray
End Function
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Is this what you want?
Dim xAry As Object
For Each param In paramlist
 xAry  = oAryFunc(param)
Next param
DFPITCAuthor Commented:
That's what I tried first, which has the same error (doesn't seem to matter if it's declared within the loop or not)

I'm guessing it is assigning the reference to the xAry object, then the next iteration, the xAry object already has a reference, so it fails.
AndyAinscowFreelance programmer / ConsultantCommented:
EXACTLY which line is failing with that error.
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Seems like a convoluted way to do this, but here you go:
Module Module1
	Sub Main()
		Dim paramlist As String() = New String() {"One", "Two", "Three", "Paul"}
		Dim xAry As Object = Nothing
		For Each param In paramlist
			xAry = oAryFunc(param)
			Console.WriteLine("{0}", String.Join(", ", Array.ConvertAll(xAry, New Converter(Of Object, String)(AddressOf ObjectToString))))
	End Sub

	Private Function ObjectToString(ByVal input As Object) As String
		Dim result As String = String.Empty
		If input IsNot Nothing AndAlso Not String.IsNullOrEmpty(input) Then
			result = input.ToString()
		End If
		Return result
	End Function

	Function oAryFunc(ByVal param As String) As Object
		Dim result As Object() = New Object(param.Length - 1) {}
		For i = 0 To param.Length - 1
			result(i) = param(i)
		Return result
	End Function
End Module

Open in new window

Which produces the following output -Capture.JPG
DFPITCAuthor Commented:
Ok, so it wasn't that part of the code at all, it was because the function was failing because of one of it's parameters was being overwritten in a different part of the code.

I thought it was my lack of knowledge about re-assigning objects within a loop.

So, not sure how to award points, I guess i'll just distribute it evenly.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DFPITCAuthor Commented:
See comment
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.