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

variable scope inside a method

P: n/a
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
Share this Question
Share on Google+
2 Replies

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.