Strict on, Array of double, late binding

G

Guest

I know I am being incredibly dunderheaded about this consider the following:
STRICT ON
Dim a(,) As Double = {{1, 2}, {3, 4}, {5, 6}}
Private Sub T1(ByVal a As Array)
Dim i, j As Integer
For i = 0 To 2
For j = 0 To 2
a(i, j) = 2 * a(i, j) Underlined as latebinding error
Next
Next
End Sub
My understanding of late binding is that it occurs when there is ambiguity
in the array as declared and there is subsequent specificity in an assignment
of that array. It seems to me that declaring this array as a two dimensional
array of double is pretty specific.

Evidently VB considers the array to be of sype system array in spite of its
having been declared as above. I have philosophically arrived at a place
where I accept this. From a utilitarian standpoint, I must now deal with it.
One idea, proposed in this forum, is to use the CType function, but alas my
interpretaion produces an error:
Private Sub T2(ByVal a As Array)
Dim i, j As Integer
Dim localC(,) As Double = CType(a, Double) Error*
For i = 0 To 2
For j = 0 To 2
localC(i, j) = 2 * localC(i, j)
Next
Next
End Sub
*system array cannot be converted to double

Strict off is not an option. Array.copy works, but is it the best way?
 
C

Cor Ligthert

Mark,

The convert functions in VBNet are so powerfull so why not use them.

a(i,j) = CDbl(2 * a(i,j))

I hope this helps?

Cor
 
S

Samuel R. Neff

It seems to me that declaring this array as a two dimensional
array of double is pretty specific.

The instance declaration "a(,) as double" is shadowed by the argument
declaration "a as array".

Therefore in the code "a" is just system.array and not a two
dimensional array of doubles.

Change the argument to be

Sub T1(a as double(,))

or don't pass it at all (it's already an instance variable).

OR

use the a.SetValue() and a.GetValue() methods to manipulate the values
in the array.

HTH,

Sam


Public Class ArrayOfDouble2

Public Shared Sub Test()
Dim a(,) As Double = {{1, 2}, {3, 4}, {5, 6}}
Test2(a)

For i As Integer = 0 To 2
For j As Integer = 0 To 1
Console.WriteLine("({0}, {1}) -> {2}", i, j, a(i, j))
Next
Next

Console.ReadLine()
End Sub

Public Shared Sub Test2(ByVal a As Array)
For i As Integer = 0 To 2
For j As Integer = 0 To 1
a.SetValue(CType(a.GetValue(i, j), Double) * 2, i, j)
Next
Next
End Sub
End Class
 
J

Jay B. Harlow [MVP - Outlook]

Mark,
Private Sub T1(ByVal a As Array)
You defined the parameter to be the base type of all arrays rather then the
actual array.

Rarely do you want to pass parameters as Array, just as rarely do you want
to pass parameters as Object.

Try:
Private Sub T1(ByVal a As Double(,))
Dim i, j As Integer
For i = 0 To 2
For j = 0 To 2
a(i, j) = 2 * a(i, j) Underlined as latebinding error
Next
Next
End Sub

If you want to cast the Array parameter try either:
Dim localC(,) As Double = CType(a, Double(,))
Dim localC(,) As Double = DirectCast(a, Double(,))

CType does both Convert & Cast, where as DirectCast only does Cast. To avoid
potential unexpected conversions I use DirectCast when I only want to cast.

Remember that both of the following are synonymous:

Dim a(,) As Double
Dim a As Double(,)

They both define a 2 dimensional array of Double. The second syntax is
needed when you need to define the specific array type as a parameter or
return type.

Hope this helps
Jay
 
H

Herfried K. Wagner [MVP]

mark said:
I know I am being incredibly dunderheaded about this consider the
following:
STRICT ON
Dim a(,) As Double = {{1, 2}, {3, 4}, {5, 6}}
Private Sub T1(ByVal a As Array)
Dim i, j As Integer
For i = 0 To 2
For j = 0 To 2
a(i, j) = 2 * a(i, j) Underlined as latebinding error
Next
Next
End Sub
My understanding of late binding is that it occurs when there is ambiguity
in the array as declared and there is subsequent specificity in an
assignment
of that array. It seems to me that declaring this array as a two
dimensional
array of double is pretty specific.

'a' is of type 'Array', not of type "two-dimensional array of doubles"
('Double(, )'). Either change the datatype of the 'a' parameter to
'Double(, )' or cast the array passed to 'T1':

\\\
Dim a(,) As Double = {{1, 2}, {3, 4}, {5, 6}}
Private Sub T1(ByVal a As Array)
Dim B(,) As Double = DirectCast(a, Double(,))
Dim i, j As Integer
For i = 0 To 2
For j = 0 To 2
B(i, j) = 2.0 * B(i, j)
Next
Next
End Sub
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top