473,387 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Declare variable in IF case

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
4 2720
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

"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
"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: David T. Ashley | last post by:
Hi, In my project, I typically declare and define variables in the .H file, i.e. DECMOD_MAIN UINT8 can_message_201_status_global #ifdef MODULE_MAIN = HAS_NEVER_BEEN_RECEIVED #endif ;
8
by: Johm | last post by:
In my codes i am often referring to one and the same controls.Is it possible to declare these controls only once and then insert them in all my functions and subs? For example, i am often...
3
by: cody | last post by:
why foreach does always have to declare a new variable? I have to write foreach (int n in array){} but Iam not allowed to write: int n=0; foreach (n in array){}
6
by: Jakob Bieling | last post by:
Hi, I recently noticed that my compiler (VC++7.1) allows me to declare variables inside the expression for an if-statement, like so: if (int i = 0) { i = 1; }
3
by: Julian | last post by:
I need to convert db field values from "1 - Attempted-Successful" to "Yes", "2 - Attempted-Unsuccessful" to "No" and all other values need to be NULL-ed. So here's my code: Dim strQuery As...
20
by: Manuel | last post by:
hi, I have a problem, a stupid problem. I can't declare a variable of type byte. The g++ said that i have syntactic error in this line. The code is this: byte * variable; well, i think...
7
by: Andrew Tatum | last post by:
Alright, so I have this problem. I want to make it easy for me and others to be able to run a query and easily choose whether we want Merchants or NonMerchants. Previously, we would have to comment...
8
by: yinglcs | last post by:
Hi, I have the following code: colorIndex = 0; def test(): print colorIndex; This won't work. But it works if i do this:
2
by: johnf40 | last post by:
Good morning I hope this will be a simple one, but I am trying to write a view to query my SQL server database, setting a variable using "declare". The code runs fine and returns a number of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.