Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Error :"The name (fname) doesn't exist in the current context"

Posted on 2011-02-23
8
Medium Priority
?
844 Views
Last Modified: 2012-05-11
I have code like below,but am trying to pas fname to search filter its throwing error

Error :"The name (fname) doesn't exist in the current context",some one enlighten me

if (username.Contains("."))
            {
                string Fname = username.Substring(0, username.IndexOf("."));
                Fname = Fname.Replace(" ", "");

                string[] splitString = username.Split(new char[] { '.' });
                username = splitString[splitString.Length - 1];

}


if (username.IndexOf(" ") > 0)

                    search.Filter = "(cn=" + username + ")";

                else

                      search.Filter = "(sn=" + username + ")(givenname=" + Fname + ")";

Open in new window

0
Comment
Question by:Sha1395
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 34965762
"FName" is declared within your "if" block, so is only available within that block. If you need access to it elsewhere then you need to move it out of the block:
string FName;    // declare here outside of the block
if (username.Contains("."))
            {
                Fname = username.Substring(0, username.IndexOf("."));
                Fname = Fname.Replace(" ", "");

                string[] splitString = username.Split(new char[] { '.' });
                username = splitString[splitString.Length - 1];

}


if (username.IndexOf(" ") > 0)

                    search.Filter = "(cn=" + username + ")";

                else

                      search.Filter = "(sn=" + username + ")(givenname=" + Fname + ")";

Open in new window

0
 
LVL 11

Assisted Solution

by:Sudhakar Pulivarthi
Sudhakar Pulivarthi earned 500 total points
ID: 34978884
Hi Sha1395,

In C# the variables declaration can have block scope. This is a good feature to control memory with in the method/function stack.
Primitively variables scope in a method was at method level, Hence the stack frame which is used by the variables in the method was not cleared until the execution completes the method. Suppose we use tremendous amount of variables in a method which are piled up in stack decreases the performance.

So C# supports block scope level which is specified with in { } brackets.
Hence variables declared instead if are removed from the stack once it comes out of if block causing the error which u raised. So when we need a variable to access outside the scope of the block need to be specified outside the block as suggested by carl_tawn.
0
 

Author Comment

by:Sha1395
ID: 34978938
Thanks a lot Sudhakar for your wonderful explanation but some how i figured it out and completed the code.

anyway thank  you so much.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 5

Expert Comment

by:Tom Knowlton
ID: 36122558
I've requested that this question be deleted for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
ID: 36122559
Author has figured out the same aolution which is suggested by carl_tawn and my self provided an explanation for the question asked for.

Hence solution assistence is provided by both the comments which is also accepted by the author.
0
 

Expert Comment

by:South Mod
ID: 36157528
All,
 
Following an 'Objection' by Sudhakar-Pulivarthi (at http://www.experts-exchange.com/Q_27177969.html) to the intended closure of this question, it has been reviewed by at least one Moderator and is being closed as recommended by the Expert.
 
At this point I am going to re-start the auto-close procedure.
 
Thank you,
 
SouthMod
Community Support Moderator
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question