public virtual string replaceVariables(string expression)
    {
        int openIndex = expression.IndexOf(EvaluationConstants.OPEN_VARIABLE, StringComparison.Ordinal);

        if (openIndex < 0)
        {
            return(expression);
        }

        string replacedExpression = expression;

        while (openIndex >= 0)
        {
            int closedIndex = -1;
            if (openIndex >= 0)
            {
                closedIndex = replacedExpression.IndexOf(EvaluationConstants.CLOSED_VARIABLE, openIndex + 1, StringComparison.Ordinal);
                if (closedIndex > openIndex)
                {
                    string variableName = StringHelperClass.SubstringSpecial(replacedExpression, openIndex + EvaluationConstants.OPEN_VARIABLE.Length, closedIndex);

                    // Validate that the variable name is valid.
                    try
                    {
                        isValidName(variableName);
                    }
                    catch (System.ArgumentException iae)
                    {
                        throw new EvaluationException("Invalid variable name of \"" + variableName + "\".", iae);
                    }

                    string variableValue = getVariableValue(variableName);

                    string variableString = EvaluationConstants.OPEN_VARIABLE + variableName + EvaluationConstants.CLOSED_VARIABLE;

                    replacedExpression = EvaluationHelper.replaceAll(replacedExpression, variableString, variableValue);
                }
                else
                {
                    break;
                }
            }


            openIndex = replacedExpression.IndexOf(EvaluationConstants.OPEN_VARIABLE, StringComparison.Ordinal);
        }

        // If an open brace is left over, then a variable could not be replaced.
        int openBraceIndex = replacedExpression.IndexOf(EvaluationConstants.OPEN_VARIABLE, StringComparison.Ordinal);

        if (openBraceIndex > -1)
        {
            throw new EvaluationException("A variable has not been closed (index=" + openBraceIndex + ").");
        }

        return(replacedExpression);
    }