Question regarding C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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_MODE

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

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

Thanks
Franck
 
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 said:
Hi,

Please Could you tell me what make these 2 pieces of code behave
differently in visual studio .NET ?
#1
tcPacket.Data = new byte[1];
tcPacket.Data[0] = GET_CURRENT_MODE;

#2
tcPacket.Data = new byte[1]{GET_CURRENT_MODE};

During execution of the code, in case#1 tcPacket.Data allways point to
NULL while in case #2 tcPacket.Data don't.
 
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 said:
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 said:
Hi,

Please Could you tell me what make these 2 pieces of code behave
differently in visual studio .NET ?
#1
tcPacket.Data = new byte[1];
tcPacket.Data[0] = GET_CURRENT_MODE;

#2
tcPacket.Data = new byte[1]{GET_CURRENT_MODE};

During execution of the code, in case#1 tcPacket.Data allways point to
NULL while in case #2 tcPacket.Data don't.
Thanks
Franck
 
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 said:
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 said:
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 said:
Hi,

Please Could you tell me what make these 2 pieces of code behave
differently in visual studio .NET ?
#1
tcPacket.Data = new byte[1];
tcPacket.Data[0] = GET_CURRENT_MODE;

#2
tcPacket.Data = new byte[1]{GET_CURRENT_MODE};

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