-1

I'm just starting out and I have a question. How do I call a public void?

e.g.

int x, y = 2;   
namespace Blah
{
  public class blah blah
  {
    public void count
    {
      Console.WriteLine("Hi: " + y);
    }
    public void counting
    {
      z = x * y
    }
  }
}

P.S: I have tried this How do I call a non-static method from a static method in C#? and it doesn't work for me

SteveC
  • 15,808
  • 23
  • 102
  • 173
buck_eats_toast
  • 31
  • 1
  • 1
  • 3
  • My Code Is At http://pastebin.com/zArD1rfK Look at line 42 – buck_eats_toast Feb 07 '15 at 05:50
  • Where are x and y really located? What you have isn't legal code, because x and y aren't part of any class. Also, methods need to have parentheses after the name. – Joel Coehoorn Feb 07 '15 at 05:51
  • 1
    Litereally just "InvCounter(...)". Please read a basic tutorial on C#. – siride Feb 07 '15 at 05:51
  • While starting write a program, you must need to care for a correct syntax for any programming language (just like you care about a grammatical mistake in your English examination). Here's the [MSDN C# Tutorials](https://msdn.microsoft.com/en-us/library/aa288436(v=vs.71).aspx) – Rohit Prakash Feb 07 '15 at 06:03
  • Guys what I posted above was just an example of what I wanted to do, it had nothing to do with what I was actually doing :( – buck_eats_toast Feb 07 '15 at 09:56

2 Answers2

2

If it's a non-static class, you need to instantiate it first. Then to call its void methods, justCallThem().

public class blahblah
{

    int x, y = 2;   // this sets y as 2, but x is not set 

    public void count()
    {
        Console.WriteLine("Hi: " + y);
    }
    public void counting()
    {
        int z =  x * y;
        Console.WriteLine("Z = {0}", z);

    }

}

class Program
{
    static void Main(string[] args)
    {
        blahblah b = new blahblah(); //instantiate the object
        b.count();  //call a method on the object
        b.counting();

        Console.ReadKey();
    }

}    

// output:
// Hi: 2
// z = 0
Ric Gaudet
  • 898
  • 6
  • 16
1

You just need to call the method by the class object reference.

// Blah is a namespace
namespace Blah
{
    // blah is the class name
    public class blah
    {
        // x, y and z are member variables.
        int x, y = 2;   
        int z = 0;

        // SayHiis a public static method. Thus it dont requires the 'blah' object to call this method.
        public static void SayHi()
        {
            Console.WriteLine("Hi: " + y);
        }

        // counting is a public non static method. Thus it requires the 'blah' object to call this method.
        public void counting()
        {
            z = x * y;
        }
    }
}

// CallingMethod is the method where you want to call the counting method of class 'blah'
public void CallingMethod()
{
     // As said above, you need a object to call the 'counting' method.
     blah objBlah = new blah();

     // Now call the non static method using this object.
     objBlah.counting();

     // and here you can directly call the non-static method SayHi like,
     blah.SayHi();
}
Rohit Prakash
  • 1,975
  • 1
  • 14
  • 24