473,405 Members | 2,141 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,405 software developers and data experts.

variable scope inside a method

Hi,

I'm having trouble with variable scope inside procedures.
this is pseudocode

LDAP NewUser;

if (something = true)
{
NewUser = LDAP.FindUser(use emailaddress)
this.textbox1 = NewUser.name
}
else
{
NewUser = LDAP.FindUser(use employeeID)
this.textbox1 = NewUser.name
}

I'd rather have this:

if (something = true)
{
NewUser = LDAP.FindUser(use emailaddress)
}
else
{
NewUser = LDAP.FindUser(use employeeID)
}

this.textbox1 = NewUser.name

however: i get the error unassigned use of NewUser. How do I declare NewUser
so that I can have access to its members outside of the if..else statement?

my real code has dozens of textboxes to fill and I don't want to duplicate
the same
statments twice.

thanks

chris
Nov 17 '05 #1
2 1921
Chris,

You would do this:

LDAP NewUser = null;

And then the rest of your code.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chris" <Ch***@discussions.microsoft.com> wrote in message
news:05**********************************@microsof t.com...
Hi,

I'm having trouble with variable scope inside procedures.
this is pseudocode

LDAP NewUser;

if (something = true)
{
NewUser = LDAP.FindUser(use emailaddress)
this.textbox1 = NewUser.name
}
else
{
NewUser = LDAP.FindUser(use employeeID)
this.textbox1 = NewUser.name
}

I'd rather have this:

if (something = true)
{
NewUser = LDAP.FindUser(use emailaddress)
}
else
{
NewUser = LDAP.FindUser(use employeeID)
}

this.textbox1 = NewUser.name

however: i get the error unassigned use of NewUser. How do I declare
NewUser
so that I can have access to its members outside of the if..else
statement?

my real code has dozens of textboxes to fill and I don't want to duplicate
the same
statments twice.

thanks

chris

Nov 17 '05 #2
Hi,

There is no need to set the variable to null , as it';s assigned in both
branches of the IF statement the compiler will not complain about it.

So the "use of unassigned variable" is not for that reason.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP12.phx.gbl...
Chris,

You would do this:

LDAP NewUser = null;

And then the rest of your code.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chris" <Ch***@discussions.microsoft.com> wrote in message
news:05**********************************@microsof t.com...
Hi,

I'm having trouble with variable scope inside procedures.
this is pseudocode

LDAP NewUser;

if (something = true)
{
NewUser = LDAP.FindUser(use emailaddress)
this.textbox1 = NewUser.name
}
else
{
NewUser = LDAP.FindUser(use employeeID)
this.textbox1 = NewUser.name
}

I'd rather have this:

if (something = true)
{
NewUser = LDAP.FindUser(use emailaddress)
}
else
{
NewUser = LDAP.FindUser(use employeeID)
}

this.textbox1 = NewUser.name

however: i get the error unassigned use of NewUser. How do I declare
NewUser
so that I can have access to its members outside of the if..else
statement?

my real code has dozens of textboxes to fill and I don't want to
duplicate
the same
statments twice.

thanks

chris


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
5
by: John Kelsey | last post by:
Back in the "old" C/C++ days, I used to declare static variables inside functions. Something like... // just a silly example to demonstrate the technique int foo(void) { static int NextVal =...
6
by: thomson | last post by:
Hi All, In a Singleton pattern , if we create an instance variable, and return it, whether this one also be a static variable , Can anyone give me insights on the Memory allocation Thanks in...
4
by: Ray | last post by:
Hello, I think I've had JavaScript variable scope figured out, can you please see if I've got it correctly? * Variables can be local or global * When a variable is declared outside any...
9
by: tai | last post by:
Hi. I'm looking for a way to define a function that's only effective inside specified function. Featurewise, here's what I want to do: bar_plugin_func = function() { ...; setTimeout(...);...
3
by: Mehmet Yavuz S. Soyturk | last post by:
Hello, consider the next code: var obj = {}; with(obj) { var x = 10; } print(x); print(obj.x);
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
2
by: Shraddha | last post by:
Can we declare extern variable as static? What will be the scope of the variable then? What if we change the value of the variable in some other function? Also can someone tell me that if we can...
5
by: chromis | last post by:
Hi there, I've recently been updating a site to use locking on application level variables, and I am trying to use a commonly used method which copies the application struct into the request...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.