Use of Unassigned local variable

前端 未结 3 1252
离开以前
离开以前 2021-01-26 11:52

I encountered an error. Despite declaring the variables (failturetext and userName) errors still appear. Can anyone please help me?

- Use of Unassigned local va         


        
相关标签:
3条回答
  • 2021-01-26 12:05

    Regarding the compiler message - look at this link

     TextBox FailureText; - you declared a variable, but it is not initialized. you might set it to null / a value / default() - but you must initialize it
     TextBox UserName; - the same
    

    when you call UserName.Text - the compiler alerts for the uninitialized UserName

    0 讨论(0)
  • 2021-01-26 12:13

    If you wish to keep the same convention as you have at the moment, then to solve the issue of it failing compilation you want to assign the two variables a null value first before you try to reference them.

    Like so:

    TextBox FailureText = null;
    TextBox UserName = null;
    

    If you do this, you might need to check on whether they are still null when you try and assign the properties of these objects.

    As people above have stated, you should not be doing it this way as UI controls should always be global to the form itself.

    0 讨论(0)
  • 2021-01-26 12:17

    You've declared the variables, but you haven't assigned anything to them before you are attempting to reference their properties. That's the origin of the messages. You've got bigger problems, though, because those represent UI controls and it's highly unlikely that they should be local variables in your function. They probably ought to belong to the page (or form) rather than be declared locally.

    0 讨论(0)
提交回复
热议问题