darda
darda

Reputation: 4162

VB .NET - how can I create an array of references to doubles?

I have a bunch of type Double variables in my program, say for example

Dim Area as Double = 0
Dim Perimeter as Double = 0

Somewhere in my program I want to calculate these values, so I define

Public Sub TheSquare(ByRef TheArea as Double, ByRef ThePerim as Double, ByVal TheSide as Double)
    TheArea = TheSide^2
    ThePerim = 4 * TheSide
End Sub

and somewhere in the program I'm collecting side lengths and calculating the area and perimeter; say

While True
    S = GetSideValueFromSomewhere()
    TheSquare(Area, Perimeter, S)
End

In my real program, I have, say, 20 quantities that I want to calculate. Obviously each one has a different equation. But in the end I want to output all 20 to a file, so to save typing, I create an array of the quantities, like this:

Dim TypingSaver() as Double = {Area, Perimeter}

so that I can dump values to file with a three-line for-loop instead of copying and pasting 20 variable names.

This does exactly what I want if Area and Perimeter were reference types like Objects. But since they are Doubles, TypingSaver contains only their values, not references to the actual variables. So after I use my TheSquare function the values of Area and Perimeter are correctly updated but TypingSaver just constains whatever the values of Area and Perimeter were when I declared the array.

So the question is: how can I create an array of references to doubles in VB.NET?

Upvotes: 1

Views: 2666

Answers (4)

Jono
Jono

Reputation: 4086

Fairly similar to your most recent answer, you'll want to wrap your types as "Nullable", which basically makes it an object that could be null, but also a reference type.

Ie. Dim testDouble as Nullable(Of Double) or Dim testDouble2 as Double?

See:

Upvotes: 0

darda
darda

Reputation: 4162

The single-element array idea was actually terrible. The code became horrendously slow. There is another work around a friend suggested: create a wrapper class like this:

Public Class DoubleWrapper
    Public Value As Double
End Class

Then when an array of DoubleWrappers is created it will be by reference, of course.

I understand this is not ideal but what I was looking for was a workaround until I have the time to rewrite the code from scratch.

Upvotes: 0

darda
darda

Reputation: 4162

There is a nasty way to do this. All numeric types are value types, but arrays are reference types. So I can change all my Doubles to arrays of doubles, like this:

Dim Area(0) as Double
Dim Perimeter(0) as Double

So now Area and Perimeter are 1-element arrays of double. My "looping array" then becomes

Dim TypingSaver() as Array = {Area, Perimeter}

Now TypingSaver stores references to Area and Perimeter. For me this was an easy change because I could search-and-replace for the Double declaration, change the type for TypingSaver, then in two other places I had to change direct access of this form:

TypingSaver(1) = 7

to

TypingSaver(1).SetValue(7, 0)

Not pretty, but it keeps my code consistent in that I have other "looping arrays" for other objects that are all related to each other.

Although it is not clear in my question the real solution is to restructure everything so instead of storing everything in arrays I crate a class which has all the objects I need and create a single array of that, as suggested in part by Blezden.

Upvotes: 0

Vlad Bezden
Vlad Bezden

Reputation: 89775

With approach that you are doing you can't do this, since as soon as you created array you copied all variables to the array and any changes that you are doing on variables are not reflected on array variables (like you pointed out).

What I would recommend create another class that will contain all your variables (20 variables name) as properties (get and set) and then override ToString method which will return list of all your variables. So when you need to dump those variables you will call ToString() method and it will return current values of all your parameters.

Upvotes: 3

Related Questions