Question regarding C#

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

    #1

    Question regarding C#

    Hi

    Please Could you tell me what make these 2 pieces of code behave differently in visual studio .NET

    #
    tcPacket.Data = new byte[1]
    tcPacket.Data[0] = GET_CURRENT_MOD E

    #
    tcPacket.Data = new byte[1]{GET_CURRENT_MO DE}

    During execution of the code, in case#1 tcPacket.Data allways point to NULL while in case #2 tcPacket.Data don't

    Thanks
    Franck
  • Michael Giagnocavo [MVP]

    #2
    Re: Question regarding C#

    In case 1, the byte[] consists of intialized data when you assign it to the
    property. My guess is that set_Data (if .Data is a property) is checking
    for just an "empty" array, and setting it to null.

    However, if Data was a local or a field, they'd not be null at either
    points.

    What type is tcPacket? Can you make a short but complete program to
    reproduce this?

    -mike
    MVP

    "Franck Sodogandji" <fsodogandji@ho tmail.com> wrote in message
    news:24892836-7B25-476A-A604-F72FBAEF40A8@mi crosoft.com...[color=blue]
    > Hi,
    >
    > Please Could you tell me what make these 2 pieces of code behave[/color]
    differently in visual studio .NET ?[color=blue]
    >
    > #1
    > tcPacket.Data = new byte[1];
    > tcPacket.Data[0] = GET_CURRENT_MOD E;
    >
    > #2
    > tcPacket.Data = new byte[1]{GET_CURRENT_MO DE};
    >
    > During execution of the code, in case#1 tcPacket.Data allways point to[/color]
    NULL while in case #2 tcPacket.Data don't.[color=blue]
    >
    > Thanks
    > Franck[/color]


    Comment

    • Mitchell Geere

      #3
      Re: Question regarding C#

      Well, this is just my 5c but isnt it a case of #1 is getting intialized to
      null (ex MyClass mc = new MyClass() <-- new instance returns null) so in
      this case #1 returns null and #2 get intialised and assigned so it isnt
      null...

      I hope this is right.

      Mitch



      "Michael Giagnocavo [MVP]" <mggUNSPAM@Atre vido.net> wrote in message
      news:#7racnIsDH A.2408@tk2msftn gp13.phx.gbl...[color=blue]
      > In case 1, the byte[] consists of intialized data when you assign it to[/color]
      the[color=blue]
      > property. My guess is that set_Data (if .Data is a property) is checking
      > for just an "empty" array, and setting it to null.
      >
      > However, if Data was a local or a field, they'd not be null at either
      > points.
      >
      > What type is tcPacket? Can you make a short but complete program to
      > reproduce this?
      >
      > -mike
      > MVP
      >
      > "Franck Sodogandji" <fsodogandji@ho tmail.com> wrote in message
      > news:24892836-7B25-476A-A604-F72FBAEF40A8@mi crosoft.com...[color=green]
      > > Hi,
      > >
      > > Please Could you tell me what make these 2 pieces of code behave[/color]
      > differently in visual studio .NET ?[color=green]
      > >
      > > #1
      > > tcPacket.Data = new byte[1];
      > > tcPacket.Data[0] = GET_CURRENT_MOD E;
      > >
      > > #2
      > > tcPacket.Data = new byte[1]{GET_CURRENT_MO DE};
      > >
      > > During execution of the code, in case#1 tcPacket.Data allways point to[/color]
      > NULL while in case #2 tcPacket.Data don't.[color=green]
      > >
      > > Thanks
      > > Franck[/color]
      >
      >[/color]


      Comment

      • Michael Giagnocavo [MVP]

        #4
        Re: Question regarding C#

        Case #1 creates a new array, and assigns it to the property (I'm guessing
        it's a property?). I'm not aware of any case where the newarr will return
        null. It'd have to throw an exception if there was a problem.

        I compiled this (using a local): The only difference when they are compiled
        is that the 2nd one has a non-zero (I'm guessing non-zero) value for its
        only element, while case 2 assigns an array with a zero value for its
        element, then assigns another value to the element.

        -mike
        MVP

        "Mitchell Geere" <mgeere@telkoms a.net> wrote in message
        news:eXKedTesDH A.3416@tk2msftn gp13.phx.gbl...[color=blue]
        > Well, this is just my 5c but isnt it a case of #1 is getting intialized to
        > null (ex MyClass mc = new MyClass() <-- new instance returns null) so in
        > this case #1 returns null and #2 get intialised and assigned so it isnt
        > null...
        >
        > I hope this is right.
        >
        > Mitch
        >
        >
        >
        > "Michael Giagnocavo [MVP]" <mggUNSPAM@Atre vido.net> wrote in message
        > news:#7racnIsDH A.2408@tk2msftn gp13.phx.gbl...[color=green]
        > > In case 1, the byte[] consists of intialized data when you assign it to[/color]
        > the[color=green]
        > > property. My guess is that set_Data (if .Data is a property) is[/color][/color]
        checking[color=blue][color=green]
        > > for just an "empty" array, and setting it to null.
        > >
        > > However, if Data was a local or a field, they'd not be null at either
        > > points.
        > >
        > > What type is tcPacket? Can you make a short but complete program to
        > > reproduce this?
        > >
        > > -mike
        > > MVP
        > >
        > > "Franck Sodogandji" <fsodogandji@ho tmail.com> wrote in message
        > > news:24892836-7B25-476A-A604-F72FBAEF40A8@mi crosoft.com...[color=darkred]
        > > > Hi,
        > > >
        > > > Please Could you tell me what make these 2 pieces of code behave[/color]
        > > differently in visual studio .NET ?[color=darkred]
        > > >
        > > > #1
        > > > tcPacket.Data = new byte[1];
        > > > tcPacket.Data[0] = GET_CURRENT_MOD E;
        > > >
        > > > #2
        > > > tcPacket.Data = new byte[1]{GET_CURRENT_MO DE};
        > > >
        > > > During execution of the code, in case#1 tcPacket.Data allways point to[/color]
        > > NULL while in case #2 tcPacket.Data don't.[color=darkred]
        > > >
        > > > Thanks
        > > > Franck[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        Working...