-1

I have a Class and Method like below

public class Wakeup : World
{
    public void MethodA(string)
        {
            Log.writeline("Wakeup World");
        }
}

And below one is another class and method in which I am trying to call "MethodA"

public class Hello : World
{
    public void MethodB()
        {
            Wakeup p = new Wakeup;
            p.MethodA(string);
        }
}

This Isn't working. Unable to call "MethodA" inside "MethodB"

Note : Both the classed are Inherited to some other class called "World"

Can anyone suggest me how can I achieve this ?

JAbdul
  • 59
  • 1
  • 6

3 Answers3

2

Create instance of first class inside second one correctly, also pass some string value instead string type in second class method call

 public class Wakeup : World
    {
       public void MethodA(string text)
           {
               Log.writeline(text);
           }
    }
 public class Hello : World
    {
        public void MethodB()
            {
                Wakeup p = new Wakeup();
                p.MethodA("Wakeup World");
            }
    }
Mostafiz
  • 7,243
  • 3
  • 28
  • 42
  • () was added properly. Still same Issue. Getting Below exception InnerException: System.NullReferenceException: Object reference not set to an instance of an object. – JAbdul Apr 25 '16 at 05:17
  • see my update you have to pass some text inside your method instead only string type – Mostafiz Apr 25 '16 at 05:19
1

You have a typo, you have not placed () at the end of instantiating your Wakeup class. It should be as follow:

Wakeup p = new Wakeup();

Another thing, you should not pass the type itself to the method, in other words do not pass the type word string, but rather a string value. A string value is placed within quotation " " marks, as follow: "Hello, World".

So the following code for you Class Hello should work. Note how I instantiated your Wakeup class, and passed a value to Method A. Here is the complete code:

public class Wakeup : World
{
    public void MethodA(string strValue)
    {
        Console.WriteLine(strValue);
    }
}
public class Hello : World
{
    public void MethodB()
    {
        Wakeup p = new Wakeup();
        p.MethodA("Hello, World");
    }
}
monstertjie_za
  • 7,277
  • 8
  • 42
  • 73
  • () was added properly. Still same Issue. Getting Below exception InnerException: System.NullReferenceException: Object reference not set to an instance of an object. – JAbdul Apr 25 '16 at 05:17
  • Look at this line in your code, if you have read my full answer, you would have noticed I mentioned this:p.MethodA(string); You cannot pass string to the MethodA, you need to pass a string value... And another thing, look at how I declared your MethodA. Copy this code, and see if it works – monstertjie_za Apr 25 '16 at 05:19
0

works great for me

public class World { }

public class Wakeup : World
{
    public void MethodA(string a)
    {
        Console.WriteLine("Wakeup World");
    }
}
public class Hello : World
{
    public void MethodB()
    {
        Wakeup p = new Wakeup();
        p.MethodA("Dsa");
    }
}
Tal
  • 700
  • 4
  • 11