VB.net to C# and odd result

J

Jake

I am converting VB.Net code to C#

Module Test

Public Function GetStrFromByte() As String
Dim tmpByte As Byte()
Dim tmpByte1 As Byte()
...
tmpByte = RandomBytes(27)

tmpByte1 = tmpByte

FillByte(tmpByte)
...
End Function

Public Sub FillByte(ByRef bytes() As Byte)
...
bytes= RandomBytes(27)
End Sub
...

I convert this so

public class Test
{
public string GetStrFromByte()
{
byte[] tmpByte = null;
byte[] tmpByte1 = null;
...
tmpByte = RandomBytes(27);

tmpByte1 = tmpByte;

FillByte(ref tmpByte)
...
}
public void FillByte(ref byte[] bytes)
{
...
bytes = RandomBytes(27);
}

In VB.Net I get correct values so that tmpByte have the first RandomBytes
run result and tmpByte1 the second RandomBytes run result.

In C# happen a odd think just after runing FillByte both tmpByte and
tmpByte1 get same the second run RandomBytes result.

What I have been missing???

Thanks in Advance,
jake
 
M

Marc Gravell

Are you sure they aren't just the same values?

i.e. is ReferenceEquals(tmpByte, tmpByte1) true or false?

If looks like the first call to RandomBytes creates a new array and
assigns to tmpByte; then tmpByte1 is also assigned a reference to the
first array; FillByte passes tmpByte byref, and FillByte reassigns
tmpByte to a new array via RandomBytes - so the two arrays should be
independent; contrary to your claim, tmpByte1 will have the first
result, and tmpByte will have the first result.

So: what is RandomBytes? is it using new Random()? In which case it is
probably timer resolution... i.e. you have two randomizers with the same
seed (taken from the clock).

Marc
 
M

Marc Gravell

Typo:
contrary to your claim, tmpByte1 will have the first result,
and tmpByte will have the ##second## result.

But again; check what ReferenceEquals returns...
 
J

Jake

I add a bit logs:

public class Test
{
public string GetStrFromByte()
{
byte[] tmpByte = null;
byte[] tmpByte1 = null;
...
tmpByte = RandomBytes(27);

tmpByte1 = tmpByte;

Utilities.ToLogHex(tmpByte1);
Utilities.ToLogHex(tmpByte);
Utilities.ToLog(System.Convert.ToString(ReferenceEquals(tmpByte1,
tmpByte)));

FillByte(ref tmpByte)

Utilities.ToLogHex(tmpByte1);
Utilities.ToLogHex(tmpByte);
Utilities.ToLog(System.Convert.ToString(ReferenceEquals(tmpByte1,
tmpByte)));
...
}
public void FillByte(ref byte[] bytes)
{
...
bytes = RandomBytes(27);
}

The results are:

18.4.2008 13:41:28 :
30 34 38 33 39 37 34 42 42 46 39 38 31 42 44 37
30 43 41 35 32 45 45 32 31 30 30
18.4.2008 13:41:28 :
30 34 38 33 39 37 34 42 42 46 39 38 31 42 44 37
30 43 41 35 32 45 45 32 31 30 30
18.4.2008 13:41:28 : True
18.4.2008 13:41:28 :
30 34 38 35 39 39 04 42 00 46 04 06 38 04 45 07
09 45 45 3D 3D 46 00 01 09 06 00
18.4.2008 13:41:28 :
30 34 38 35 39 39 04 42 00 46 04 06 38 04 45 07
09 45 45 3D 3D 46 00 01 09 06 00
18.4.2008 13:41:28 : True

Thanks in Advance,
jake
 
M

Marc Gravell

So I say again; what is RandomBytes?

In short, I think the problem is in the code you aren't posting - for
example, the following (short but runnable) illustrates my guesswork at
how it might be implemented, and it works as expected. If your code is
doing something else, then you haven't shown us the code that is doing it...

Marc

using System;
class Program
{
static void Main(string[] args)
{
byte[] tmpByte = null;
byte[] tmpByte1 = null;
tmpByte = RandomBytes(27);
tmpByte1 = tmpByte;
FillByte(ref tmpByte);
Console.WriteLine(ReferenceEquals(tmpByte, tmpByte1));
// write as base-64 for brevity; enough to compare, at least...
Console.WriteLine(Convert.ToBase64String(tmpByte));
Console.WriteLine(Convert.ToBase64String(tmpByte1));
}
static void FillByte(ref byte[] bytes)
{
bytes = RandomBytes(27);
}
static byte[] RandomBytes(int count)
{
byte[] data = new byte[count];
lock (rand)
{
rand.NextBytes(data);
}
return data;
}
readonly static Random rand = new Random();
}
 
M

Marc Gravell

Also - my guess would be that FillByte doesn't actually *assign* an
array like that at all, but that it simply updates values in the
existing array. If this is the case, then yes: there is only one array,
and you updated the contents...
 
C

Cor Ligthert[MVP]

Jake,

Be aware that VB has an odd way of creating arrays.

In VB for Net does declaring RandomBytes(27) mean 28 bytes in an array.
(This is done to keep it compatible with VB6, in my idea one of the most
stuppid decissions I ever saw, however as in C# are C++ amd Java loby
active, that is the same with VB but than for the classic VB languages).

Maybe is that the reason of the odd result.

Cor
 

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

Top