Solved

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

Posted on 2011-02-23
8
833 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
8 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 125 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 125 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 5

Expert Comment

by: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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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.
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now