2 Replies - 370 Views - Last Post: 04 December 2012 - 12:10 PM Rate Topic: -----

#1 narmer93  Icon User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 317
  • Joined: 13-March 08

can't call method with a double array parameter

Posted 04 December 2012 - 02:09 AM

OK guys, this is the code for a two operand calculator, i got most of the code from the professor, i understand the part where the code reads the input of .txt file, reads every line, and makes the calculations and give the resutls
but the problem for me is that i don't remember how to call the TwoOperandsExpressionAnalyzer method with the correct parameters


here's my code for now

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace BatchCalculator
{
    
    class Program
    {
  static void Main(string[] args)
        {
            BatchMathExpression();
            Console.ReadKey();
        }
  
        static void BatchMathExpression()
        {
            Console.WriteLine("Input file name...");
            string filename = Console.ReadLine();
            try
            {
                string[] buffer = File.ReadAllLines(filename);
             
                foreach (string line in buffer)
                {
                        double result = TwoOperandsExpressionAnalyzer(line,?????);
                     Console.WriteLine(line + " = " + result.ToString()); 
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
       


        static string TwoOperandsExpressionAnalyzer(string expression, ref  double[] Operands)
        {
            try
            {

                if (Operands.Length != 2)
                {
                    throw new Exception("The size of the Operands Array should be 2");
                }
                string ExpressionNoSp = Regex.Replace(expression, " ", "");
                char[] CharBuffer = ExpressionNoSp.ToCharArray();
                // This is a splitter.
                bool OperatorDone = false;
                string operand = "";
                string[] opBuffer = new string[2];
                int j = 0;
                for (int i = 0; i < CharBuffer.Length; i++)
                {
                    //Check if the Operator is already obtained, No need to
                    //check it. Also avoid consider the first chracter as operator.
                    if (i > 0 && !OperatorDone)
                    {
                        if (CharBuffer[i] == '-')
                        {
                            OperatorDone = true;
                            operand = "-";
                            j = 1;
                            continue;
                        }
                        if (CharBuffer[i] == '+')
                        {
                            OperatorDone = true;
                            operand = "+";
                            j = 1;
                            continue;
                        }
                        if (CharBuffer[i] == '*')
                        {
                            OperatorDone = true;
                            operand = "*";
                            j = 1;
                            continue;
                        }
                        if (CharBuffer[i] == '/')
                        {
                            OperatorDone = true;
                            operand = "/";
                            j = 1;
                            continue;
                        }
                    }
                    // Continue add the charcters stream to operand buffer
                    //till got the operator, then switch the chracter straem to the next buffer.
                    opBuffer[j] += CharBuffer[i].ToString();
                }
                // Convert the charcetr array to double array.
                for (int k = 0; k < 2; k++)
                {
                    Operands[k] = Convert.ToDouble(opBuffer[k]);
                    Console.WriteLine(opBuffer[k]);
                }
                return operand;
            }
            catch (Exception)
            {
                return "BAD";
            }
        }
    }
}



the problem is mainly for me in line 29, and i don't really understand the second parameter of line 41 and line 102

any help will be really appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: can't call method with a double array parameter

#2 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,519
  • Joined: 08-April 09

Re: can't call method with a double array parameter

Posted 04 December 2012 - 04:35 AM

The second parameter is a ref.

The ref keyword causes an argument to be passed by reference, not by value. The effect of passing by reference is that any change to the parameter in the method is reflected in the underlying argument variable in the calling method. The value of a reference parameter is always the same as the value of the underlying argument variable.

double result = TwoOperandsExp<b></b>ressionAnalyzer(line,?????)

This should be written like this
double result = TwoOperandsExp<b></b>ressionAnalyzer(line,ref double[] Operands)

Was This Post Helpful? 0
  • +
  • -

#3 narmer93  Icon User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 317
  • Joined: 13-March 08

Re: can't call method with a double array parameter

Posted 04 December 2012 - 12:10 PM

View PostMichael26, on 04 December 2012 - 04:35 AM, said:

The second parameter is a ref.

The ref keyword causes an argument to be passed by reference, not by value. The effect of passing by reference is that any change to the parameter in the method is reflected in the underlying argument variable in the calling method. The value of a reference parameter is always the same as the value of the underlying argument variable.

double result = TwoOperandsExp<b></b>ressionAnalyzer(line,?????)

This should be written like this
double result = TwoOperandsExp<b></b>ressionAnalyzer(line,ref double[] Operands)


thanks buddy, i figured it out already
that's the code that worked for me

  double[] opr = new double[2];
string operand = TwoOperandsExpressionAnalyzer(line, ref opr);

that's the way i figured out to get something out of a function other than the return method
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1