The name 'xyz' does not exist in the current context

yguyon28
yguyon28 used Ask the Experts™
on
I'm getting the following error: The name does not exist in the current context. in the following 2 lines bellow after I implement my if else statement. Not sure if there is a better way to write this ifelse statement as a tenary statement...

            var resultsContainer = GetReportResultsObject(searchCriteria, LoggedInUser, systemCultureInfo);
            var resultsContainer2 = GetReportResultsObject2(searchCriteria2, LoggedInUser, systemCultureInfo);


public ActionResult GenerateReportFromCache(string guidToken)
        {
            var objResponse = MemoryCache.Default.Get(guidToken) as ResponseObject;
            
            if (objResponse.Model.GetType().Equals(typeof(xxx.Models.CustomReports.DailyTechnicianRecapSearchCriteria)))
            {
                var searchCriteria = objResponse.Model as DailyEmployeeRecapSearchCriteria;
            }
            else
            {
                var searchCriteria2 = objResponse.Model as DailyTechnicianRecapSearchCriteria;
            }
           
           var systemCultureInfo = (System.Globalization.CultureInfo)ViewBag.SystemCultureInfo;

            var resultsContainer = GetReportResultsObject(searchCriteria, LoggedInUser, systemCultureInfo);
            var resultsContainer2 = GetReportResultsObject2(searchCriteria2, LoggedInUser, systemCultureInfo);


            XtraReport mainReport = new XtraReport();
            if (searchCriteria.ReportByTechnician)
            {
                DailyTechnicianRecapSearchCriteria techCriteria = new DailyTechnicianRecapSearchCriteria();
                techCriteria.Map(searchCriteria);
                techCriteria.FavoriteReportName = CustomReportsNames.DailyTechnicianRecap;
                mainReport = BuildTechnicianReportObject(techCriteria, LoggedInUser, systemCultureInfo);
            }
            else
            {
                searchCriteria.FavoriteReportName = CustomReportsNames.DailyEmployeeRecap;
                BuildReportObject(searchCriteria, LoggedInUser, systemCultureInfo, ref mainReport);
            }

            return GenerateReport(mainReport, searchCriteria.PreviewFlag);
        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AndyAinscowFreelance programmer / Consultant
Commented:
Your problem is with the lifetime of the variables:

{
                var searchCriteria = objResponse.Model as DailyEmployeeRecapSearchCriteria;
            }  //After going past this brace the variable searchCriteria is DESTROYED


You need something like this:
var searchCriteria;
var searchCriteria2;
            if (objResponse.Model.GetType().Equals(typeof(xxx.Models.CustomReports.DailyTechnicianRecapSearchCriteria)))
            {
                searchCriteria = objResponse.Model as DailyEmployeeRecapSearchCriteria;
            }
            else
            {
                searchCriteria2 = objResponse.Model as DailyTechnicianRecapSearchCriteria;
            }
AndyAinscowFreelance programmer / Consultant
Commented:
Note it still will give you problems because only one will be given a value BUT you attempt to use both later.

Author

Commented:
Yes that is my problem.. The logic is wrong.
Dustin SaundersCo-Founder and Chief Architect
Top Expert 2016
Commented:
Seems like you could implement an Interface on these objects and simplify a lot of this code, no?
Freelance programmer / Consultant
Commented:
You need to sort out the logic yourself.  Your existing code does not make sense so I can't really suggest how to fix it.
You attempt to fill two variables when the if statement means only one search criteria is set.  Of those two variables one is never used at all later.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial