• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

How to pervent a VB function from modefying an array argument using it is interface?

I got a VB.net function:
    Private Function Mean(ByVal A() As Double) As Double

I found that I can modify elements of A using the statement like
    A(3)=0
and I doesn't feel this is a good declaration of the function but couldn't find a better way.

How can I do it better?

Thank you!
0
huangs3
Asked:
huangs3
1 Solution
 
Göran AnderssonCommented:
You can't put anything in the declaration of the function that would prevent the access of the members of the array.

You can send something other than an array to the function, though. If the function only needs to loop throught the items, you could send the enumerator of the array instead:

Private Function Mean(A As Enumerator(Of Double)) As Double

Otherwise you could create a class that encapsulates the array, and only offers read access to it, and send an instance of that class to the function.
0
 
huangs3Author Commented:
I see, thank you GreenGhost
0
 
Jaime OlivaresSoftware ArchitectCommented:
not sure about what you are asking, you can initialize an array this way:

Dim arr As Double() = New Double() {0.2, 0.5, 0.87, 1.15, 3.14}
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now