Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

c# - Warning "Fields .... is never assigned to and will always have its default value null"

I am trying to insert the data into the database but the values inserted in the database are displayed null. There are also warnings like "Fields (x) is never assigned to and will always have its default value null" ( x = response1, response2, response3, response4, response5)

private void YesOrNoChecked(RadioButton radioButtonYes, RadioButton radioButtonNo,string response,int questionNo)
        {
            if (radioButtonYes.Checked)
            {
                response = "Yes";
            }
            else if (radioButtonNo.Checked)
            {
                response = "No";
            }
            else
            {
                MessageBox.Show("Question " + questionNo + " is not answered", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                isChecked = false;
            }
        }

private void SatisfactionChecked(RadioButton rdoVerySatisfied, RadioButton rdoSatisfied, RadioButton rdoNeutral, 
            RadioButton rdoDissatisfied, RadioButton rdoVeryDissatisfied,string response,int questionNo)
        {
            if (rdoVerySatisfied.Checked)
            {
                response = "Very Satisfied";
            }
            else if (rdoSatisfied.Checked)
            {
                response = "Satisfied";
            }
            else if (rdoNeutral.Checked)
            {
                response = "Neutral";
            }
            else if (rdoDissatisfied.Checked)
            {
                response = "Dissatisfied";
            }
            else if (rdoVeryDissatisfied.Checked)
            {
                response = "Very Dissatisfied";
            }
            else
            {
                MessageBox.Show("Question " + questionNo + " is not answered", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                isChecked = false;
            }
        } 

I called the functions...

    YesOrNoChecked(rdoYes1, rdoNo1, response1, 1); 
    YesOrNoChecked(rdoYes2, rdoNo2, response2, 2);  
    SatisfactionChecked(rdoVerySatisfied3, rdoSatisfied3, rdoNeutral3, rdoDissatisfied3, rdoVeryDissatisfied3, response3, 3);
    SatisfactionChecked(rdoVerySatisfied4, rdoSatisfied4, rdoNeutral4, rdoDissatisfied4, rdoVeryDissatisfied4, response4, 4);  
    SatisfactionChecked(rdoVerySatisfied5, rdoSatisfied5, rdoNeutral5, rdoDissatisfied5, rdoVeryDissatisfied5, response5, 5); 

I insert the data into the database

consumerResponsesTableAdapter.InsertConsumerResponses(lblSurveyDate.Text,
                        consumerID, ConsumerMenu.surveyID, response1, response2, response3, response4, response5, response6);

Is there any way to fix this?

question from:https://stackoverflow.com/questions/65939211/warning-fields-is-never-assigned-to-and-will-always-have-its-default-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You seem to be under the misunderstanding that if you assign a value to a C# method parameter, that the value will affect the variable passed as that parameter by the calling method. In pass-by-value semantics this is not the case.

You wrote:

    private void YesOrNoChecked(RadioButton radioButtonYes, RadioButton radioButtonNo,string response, ...)
    {
        if (radioButtonYes.Checked)
        {
            response = "Yes";
        }

You might call it like:

string x = "x";
YesOrNoChecked(rb1, rb2, x, ...);

But x will never, ever have "Yes" or "No" in it, it will always only ever have "x" in it.

The reason for this is because, even though x and response start out referring to the same thing when you call the method (they both refer to "x") your assigning response = "Yes" makes a new thing ("Yes") leaving x pointing to the old thing ("x"), then when the method finishes, response is deleted, so the "Yes" is lost, and x always only ever pointed to "x"

In steps:
x --> "x"                               Before you call the method
x --> "x" <-- response                  As soon as you call the method
x --> "x"     response --> "Yes"        During the method
x --> "x"     response --> (is lost)    After the method finishes

To get it to work like that, you could use out modifier but I wouldn't recommend it (there are nearly never any reasons to use out parameters in C#). Instead, modify your methods to return the "Yes" or "No" and capture it when you call the method:

    private string YesOrNoChecked(RadioButton radioButtonYes, RadioButton radioButtonNo)
    {
        if (radioButtonYes.Checked)
        {
            return "Yes";
        }

and use it like:

string x = YesOrNoChecked(rb1,rb2);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...