Click here to Skip to main content
15,846,038 members
Please Sign up or sign in to vote.
4.00/5 (1 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.

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

  }


In another class I am initializing the array. Here:

C#
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 5: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!

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

Example:

C#
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;
 
Share this answer
 
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
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:

C#
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]
 
Share this answer
 
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!
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.
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900