Click here to Skip to main content
16,009,728 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am relatively new to C#. I actually stopped coding just after VB6. I was wondering if someone could assist me. I cannot for the life of me figure out how to call methods or functions that are in a class.

So Ill share code below I have in hopes someone could maybe point me in the right direction.
On Button click Id like to call the function for the following code I have written.
Kind of like using a .bas and calling the function Library.FunctionName in VB. Anyways thanks for your help!

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ServiceProcess;
using System.IO;
using System.Management;

namespace TunerReconstruct
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                ServiceController myService = new ServiceController();
                myService.ServiceName = "Service1";
                string svcStatus = myService.Status.ToString();
                myService.Stop();
                MessageBox.Show("Service1 Service is now stopping.", "Windows Service Controller", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

          // This will catch any unexpected errors while stopping the windows service.
            catch (Exception ex)
            {
                string strMessage = ex.Message;
                MessageBox.Show("An error occured stopping the Service1 Service" + strMessage, "Service1 Repair Controller", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }
    }
}


I have a Class in my project named Library,
Id like the code for Button 1 Click in the Class

When the user clicks button one, Id like it call the Class Function so I can add multiple functions with try so I can see what sections may error out.
Posted
Comments
[no name] 22-Jun-14 10:00am    
"how to call methods or functions that are in a class", it's not that hard. You need an instance of the class to work with.

Library lib = new Library();
lib.NameOfMethodToCall();
FMG Tech 22-Jun-14 10:06am    
Thanks Wes, pardon me for sounding absolutely stupid. So If I take the code I have and put it in a class like the following.

class Library
{
private void StopService();
{//Code}
}

I can call it using your code
lib.stopservice();

after declaring it?
[no name] 22-Jun-14 10:22am    
No for 2 reasons. You declared your method as private so you cannot access outside of your class and C# is case sensitive. StopService and stopservice are 2 different things.
FMG Tech 22-Jun-14 11:34am    
Wes thank you very much!

It looks like this now in my Class;
namespace TunerReconstruct
{
class Library
{
public void StopService()
{
try
{
ServiceController myService = new ServiceController();
myService.ServiceName = "Service1";
string svcStatus = myService.Status.ToString();
myService.Stop();
}

// This will catch any unexpected while repairing Marimba Files
catch (Exception ex)
{
string strMessage = ex.Message;
MessageBox.Show("An error occured stopping Service " + strMessage, "Marimba Repair Controller", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}


On the button click;

private void button1_Click(object sender, EventArgs e)
{
Library lib = new Library();
lib.StopService();
}


Your awesome sir!
[no name] 22-Jun-14 14:35pm    
You're welcome!

Please don't spread that around. I have a reputation to maintain... :-)

1 solution

There are two ways to access class methods (assuming that the access specifiers permit you to, but let's assume that all the methods are public which means that any class can access them).

The first is a static access:
C#
int result = Library.Multiply( 2, 5 );

This requires a static definition:
C#
public class Library
   {
   private int myValue = 666;
   public static int Multiply(int a, int b) { return a * b; }
   }
The disadvantage of this is that the Multiply method can only access static members of the Library class: it cannot access class instance members. Thus, Multiply cannot access myValue.

The other is to use an instance of the Library:
C#
Library lib = new Library();
int i = lib.Multiply( 2, 10 );

This requires an instance definition:
C#
public class Library
   {
   private int myValue = 666;
   public int Multiply(int a, int b) { return a * b; }
   }
In this version, Multiply can access myValue if it needs to, because it has access to all the instance members of the Library class.
 
Share this answer
 

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