Declare variable in IF case

  • Thread starter Thread starter macneed
  • Start date Start date
M

macneed

how can i declare a variable inside a if case?

if(today != recordday)
{
string[] NewRecordOfToday;
}
.. other work...
..
..
if(today != recordday)
{
NewRecordOfToday[0] = "Fine";
]

and the variable can USE outside of if { }
if the if case not pass, the variable doesn't need...

THANK!!
 
macneed said:
how can i declare a variable inside a if case?

if(today != recordday)
{
string[] NewRecordOfToday;
}
.. other work...
..
..
if(today != recordday)
{
NewRecordOfToday[0] = "Fine";
]

and the variable can USE outside of if { }
if the if case not pass, the variable doesn't need...

THANK!!

There is nothing stopping you from declaring a variable inside an if block,
but this variable will not be accessible outside the scope of the if block.
This is by design.

If you want to preserve variables set inside the if block you need to keep a
reference to the variable outside the if block.

string[] NewRecordToday = null;

if(someCheck)
{
NewRecordToday = new string[10];
}

if(NewRecordToday != null)
{
string s = NewRecordToday[0];
}
 
how can i declare a variable inside a if case?

if(today != recordday)
{
 string[] NewRecordOfToday;}

. other work...
.
.
if(today != recordday)
{
NewRecordOfToday[0] = "Fine";
]

and the variable can USE outside of if { }
if the if case not pass, the variable doesn't need...

THANK!!

ypu have to declare the variable outside the if
 
macneed said:
how can i declare a variable inside a if case?

if(today != recordday)
{
string[] NewRecordOfToday;
}
. other work...
.
.
if(today != recordday)
{
NewRecordOfToday[0] = "Fine";
]

and the variable can USE outside of if { }
if the if case not pass, the variable doesn't need...

You can't conditionally declare a variable in that way. You can
conditionaly initialise it:-

string [] NewRecordOfToday;

if (today != recordday)
{
NewRecordOfToday = new String[2]; // Change 2 to what your actual dim
}

.... other code ...

if (today!= recordday)
{
NewRecordOfToday[0] = "Fine";
}
 
macneed said:
how can i declare a variable inside a if case?

if(today != recordday)
{
string[] NewRecordOfToday;
}

This IS correct. You can declare a variable in this way. But it is only
useful if you do some other work inside the { } block. The variable
disappears as soon as you reach the closing bracket.
[...]
if(today != recordday)
{
NewRecordOfToday[0] = "Fine";
}

This is NOT correct. The scope of a variable is limited to the block
that encloses it. If you want a variable to be accessible from both blocks,
you have to declare it OUTSIDE both blocks.
and the variable can USE outside of if { }
if the if case not pass, the variable doesn't need...

I'm not able to understand the preceding sentences. Perhaps you could
enlist someone else's help in revising your English.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top