By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,876 Members | 1,159 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,876 IT Pros & Developers. It's quick & easy.

Declare variable in IF case

P: n/a
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!!
Aug 29 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Aug 29, 9:23*am, "macneed" <macn...@yahoo.com.hkwrote:
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
Aug 29 '08 #2

P: n/a

"macneed" wrote:
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];
}

--
Happy Coding!
Morten Wennevik [C# MVP]

Aug 29 '08 #3

P: n/a
"macneed" <ma*****@yahoo.com.hkwrote in message
news:48********@127.0.0.1...
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.

Aug 29 '08 #4

P: n/a

"macneed" <ma*****@yahoo.com.hkwrote in message
news:48********@127.0.0.1...
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";
}

--
Anthony Jones - MVP ASP/ASP.NET
Aug 29 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.