PC Review


Reply
Thread Tools Rate Thread

Array Syntax?

 
 
gregory_may
Guest
Posts: n/a
 
      2nd Mar 2004
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)






 
Reply With Quote
 
 
 
 
gregory_may
Guest
Posts: n/a
 
      2nd Mar 2004
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?


"gregory_may" <None> wrote in message
news:OlMRZ6%23$(E-Mail Removed)...
> 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)
>
>
>
>
>
>



 
Reply With Quote
 
Peter Huang
Guest
Posts: n/a
 
      2nd Mar 2004
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/de...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.

 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      2nd Mar 2004
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

"gregory_may" <None> wrote in message
news:OlTGz%23%23$(E-Mail Removed)...
> 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?
>
>
> "gregory_may" <None> wrote in message
> news:OlMRZ6%23$(E-Mail Removed)...
> > 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)
> >
> >
> >
> >
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MultiDimensional Array Syntax Jack Leach Microsoft Access VBA Modules 4 2nd Dec 2009 05:04 AM
Re: Object Array Syntax Mythran Microsoft C# .NET 0 9th Jan 2007 06:07 PM
Populating an array<System::UInt32>^ from an unmanaged array in the new C++/CLI syntax Bern McCarty Microsoft VC .NET 2 21st Dec 2006 05:14 AM
Array Syntax Samuel Shulman Microsoft VB .NET 2 20th Aug 2006 11:34 PM
Want to Use an Array; Not Sure of Syntax RichK Microsoft Excel Misc 4 6th May 2005 08:15 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:39 PM.