-1

First the code:

if (A1.Text != "" || A1.Text != null)
{
    string D1 = "<"+A1.Text+">";
}
else
{
    string D1 = "null";
}

Now I am trying to later reference D1.

Ex this.label1.Text = D1;

The reference and the if statement that defines the string are in the same parent block, so why can I not use D1?

MethodMan
  • 18,625
  • 6
  • 34
  • 52
user1720845
  • 322
  • 2
  • 14
  • 5
    Because variables defined in child blocks (scopes) don't survive higher than the block they are defined in. Otherwise your example would be a compiler error about defining it more than one time. Think of them as scopes, not blocks, the curly brackets define a new scope. – Ron Beyer Nov 24 '15 at 03:56
  • Declare D1 as a private variable of the class – Metaphor Nov 24 '15 at 03:56
  • 1
    @Metaphor There's no indication whatsoever that the variable's scope should be outside of the method. – Servy Nov 24 '15 at 04:07
  • 1
    You also need to change that `||` to `&&`. Otherwise if A1.Text is null, the first test !="" will be evaluate to true and then the OR will short circuit and you will enter the true portion of the if statement. Better still use: String.IsNullOrWhiteSpace(A1.Text). – Clay Ver Valen Nov 24 '15 at 05:15

4 Answers4

2

You need to move the declaration into the parent block, like this:

    string D1;
    if (A1.Text != "" || A1.Text != null)
    {
        D1 = "<"+A1.Text+">";
    }
    else
    {
        D1 = "null";
    }

    // Then you can reference `D1` later in the same method.
    this.label1.Text = D1;
Ruslan
  • 2,691
  • 1
  • 19
  • 29
0

A variables is accessible only in the defining scope or child scopes. restructure your code this way -

string D1;
if (A1.Text != "" || A1.Text != null)
    {
        D1 = "<"+A1.Text+">";
    }
    else
    {
        D1 = "null";
    }
Kapoor
  • 1,388
  • 11
  • 21
0

Adding to the answer above

Please check this link for more enlightment

Compilers are in the business of generating code which manages the storage of the data manipulated by that program. There are lots of different ways of generating code to manage memory, but over time two basic techniques have become entrenched.

The first is to have some sort of "long lived" storage area where the "lifetime" of each byte in the storage -- that is, the period of time when it is validly associated with some program variable -- cannot be easily predicted ahead of time. The compiler generates calls into a "heap manager" that knows how to dynamically allocate storage when it is needed and reclaim it when it is no longer needed.

The second is to have some sort of "short lived" storage area where the lifetime of each byte in the storage is well known, and, in particular, lifetimes of storages follow a "nesting" pattern. That is, the allocation of the longest-lived of the short-lived variables strictly overlaps the allocations of shorter-lived variables that come after it.

Local variables follow the latter pattern; when a method is entered, its local variables come alive. When that method calls another method, the new method's local variables come alive. They'll be dead before the first method's local variables are dead. The relative order of the beginnings and endings of lifetimes of storages associated with local variables can be worked out ahead of time.

For this reason, local variables are usually generated as storage on a "stack" data structure, because a stack has the property that the first thing pushed on it is going to be the last thing popped off.

So overall local variable are are short lived and usually stored in stack, why stack coz its efficient than others. Link for more info why stack. So you should increase its scope as suggested before

    string D1;
    if (A1.Text != "" || A1.Text != null)
    {
        D1 = "<"+A1.Text+">";
    }
Community
  • 1
  • 1
Sandip Bantawa
  • 2,822
  • 4
  • 31
  • 47
0

To simplyfy the code you can write it like this as well

if (!string.IsNullOrWhiteSpace(A1.Text))
{
    label1.Text = "<" + A1.Text + ">";
}
else
{
    label1.Text = "null";
}
Mohit S
  • 13,723
  • 6
  • 34
  • 69