Array Syntax?

  • Thread starter Thread starter gregory_may
  • Start date Start date
G

gregory_may

I feel silly for posting such a simple question, but here goes.

What is the difference between these statements - Case 3 doesnt work ... why
not?:

CASE 1

Public Case1 As Byte()



CASE 2

dim Case2() As Byte



Case 3

dim Case3 as new Byte()





I am trying to get something like this to work:

case1.copy (case1,case3,0)
 
Ok, I seem to be getting it to work by:

Dim Case2(case1.length) as byte()
case1.copy(case1,case2,0)

seems kind of wacky, is there any better way?
 
Hi Gergory,

Thanks for posting in the community.
CASE 1

Public Case1 As Byte()

declare an Case1 as an type Byte array
CASE 2

dim Case2() As Byte
Same as above, except you can define the boundlist in the parentheses in
this method.
Case 3

dim Case3 as new Byte()

This line will become dim Case3 as new Byte
This will call the new instance of Byte type.

[ <attrlist> ] [{ Public | Protected | Friend | Protected Friend |
Private | Static }] [ Shared ] [ Shadows ] [ ReadOnly ]
Dim [ WithEvents ] name[ (boundlist) ] [ As [ New ] type ] [ = initexpr ]

For detailed information take a look at the link below.
Dim Statement
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/
vastmdim.asp

Here is the code in VB.NET
Sub Main()
Dim Case1 As Byte()
Dim Case2() As Byte
Dim Case3 As Byte
Dim Case4 As New Byte
End Sub


The according IL language generated.
..method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01
00 00 00 )
// Code size 11 (0xb)
.maxstack 1
.locals init ([0] unsigned int8[] Case1,
[1] unsigned int8[] Case2,
[2] unsigned int8 Case3,
[3] unsigned int8 Case4)
IL_0000: nop
IL_0001: ldloca.s Case4
IL_0003: initobj [mscorlib]System.Byte
IL_0009: nop
IL_000a: ret
} // end of method Module1::Main


So Case1 and Case2 are of the same type. So do with the Case3 and Case4.


It seems that you wants to copy and byte array from one to another. If I
misunderstand your meaning, please feel free to post here.
Dim Case2(case1.length) as byte()
case1.copy(case1,case2,0)

I think the method will be ok.

'Declare Case1
Dim Case1 As Byte() = New Byte() {12, 34}

'Allocate the memory for Case2
Dim Case2(Case1.Length) As Byte
'Use the Copy function of Array to copy the data from case1 to case2
Array.Copy(Case1, Case2, Case1.Length)

'Change the data in Case1
Case1(0) = 100
'This will write out 12
Console.WriteLine(Case2(0))

If you still have any concern please post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Gregory,
It appears that Peter answer what the difference is:

I want to point out a (potential) "off by one" bug in your sample:

The code:
Dim Case1(10) As Byte
Dim Case2(case1.length) As Byte
case1.copy(case1,case2,0)
Will cause Case1 to be an 11 element array, while Case2 will be a 12 element
array.

It needs to be:
Dim Case1(10) As Byte
Dim Case2(case1.length - 1 ) as byte
case1.copy(case1,case2,0)

As VB.NET expects the upper bound when defining an array, not the length.
Your case2 array would be one more element then your case1 array, hence "off
by one".

Remember that arrays start with index 0 in VB.NET.

Also VB.NET 2003 (.NET 1.1) gives an error if you include the trailing () on
Byte.

Hope this helps
Jay
 
Back
Top