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
Solved

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

Posted on 2011-02-23
8
838 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Keyboard 2 41
compare between entities in entity framework 8 94
C# MVC Insert Multiple Row into DB 2 30
Code works but breaks when I add one section 4 20
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

839 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