Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subroutines in C#

I'm a bit new to C#, and not quite sure how to call a subroutine. Here's what I'm trying to do:

private void button1_Click(object sender, EventArgs e)
{
    // Call whatever subroutine you like
    StartExstream();
}

public void StartExstream()
{
    // Do Stuff Here
}

Unfortunately for me, this doesn't work. I'm getting a "Only assignment, call, increment, decrement, and new object expressions can be used as a statement" error.

How do I call my StartExstream sub from my Button1_Click event?

Thanks, Jason

EDIT:

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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // Call whatever subroutine you like
         StartExstream();
    }



    public void StartExstream()
    {
        tcpExstream.Service1Client MyTCP = new tcpExstream.Service1Client();

        string ExStreamPath;
        string datPath;
        string optPath;

        // My Working Arguments
        ExStreamPath = @"C:\Program Files\Exstream\Dialogue 6.1\Engine.exe";
        datPath = @"-FILEMAP=DataFile,\\Dev-srv1\Exstream\LetterWriterApp\Input Files\Data Files\SAVEezkazivaftf40s452ndayb45.dat";
        optPath = @"-CONTROLFILE=C:\Exstream\Development\LetterWriter\ControlFiles\Letter.opt";

        // Hong's Arguments
        //ExStreamPath = @"C:\Program Files\Exstream\Dialogue 6.1\Engine.exe";
        //datPath = @"-FILEMAP=DataFile,C:\Exstream\development\AGDocGenerator\TempFiles\DataFiles\Data_456231_1598.xml";
        //optPath = @"-CONTROLFILE=C:\Exstream\development\AGDocGenerator\ExstreamDialogue\ControlFiles\AGDocGenerator.opt";

        // Kick It!
        MyTCP.StartExStream(datPath, optPath, ExStreamPath);

        // Extra line of code for breaking point
        optPath = "nothing";
    }
}

}


2 Answers

First

If the Routine is in the same class than there you can directly call the routine by just writing name of it

Example

private void AnotherMethod()
    {
        // Call whatever subroutine you like
         MyRutine();
    }

Second

If its not in the same class you need to create instance of the class which contains routine and than you can use that object to call you routine

Example

MyClass c = new MyClass();
c.MyRutine();
like image 57
Pranay Rana Avatar answered Jan 20 '26 02:01

Pranay Rana


You have to have this all in a namespace and then in a class for this to work.

namespace some.namespace
{
   public class myclass
   {
        private void button1_Click(object sender, EventArgs e)
        {         // Call whatever subroutine you like         
              StartExstream();     
        }
        public void StartExstream()
        {         // Do Stuff Here     
        } 
   }
}
like image 28
Bueller Avatar answered Jan 20 '26 00:01

Bueller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!