Click here to Skip to main content
14,665,555 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

I have defined a struct in C#. Inside this struct I have an array of integers. This field is ImagesLoadStatus as below.

struct ProductImages
   {
       public string ImageB;
       public string ImageS;
       public string ImageOrg;
       public int[] ImagesLoadStatus;

  }


In another class I am initializing the array. Here:

ProductImages prdImages = new ProductImages();
for (int i = 0; i < 4; i++)
    prdImages.ImagesLoadStatus[i] = 0;

I build the application without error. But a warning message appears saying :


warning CS0649: Field 'ProjectName.ClassFolder.ProductImages.ImagesLoadStatus' is never assigned to, and will always have its default value null

I am already initializing he array in the struct, why do I get such a warning message ?

Thanks in advance,
Posted
Updated 27-Jan-11 4:13am
v2
Comments
OriginalGriff 27-Jan-11 10:29am
   
Please note, Nish and I have both been corrected by SAKryukov - you shouldn't use literal constants as loop guards. We didn't spot that - sorry!

Rate this:
Please Sign up or sign in to vote.

Solution 1

The array itself is never initialized. You need to do that too.

Example:

ProductImages prdImages = new ProductImages();
prdImages.ImagesLoadStatus = new int[ProductImages.MAXBUFFER];
int initValue = 7; // arbitrary example
int minCount = 5; // arbitrary example
for (int i = 0; i < Math.Min(minCount, prdImages.ImagesLoadStatus.Length); i++)
    prdImages.ImagesLoadStatus[i] = initValue;
   
v5
Comments
Close Network 27-Jan-11 10:13am
   
Î am already doing that. Here:
for (int i = 0; i < 4; i++)
prdImages.ImagesLoadStatus[i] = 0;
Nish Nishant 27-Jan-11 10:15am
   
No, you are not. That only sets the array members, not the array itself. Check my updated answer and look at the highlighted line of code.
Close Network 27-Jan-11 10:16am
   
OK, thanks a lot.
Nish Nishant 27-Jan-11 10:16am
   
You are welcome.
Sergey Alexandrovich Kryukov 27-Jan-11 10:25am
   
Nishant, not 4, replace with array size. Using 4 here would be a plain bug.
Thank you.
--SA
Nish Nishant 27-Jan-11 10:26am
   
I actually used 20 to be safe, but I reckon in his actual code he'd have a const int or something that he'd use.
Nish Nishant 27-Jan-11 10:27am
   
Ah you meant the for-loop, yeah I see what you mean :-)
OriginalGriff 27-Jan-11 10:28am
   
Yep, I made the same whoopsie, too!
Nish Nishant 27-Jan-11 10:29am
   
:-)
Nish Nishant 27-Jan-11 10:28am
   
Updated my answer, thanks.
Sergey Alexandrovich Kryukov 27-Jan-11 21:28pm
   
Nishant, why are yout still showing a code with 4? I see no point...
Nish Nishant 27-Jan-11 21:36pm
   
Updated the code. It now uses Math.Min (since he specifically wants 4 items irrespective of the array length). Thank you.
Sergey Alexandrovich Kryukov 27-Jan-11 22:10pm
   
Again, it misses the purpose. Why 20 then? Instead, OP could be noted about bad design.
Don't you think so?
--SA
Nish Nishant 28-Jan-11 9:39am
   
You are a tough guy to please, Kryukov :-)

Anyway, I have updated my answer, so it should be quite better now. Thank you.
Sergey Alexandrovich Kryukov 28-Jan-11 11:58am
   
Pleasing me is not your responsibility :-) I'm surprised you take it so seriously.
Thank anyway and best wishes!
--SA
Nish Nishant 28-Jan-11 11:59am
   
Well both you and me, we share a common aim to give the OP the best answer possible. So I actually enjoy the criticism I get from you :-)
Sergey Alexandrovich Kryukov 28-Jan-11 13:54pm
   
I can feel it. My pleasure. Good to talk with you.

Can I also say I am personally very much driven by criticism.
I think at school and especially at university (which is famous PhysTech) we had a very harsh but I think very productive cultural background. Should you ever dare to say that you became and expert in any topic, one of your best friends will take if for great honor to challenge you with some more difficult problem in order to dip your face in dirt and say: "you still think you really understand the matter well?". If your find it offensive nobody will respect you. People around will only respect you if you meet a challenge properly; they will respect you even more if your have enough bravery to say "I failed; now I understand it". Can you see why this is very productive? Very often you can learn from fellow students more then from professors (especially taking into account one of my course mates won a Nobel prize in 2010 and some part of other students around had decent skills).

Unfortunately, in US this presents some cultural barrier. Most of co-workers are educated by hearing "Good job!" in almost all cases and did not learn how to work on criticism to improve instead of protecting themselves. I see you work in US, too, so you should know that...

--SA
Rate this:
Please Sign up or sign in to vote.

Solution 2

When you use the array, you are assigning values to the individual elements.
prdImages.ImagesLoadStatus[i] = 0;

However, you do not assign the actual array. Add the one line:

ProductImages prdImages = new ProductImages();
prdImages.ImagesLoadStatus = new int[myArraySize];   // Add this line.
for (int i = 0; i < prdImages.ImagesLoadStatus.Length; i++)
    prdImages.ImagesLoadStatus[i] = 0;
And it should be fine.

[edit]Changed loop guard to array size - OriginalGriff[/edit]
   
v2
Comments
Close Network 27-Jan-11 10:19am
   
I have already added, thanks..
Sergey Alexandrovich Kryukov 27-Jan-11 10:24am
   
Griff, not 4, replace with myAttaySize. Using 4 here would be a plain bug.
Thank you.
--SA
OriginalGriff 27-Jan-11 10:27am
   
Good point - changed to use array size.
Sergey Alexandrovich Kryukov 27-Jan-11 21:27pm
   
My vote of 5. Thank you.
--SA
unolegg 14-Sep-15 0:50am
   
Coming from C background, it takes time getting used with the c# convention. Your example is concise and clear - Thank you!
Rate this:
Please Sign up or sign in to vote.

Solution 3

Your initial code or your constructor should really create this array and set default values, the code as it stands is quite fragile and requires all users to check for null, etc.
   
Comments
Close Network 27-Jan-11 14:20pm
   
I am sorry. I didn't quite understand your point ? Could you clarify a little bit by adding some code ? Thanks,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100