newbee need help with code

  • Thread starter Thread starter Danny Ni
  • Start date Start date
D

Danny Ni

Hi,

Can somebody translate the following c# code into Vb.Net?

byte[] myData = new byte[nFileLen];

I tried

Dim myData As Byte() = New Byte(nFileLen)

Does not compile , with error message:
Type 'byte' has no constructors.



TIA
 
Danny Ni said:
Can somebody translate the following c# code into Vb.Net?

byte[] myData = new byte[nFileLen];

I tried

Dim myData As Byte() = New Byte(nFileLen)

Does not compile , with error message:
Type 'byte' has no constructors.

I would choose the solution posted by alejandro. However, you can make the
code above work with a little change:

\\\
Dim FileLen As Integer = 22
Dim MyData1 As Byte() = New Byte(FileLen - 1) {}
Dim MyData3() As Byte = New Byte(FileLen - 1) {}
Dim MyData4(FileLen - 1) As Byte
///
 
Dim myData(nFileLen) As Byte

Herfried K. Wagner said:
Danny Ni said:
Can somebody translate the following c# code into Vb.Net?

byte[] myData = new byte[nFileLen];

I tried

Dim myData As Byte() = New Byte(nFileLen)

Does not compile , with error message:
Type 'byte' has no constructors.

I would choose the solution posted by alejandro. However, you can make the
code above work with a little change:

\\\
Dim FileLen As Integer = 22
Dim MyData1 As Byte() = New Byte(FileLen - 1) {}
Dim MyData3() As Byte = New Byte(FileLen - 1) {}
Dim MyData4(FileLen - 1) As Byte
///
 
Crouchie1998 said:
Dim myData(nFileLen) As Byte

.... will create an array with 'nFileLen' + 1 elements with indices 0 through
'nFileLen'. I don't think that this is what the OP wants to archieve.
 
I put the user's original C# code through two different convertors & they
both came up with the same result. One of these convertors is from the VB.NET
Resource Kit
 
Crouchie1998 said:
I put the user's original C# code through two different convertors & they
both came up with the same result. One of these convertors is from the
VB.NET
Resource Kit

So the converters are buggy. 'byte[] myData = new byte[nFileLen]' should
translate to 'Dim MyData(nFileLen - 1) As Byte'.
 
Crouchie,

Because of probably compatible reasons does the array in VBNet create two
types arays in one.
One that fits the zero (C language compatible) indexer commands
One that fits the one (VB language compatible) indexer commands.

Therefore there is always created one item extra than you want. That is why
people like me (and probably Herfried) create arrays with indexes that are
forever -1 and start always with zero.

I don't (probably as you) understand why they did not build in with commands
as "mid" the method to subtract internal 1 from the indexer. However, they
did not and now because it is become standard for a long time not changable.

Probably one of those places where they did not wanted to change VB6 from
VBNet, although VB6 does not work with the framework and it could have been
only a framework part.

However the last parts are only my idea.

Cor
 
Cor Ligthert said:
Because of probably compatible reasons does the array in VBNet create two
types arays in one.
One that fits the zero (C language compatible) indexer commands
One that fits the one (VB language compatible) indexer commands.

Therefore there is always created one item extra than you want. That is
why people like me (and probably Herfried) create arrays with indexes that
are forever -1 and start always with zero.

VB does /not/ support 1-based arrays. It only supports 0-based arrays, but
/collections/ are 1-based.
 
Herfried,

They are zero based, however you can use them when you do it consequently as
1 base withouth any problem. (And than I mean real fixed arrays, when you
understand what I want to say with the last)

I never did that so you bring me in doubt.

Cor
 
Cor Ligthert said:
They are zero based, however you can use them when you do it consequently
as 1 base withouth any problem. (And than I mean real fixed arrays, when
you understand what I want to say with the last)

You can use them, but there will always be one element in the array that is
not used, and the array's 'Length' property and 'GetLength'/'GetLowerBound'
will return "wrong" values.
 

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

Back
Top