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?

Eg:

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
DFPITCAsked:
Who is Participating?
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
0
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.
0
AndyAinscowFreelance programmer / ConsultantCommented:
EXACTLY which line is failing with that error.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

it_saigeDeveloperCommented:
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))))
		Next
		Console.ReadLine()
	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)
		Next
		Return result
	End Function
End Module

Open in new window

Which produces the following output -Capture.JPG
0
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.
0

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
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.