Vb.Net Declare Byte Array
Vb.Net Declare Byte Array
Vb.Net Declare Byte Array
Vb.Net Declare Byte Array
Vb.Net Declare Byte Array
Vb.Net Declare Byte Array Vb.Net Declare Byte Array Vb.Net Declare Byte Array Vb.Net Declare Byte Array Vb.Net Declare Byte Array Vb.Net Declare Byte Array Vb.Net Declare Byte Array Vb.Net Declare Byte Array
Vb.Net Declare Byte Array Vb.Net Declare Byte Array
Vb.Net Declare Byte Array
Go Back  Xtreme Visual Basic Talk > > > Vb.Net Declare Byte Array


Reply
 
Thread Tools Display Modes
  #1  
Old 01-05-2009, 04:23 PM
AgentSmithers AgentSmithers is offline
Contributor
 
Join Date: Jan 2005
Location: SOCal
Posts: 492
Default Vb.Net Declare Byte Array


Well I know you can Declare like this

Dim MyByte() as Byte = {&h1, &h1, &h1}

But what if I want to make it a Static Length like this

Dim MyByte(100) as Byte = {&h1, &h1, &h1} 'assuming the rest is Zeros

Do I have to like make a ByteArray like the 1st Example and do a For I Loop to fill in Example 2? What is the next way of Declaring something such as

Dim MyVar = New Byte() = {&h1, &h1, &h1}

Thankyou!
__________________
Http://ControllingTheInter.Net
My General Computer Forum, From Security To Programming And Back To Troubleshooting.
Reply With Quote
  #2  
Old 01-06-2009, 12:07 PM
AgentSmithers AgentSmithers is offline
Contributor
 
Join Date: Jan 2005
Location: SOCal
Posts: 492
Default

I Assume this is the best way??

Dim MyByteArray() As Byte = New Byte() {&H1, &H1}
ReDim Preserve MyByteArray(100)

Anyway to do this in one line?? =) without a ":" of course =O

And Whats the Difference Between

Dim MyByteArray() As Byte = New Byte() {&H1, &H1}

and

Dim MyByteArray() As Byte = {&H1, &H1}
__________________
Http://ControllingTheInter.Net
My General Computer Forum, From Security To Programming And Back To Troubleshooting.
Reply With Quote
  #3  
Old 01-06-2009, 01:54 PM
AtmaWeapon's Avatar
AtmaWeaponVb.Net Declare Byte Array AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I think it's easier to explain how arrays can be initialized and what happens when you initialize them, rather than focusing on what individual attempts you have made do. One thing I'd like to point out early on is the pursuit of accomplishing tasks in one line is usually folly; It's generally better to use multiple lines to write code that's more clear than it is to do everything in a single line. That aside, let's talk arrays.

1. First, we have the basic array declaration:
Dim myArray() As Byte

This tells VB .NET that you want the variable myArray to be an array with elements of type Byte, but you don't want to create the array right now. No memory is allocated, and myArray's value is Nothing.

2. Next, you can declare an array with a size:
Dim myArray(9) As Byte

This tells VB .NET that you want an array with bounds 0-9, or 10 elements. Each element is of type Byte. When this line is executed, VB .NET creates enough memory for a 10-element Byte array and myArray will point to this array. Each element is initialized to its default value, 0.

3. This is an extension of 1. After declaring the array, you need to actually create an array so it can be used:
Dim myArray() As Byte
ReDim myArray(9)


Note that you cannot write a line such as myArray = New Byte(9) because the compiler interprets this as a constructor call. You have to use the ReDim statement. ReDim can handle things if your array variable is Nothing, so it's a convenient way to create a new array.

Now, let's examine how to initialize arrays. Note that some of this might be a new feature of VS 2008; I seem to remember array initialization got some new syntax in this version.

Because of case 3 above, VB .NET's syntax for array initialization can get a little clumsy. Here's the tragic syntax collision that causes problems:
  • The New keyword has to figure out which type you are referring to.
  • The () symbols are used to delimit a method's parameter list.
  • The () symbols are used to delimit array bounds.

Whoops! When the compiler sees New Byte(9), it has no idea if you mean you want to call some constructor on Byte or if you mean you wish to refer to a 9-element Byte array. The compiler guesses you mean a constructor call, but no such constructor exists so compilation fails. The solution is in the {} bracket syntax; when the compiler sees these it knows that New Byte() is an array initializer and not a constructor call.

There are some simple rules to array initialization:
  • Bounds are optional, but if provided they must match the initializer list exactly.
  • The initializer list determines the size of the array; this overrules any other information.

So, this is valid and will create an array that has 10 elements, each containing the Byte value 1:
Dim bytes() As Byte = New Byte() {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}

It would also be legal to write it this way:
Dim bytes() As Byte = New Byte(9) {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}

You can also take a small shortcut and toss out the = part of the line:
Dim bytes() As New Byte() {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}

It turns out that the New Byte() part is optional, since the initialization list implies it, so this is legal as well:
Dim bytes() As Byte = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}

Now, this syntax doesn't allow a few things. First, if your array is declared with a size (2 above) you cannot use an initializer:
' WRONG
Dim bytes(3) As Byte = { 1, 1, 1, 1 }
Dim bytes(3) As Byte = New Byte() { 1, 1, 1, 1 }


You also cannot specify less elements in the initialization list than the number of elements in the array:
' WRONG
Dim bytes() As Byte = New Byte(9) { 1 }


Some programming languages allow this, and it serves to initialize every value of the array to the indicated value. Other programming languages take it to mean you want to specify some values but leave the rest at the defaults. In VB .NET, if you want to specify some but not all values, you will have to write the code yourself:
Code:
Dim bytes(99) As Byte

For i As Integer = 0 To 3
    bytes(i) = i
Next

For i As Integer = 4 To bytes.Length - 1
    bytes(i) = 1
Next
Your proposed method using ReDim Preserve is wasteful, though not by a lot. What it does is create a 2-element array, then create a 101-element array, then copy the 2 elements into the 101-element array. I would prefer something more like this:

Code:
Dim myByteArray(100) As Byte

myByteArray(0) = &H1
myByteArray(1) = &H1
I'd use a for loop if there were more than 2 or 3 values to set. Be certain that you really want 101 values; remember that in VB the array bounds given indicates the upper bound of the array, not the number of elements.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #4  
Old 01-06-2009, 06:00 PM
AgentSmithers AgentSmithers is offline
Contributor
 
Join Date: Jan 2005
Location: SOCal
Posts: 492
Default

Thank you for the Explanation, But if you use the Methed without redim such as

Code:
Dim MyBytes(100) as Byte
then Assinening it by hand like say 20 Bytes? thats 21 lines of code instead of just 2?

Code:
Dim MyBytes() as Byte = { &h1, &h2 .... }
Redim Preserve MyBytes(100)
So Is your Example still a better way of doing it?
__________________
Http://ControllingTheInter.Net
My General Computer Forum, From Security To Programming And Back To Troubleshooting.
Reply With Quote
  #5  
Old 01-06-2009, 07:08 PM
AtmaWeapon's Avatar
AtmaWeaponVb.Net Declare Byte Array AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Well if you're doing 20 bytes by hand, the best case is when you have a pattern like in your example:

Code:
Dim myBytes(100) As Byte

For i As Integer = 0 to 19
    myBytes(i) = i + 1
Next
If it's 20 random bytes, you've got to type them in anyway, so I'd rather use something more like this:
Code:
Shared ReadOnly InitialBytes() As Byte = { 1, 5, 7, 3, ... }
' later...
Dim myBytes(100) As Byte

For i As Integer = 0 to 19
    myBytes(i) = InitialBytes(i)
Next
If I weren't so lazy, I'd look up the Array.Copy syntax and do it in two lines, but it's not like the loop's hard to follow.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #6  
Old 01-07-2009, 11:36 AM
AgentSmithers AgentSmithers is offline
Contributor
 
Join Date: Jan 2005
Location: SOCal
Posts: 492
Default

Great I figured, Thanks!
So all in all is Redim just Ineffective and bad way of programming such as "GoTo" loop..?
__________________
Http://ControllingTheInter.Net
My General Computer Forum, From Security To Programming And Back To Troubleshooting.
Reply With Quote
  #7  
Old 01-07-2009, 11:42 AM
darkforcesjedi's Avatar
darkforcesjediVb.Net Declare Byte Array darkforcesjedi is offline
Trust me, I'm an

* Expert *
 
Join Date: Apr 2001
Location: In ur base, pwnin d00dz
Posts: 1,964
Default

Redimensioning an array isn't necessarily bad. But there's no sense in doing it if you don't have to. IIRC, ReDim Preserve actually creates a new array, copies the old one to the new one, then gets rid of the old one.
__________________
To err is human; to debug, divine.
Reply With Quote
  #8  
Old 01-07-2009, 12:40 PM
AtmaWeapon's Avatar
AtmaWeaponVb.Net Declare Byte Array AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I agree with darkforcesjedi. ReDim isn't bad by itself, but there are better ways to solve the problem. The primary thing to remember is that if you ReDim Preserve, you are performing an array copy, so if you do it a lot things will be much slower than they could be otherwise. Here's my rules for using it.

I'll use ReDim to resize a fixed array once. For example, sometimes I don't know how big I want an array to be at compile time, but I know that once I know how big it should be the size won't change. For example, I might have a class that needs to load some items from a file but the file can have several different things to load:
Code:
Public Class FileLoader
    Private _lines() As String

    Public Sub LoadFile()
        Dim fs As New FileStream...
        Dim numberOfItems As Integer = GetNumberOfItems(fs)
        ReDim _lines(numberOfItems - 1)
        LoadItems(fs)
        fs.Close()
    End Sub

    ' other code...
End Class
If I'm using ReDim more than once, I ask myself why. If the answer is because the array's part of a class that's initialized, used, then thrown away (like FileLoader above), I leave things as they are. If the answer is because it's the nature of the array to change sizes frequently, I immediately switch to a List(Of T).

I never use ReDim Preserve. I feel like this is a sign that the array has a constantly changing capacity, and I feel like this is the job of List(Of T) or some other list-like class. Really the only strong recommendation I'd make is the one about ReDim Preserve; I feel like it's wasteful.

However, these are guidelines I follow, not set-in-stone rules. I'm sure there's scenarios where I might find myself breaking these guidelines. Still, I feel it's good to ask yourself why you're using ReDim and determine whether List(Of T) might be a better fit. Arrays imply fixed-size, lists imply variable size, so if you use the right one your code is more clear.
Reply With Quote
  #9  
Old 01-07-2009, 04:07 PM
AgentSmithers AgentSmithers is offline
Contributor
 
Join Date: Jan 2005
Location: SOCal
Posts: 492
Default

Ah Thank you I feel this was a Useful post.
__________________
Http://ControllingTheInter.Net
My General Computer Forum, From Security To Programming And Back To Troubleshooting.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Vb.Net Declare Byte Array
Vb.Net Declare Byte Array
Vb.Net Declare Byte Array Vb.Net Declare Byte Array
Vb.Net Declare Byte Array
Vb.Net Declare Byte Array
Vb.Net Declare Byte Array Vb.Net Declare Byte Array Vb.Net Declare Byte Array Vb.Net Declare Byte Array Vb.Net Declare Byte Array Vb.Net Declare Byte Array Vb.Net Declare Byte Array
Vb.Net Declare Byte Array
Vb.Net Declare Byte Array
 
Vb.Net Declare Byte Array
Vb.Net Declare Byte Array
 
-->