简体   繁体   中英

C# Use of Unassigned local variable - out statement

I've looked for this problem but I don't know (and haven't found) how to solve it. Does anyone here know how to solve the problem? I'm using EMGU, but the issue is with the c# coding (I'm reasonably new to C#) - I think it's to do with the out statement as I haven't used them much:

Image<Gray, Byte> first_image;

if (start_at_frame_1 == true)
{
    Perform_custom_routine(imput_frame, out first_image);
}
else
{
     Perform_custom_routine(imput_frame, out second_image);
}

Comparison(first_image);

You must give default value to the variable:

Image<Gray, Byte> first_image = null;

Otherwise there is a chance you won't assign anything, if you pass second_image as the out parameter.

You have 3 options of doing it

Image<Gray, Byte> first_image = default(Image<Gray, Byte>);

or

Image<Gray, Byte> first_image = null;

or

Image<Gray, Byte> first_image = new Image<Gray, Byte>();

Don't forget to do this with second_image also.

The compiler is warning you, that you are about to use an unassigned variable in your call to Comparison . If start_at_frame_1 is false , your variable first_image would never be set.

You can address this by setting first_image = null either in the initialization or in the else block.

Or you could make it a return parameter from Perform_custom_routine.

Image<Gray, Byte> first_image;  

if (start_at_frame_1 == true)  
{  
    first_image = Perform_custom_routine(imput_frame, out first_image);  
}  
else  
{  
     first_image = Perform_custom_routine(imput_frame, out second_image);  
}  

If you use the 'out' keyword, you have to give the variable a value before passing it.

If you use the 'ref' keyword, you have to give the variable a value in the method you pass it to before returning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM