Home > Net >  C # loop variable problem
C # loop variable problem

Time:10-01

For (int I=1; I & lt; 5; I++)
{
Var name="radioButton" + I;
String n=this. Controls [name] Tag. The ToString ();

If (n=="Number")
{

MessageBox. Show (" 1111 ");

}
This is why an error "System. NullReferenceException" type of untreated exception?

CodePudding user response:

http://www.cnblogs.com/alexis/archive/2010/11/06/1870519.html visual debugged, enclosing Controls [name] Tag this question,

CodePudding user response:

RadioButton one controls the tag is NULL, or no 4 radioButton

CodePudding user response:

refer to the second floor ManBOyyy response:
radioButton one controls the tag is NULL, or no 4 radioButton

I manually tag assignment but the string n value is null

CodePudding user response:

reference weixin_44949408 reply: 3/f
Quote: refer to the second floor ManBOyyy response:

RadioButton one controls the tag is NULL, or no 4 radioButton

I manually tag the assignment but the value is null string n

In the first place? You delete all the radioButton, then keep radioButton1, then assignment radioButton1 Tag value, then the
For (int I=1; I & lt; 2; I++)
It would be successful

CodePudding user response:

reference 4 floor ManBOyyy response:
Quote: reference weixin_44949408 reply: 3/f

Quote: refer to the second floor ManBOyyy response:

RadioButton one controls the tag is NULL, or no 4 radioButton

I manually tag the assignment but the value is null string n

In the first place? You delete all the radioButton, then keep radioButton1, then assignment radioButton1 Tag value, then the
For (int I=1; I & lt; 2; I++)
It would be successful

So what is this principle?

CodePudding user response:

reference 5 floor weixin_44949408 reply:
Quote: refer to 4th floor ManBOyyy response:

Quote: refer to the third floor weixin_44949408 response:

Quote: refer to the second floor ManBOyyy response:

RadioButton one controls the tag is NULL, or no 4 radioButton

I manually tag the assignment but the value is null string n

In the first place? You delete all the radioButton, then keep radioButton1, then assignment radioButton1 Tag value, then the
For (int I=1; I & lt; 2; I++)
It would be successful

So what is this principle?

O determine the NULL values
For (int I=1; I & lt; 5; I++)
{
Var name="radioButton" + I;
If (this. Controls [name]!=null)
{
If (this. Controls [name] Tag!=null)
{
String n=this. Controls [name] Tag. The ToString ();
If (n=="Number")
{
MessageBox. Show (" 1111 ");
}
}
}

}

CodePudding user response:

You'll know one debugging,
Is this. Controls [name] is null

CodePudding user response:

The tag is an empty

CodePudding user response:

String n=this. Controls [name] Tag. The ToString ();
The breakpoint

CodePudding user response:

 
This. Controls [name]


Verify the place

CodePudding user response:

Good job
  •  Tags:  
  • C#
  • Related