Solved

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

Posted on 2011-02-23
8
843 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

632 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