VB.net to C# and odd result

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jake

    VB.net to C# and odd result


    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(tmpByt e)
    ...
    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


  • Marc Gravell

    #2
    Re: VB.net to C# and odd result

    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

    Comment

    • Marc Gravell

      #3
      Re: VB.net to C# and odd result

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

      But again; check what ReferenceEquals returns...

      Comment

      • Jake

        #4
        Re: VB.net to C# and odd result


        I add a bit logs:

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

        tmpByte1 = tmpByte;

        Utilities.ToLog Hex(tmpByte1);
        Utilities.ToLog Hex(tmpByte);
        Utilities.ToLog (System.Convert .ToString(Refer enceEquals(tmpB yte1,
        tmpByte)));

        FillByte(ref tmpByte)

        Utilities.ToLog Hex(tmpByte1);
        Utilities.ToLog Hex(tmpByte);
        Utilities.ToLog (System.Convert .ToString(Refer enceEquals(tmpB yte1,
        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


        Comment

        • Marc Gravell

          #5
          Re: VB.net to C# and odd result

          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.WriteLi ne(ReferenceEqu als(tmpByte, tmpByte1));
          // write as base-64 for brevity; enough to compare, at least...
          Console.WriteLi ne(Convert.ToBa se64String(tmpB yte));
          Console.WriteLi ne(Convert.ToBa se64String(tmpB yte1));
          }
          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();
          }

          Comment

          • Marc Gravell

            #6
            Re: VB.net to C# and odd result

            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...

            Comment

            • Jake

              #7
              Re: VB.net to C# and odd result


              Your idea is completely correct!

              RandomBytes just get random bytes as many as parameter said and return them
              as byte[] format.

              Making tmpByte1 = tmpByte; only makes that both variables are same object
              so pointer in both variables are same.

              I have to use for loop and now they are different object and they have
              correct values.

              6+ years coding VB.Net have too much! I have forgot C/C++.

              Thank you very much and have a Nice Weekend,
              jake



              Comment

              • Cor Ligthert[MVP]

                #8
                Re: VB.net to C# and odd result

                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


                "Jake" <japentik@hotma il.comschreef in bericht
                news:OfRMOwToIH A.2064@TK2MSFTN GP05.phx.gbl...
                >
                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(tmpByt e)
                ...
                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
                >
                >

                Comment

                Working...