Solved

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

Posted on 2007-11-24
3
170 Views
Last Modified: 2013-11-26
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
Comment
Question by:huangs3
3 Comments
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 total points
ID: 20343492
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
 

Author Comment

by:huangs3
ID: 20343498
I see, thank you GreenGhost
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20343501
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question