I know this is a stupid question, but I have been teaching myself c#, so im learning the lingo as I go.
My question is: Often when I write methods, I find my self declaring objects and variables at the top of the method when I need them to share the same scope. Do these types of objects/variables have a name? I know when declared outside a method they would be properties.
Example code of what I mean: My question is what to call the objects in the Question region.
public Label start_Ping(String target, string name, ref bool router)
{
#region [ Question ]
Label status_Label = new Label(); //Declare the label which will be dynamically created
Ping ping = new System.Net.NetworkInformation.Ping(); //Declare the ping object
PingReply reply = null; //Declare the pingReply object
byte[] buffer = new byte[100]; //Set the byte size
#endregion
if (name == "router")
{
try
{
reply = ping.Send(target, 50, buffer);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
if (reply.Status == IPStatus.Success)
{
router = true;
}
else
{
router = false;
return null;
}
}
try
{
...
Thanks in advance. I understand this may be quite simple =)
In your code,
status_Label,ping,replyandbufferare known simply as local variables.That is, they’re local to the method you create them in (or whatever scope you declare them in).
It doesn’t matter where in your method you declare them, by the way, as long as they’re within some scope.