• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

How can I shorten this small piece of code?

I'm using sockets in my application, so for the most part, I'm using byte arrays..

Now, I'd like to shorten this code:

Dim OutData(3) As Byte
OutData(0) = 0
OutData(1) = 0
OutData(2) = 0
OutData(3) = 1
SendCommand(OutData)

SendCommand is a function I wrote that accepts a byte array, puts some headers onto it, and sends it via TCP. The problem shows up when I have say a 32 byte string to send.

I know I could possible do something like this:

SendCommand(ASCII.GetBytes(Chr(0) & Chr(0) & Chr(0) & Chr(1)))

But that's kind of ugly with all the Chr's.

Can I do something like this? This doesn't work, but there has to be something similar:

SendCommand(New Byte() = {0, 0, 0, 1})

in the same way you could do New TYPE = VALUE
0
todd_
Asked:
todd_
  • 3
  • 2
  • 2
1 Solution
 
EagleEye1975Commented:
How about:

Dim OutData(3) as byte = 0
OutData(3) = 1

I wonder if all of the array elements would be set to 0 in the DIM, and then you just change the one you want as 1 to 1. :)
0
 
EagleEye1975Commented:
Hmm, I just tried it and it didn't work...

This did though:

Dim OutData() As Byte = {0, 0, 0, 1}

0
 
todd_Author Commented:
Yes, I was also aware of that, but I wanted to do it without a separate variable, and preferrably in one line.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
EagleEye1975Commented:
Class Outdata
   Dim Data(3) as byte

   Sub New()
      Data(0) = 0
      Data(1) = 0
      Data(2) = 0
      Data(3) = 1
   EndSub
EndClass

SendCommand(Dim OD as new OutData)

There. :)
0
 
Ignacio Soler GarciaCommented:
Your first try was ok, I always does like this, only that it had a small typo, try like this:

SendCommand(New Byte() {0, 0, 0, 1})

This works fine.

SoMoS
0
 
Ignacio Soler GarciaCommented:
Hey todd, what do you think about the solution I proposed?

It's ok?

SoMoS
0
 
todd_Author Commented:
That's perfect, I'm sorry it took so long for me to reply as I kind of lost hope after I replied.. thanks.. all points to you :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now