Monday, January 25, 2016

TryGet method


Try/Get methods return a boolean and "out" a value. Could be later called without assigning the return value to a variable:

tracker.TryGetRunningVisitorTest(out recordedVisitorTest);

======================================


public override bool TryGetRunningVisitorTest(out TestDefinitionItem test)
        {
            string testId = string.Empty;

            try
            {
                if (!string.IsNullOrEmpty(Tracker.Visitor.Tags[_VisitorTestTagName]))
                {
                    //test for test not being in the web db
                    testId = Tracker.Visitor.Tags[_VisitorTestTagName];
                    Item testItem = Sitecore.Context.Database.GetItem(testId);

                    if (testItem != null)
                    {
                        test = new TestDefinitionItem(testItem);
                       
                        if (test.IsRunning)
                        {
                            return true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("Error reading a Visitor Tag " + _VisitorTestTagName, ex, this);
                test = null;
                return false;
            }

            test = null;
            return false;

        }

No comments:

Post a Comment