0

Basic arrays question: I have declared a public array and initialized it with values. In another private sub, I want to fill that public array with values. What is the easiest way to do it?

The double variables in the array are also declared public. The following code is the array created and initialized in a public module. In the private sub, I am changing the values of the double variables (ch0LowLmt, ch1LowLmt, etc, etc) and now I just simply want to reinitialize the array with the new values. What is the simplest way to do it?

Public AlarmLowLimit() As Double = New Double(15) {Ch0LowLmt, Ch1LowLmt, Ch2LowLmt, Ch3LowLmt, Ch4LowLmt, Ch5LowLmt, Ch6LowLmt, Ch7LowLmt, Ch8LowLmt, Ch9LowLmt, _
                                                   Ch10LowLmt, Ch11LowLmt, Ch12LowLmt, Ch13LowLmt, Ch14LowLmt, Ch15LowLmt}
busarider29
  • 185
  • 5
  • 18
  • Standard arrays answer: dont use them. Use a List(of) instead. In that snippet, unless those variables are also declared, all the arrays values will be 0. To change them is simple though: `AlarmLowLimit(x) = newVal` – Ňɏssa Pøngjǣrdenlarp Oct 13 '15 at 13:57
  • Yes, the variables are also declared as doubles. I am doing what you suggest, but was wondering if there was a simpler way to do it. Since the array only has 16 elements, then doing it that way (AlarmLowLmt(0) = ch0LowLmt, etc...) works fine but if I had to do it that way for an array that had say, 100 or more variables in it, then that would be a lot of code. Was just wondering if there was a simpler, more "efficient" way of doing it. – busarider29 Oct 13 '15 at 14:11
  • It is hard to say with just a declaration snippet to go on, but if there is code to apply or check a limit of something, **I** would create a class to do that for me, then perhaps a collection class to manage the 16 or 100 of them. – Ňɏssa Pøngjǣrdenlarp Oct 13 '15 at 14:49
  • If you want to be more "efficient" then don't use individual Variables **and** an Array...as then you have to keep them "in sync". This is a coding nightmare waiting to happen. Change your design... – Idle_Mind Oct 13 '15 at 15:00
  • @busarider29, did u c my answer? – ilans Oct 14 '15 at 09:50
  • Guess you didn't... ok. – ilans Nov 24 '15 at 08:14

1 Answers1

0

Because Double is a value type, you do need to change it manually in the way you are working.

Another way, is to encapsulate the double var inside a class, which can be put inside your array, so it will be a reference type.

In this case you'd just update your new reference type objects in the private function you have mentioned. They will also change in your array.

See a nice solution here.

And the code example in Vb.Net:

Public Class MyObjectWithADouble
    Public Property Element() As Double
        Get
            Return m_Element
        End Get
        Set
            m_Element = Value
        End Set
    End Property
    Private m_Element As Double
    ' property is optional, but preferred.
End Class

Dim obj = New MyObjectWithADouble()
obj.Element = 5.0
Community
  • 1
  • 1
ilans
  • 2,537
  • 1
  • 28
  • 29