Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I got a compile error at the lsat line "The name "getPointer" does not exist in the current context"
Thanks in advance.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Delagate01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public class clsMaths
        {
            public delegate int PointerMaths(int i, int y);
            public PointerMaths getPointer(int intoperation)
            {
                PointerMaths objpointer = null;
                if (intoperation == 1)
                {
                    objpointer = Add;
                }
                else if (intoperation == 2)
                {
                    objpointer = Sub;
                }
                else if (intoperation == 3)
                {
                    objpointer = Multi;
                }
                else if (intoperation == 4)
                {
                    objpointer = Div;
                }
                return objpointer;
            }
            private int Add(int i, int y)
            {
                return i + y;
            }
            private int Sub(int i, int y)
            {
                return i - y;
            }
            private int Multi(int i, int y)
            {
                return i * y;
            }
            private int Div(int i, int y)
            {
                return i / y;
            }
        }
        private void Evaluate_Click(object sender, EventArgs e)
        {
            string resText = "";
            int intNumber1 = int.Parse(number1.Text);
            int intNumber2 = int.Parse(number2.Text);
            int intOperation = int.Parse(request.Text);
                     
           // int intResult = objMath.getPointer(intOPeration).Invoke(intNumber1, intNumber2);
            int intResult = intNumber1 + intNumber2;
            resText = string.Format("{0}", intResult);
            this.result.Text = resText;
            MessageBox.Show(getPointer(intOperation).Invoke(intNumber1, intNumber2));
          
        }
       
    }
}
Posted
Updated 2-Mar-11 5:56am
v3
Comments
R. Giskard Reventlov 2-Mar-11 11:19am    
What's the error? Unlikely anyone will copy and paste your code and try to recreate the problem: you need to give full information.
Yusuf 2-Mar-11 11:40am    
You need to provide textual description of your problem

In addition to Henry's answer:

You are trying to call a method declared in a nested class (so not from the correct class). Just comment the class declaration:
namespace Delagate01
{
    ...
        //remove the class declaration
        //public class clsMaths
        //{
              //keep everything inside
              ...

        //remove the last curly brace
        //}
    ...
}


Now it should compile.
If you want to encapsulate your functions in another class, then you will need to make your functions static for example, or instanciate the class to access its methods.
 
Share this answer
 
v2
Comments
Henry Minute 2-Mar-11 12:12pm    
I didn't really examine the whole of the OPs code, just the last line, since that's where they said the error was.

Yours is a much more complete explanation.
Olivier Levrey 2-Mar-11 12:15pm    
Well, not complete since I added something from yours!
Anyway, I think OP can fix everything now.
ephibr 2-Mar-11 12:26pm    
Thanks a lot but now I have a compile error on "void"
In the line " private void Evaluate_Click(object sender, EventArgs e)"
Thank you.
Espen Harlinn 4-Mar-11 12:39pm    
Good points, a 5 - should have been enough :D
Your getPointer method returns a PointerMaths object. MessageBox is expecting a string and so it complains.

You could try
C#
MessageBox.Show(getPointer(intOperation).Invoke(intNumber1, intNumber2).ToString());


Although you may get "WhateverTheNamespaceIs.PointerMaths" as the output if PointerMaths doesn'y have a ToString() override.
 
Share this answer
 
Comments
Olivier Levrey 2-Mar-11 12:10pm    
This is correct, but this is not the only error. You may have a look to my answer.
ephibr 2-Mar-11 12:30pm    
Thanks a lot but i still got the errot with "ToString()", I tried "Solution 1 and I have an error
at the line " private void Evaluate_Click(object sender, EventArgs e)" compile error on "void"
Henry Minute 2-Mar-11 13:54pm    
What is the wording of the error? We are not mind readers you know.
ephibr 2-Mar-11 14:00pm    
It OK now you were very helpfull
Thanks a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900