C# C语言中的子程序#
我对C#有点陌生,不太清楚如何调用子例程。以下是我想做的:C# C语言中的子程序#,c#,function,call,subroutine,C#,Function,Call,Subroutine,我对C#有点陌生,不太清楚如何调用子例程。以下是我想做的: private void button1_Click(object sender, EventArgs e) { // Call whatever subroutine you like StartExstream(); } public void StartExstream() { // Do Stuff Here } 不幸的是,这不起作用。我得到一个“只能将赋值、调用、递增、递减和新对象表达式用作语句”错
private void button1_Click(object sender, EventArgs e)
{
// Call whatever subroutine you like
StartExstream();
}
public void StartExstream()
{
// Do Stuff Here
}
不幸的是,这不起作用。我得到一个“只能将赋值、调用、递增、递减和新对象表达式用作语句”错误
如何从Button1_Click事件调用StartExstream sub
谢谢,
杰森
编辑:
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";
}
}
}您必须将所有这些都放在一个名称空间中,然后放在一个类中才能工作
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
}
}
}
首先 如果例程在同一个类中,那么您可以直接通过编写例程的名称来调用它
Example
private void AnotherMethod()
{
// Call whatever subroutine you like
MyRutine();
}
秒
如果它不在同一个类中,则需要创建包含例程的类的实例,然后可以使用该对象调用例程
Example
MyClass c = new MyClass();
c.MyRutine();
好的,根据进一步的帖子,你的代码
tcpExstream.Service1Client MyTCP = new tcpExstream.Service1Client();
正在尝试创建类型不是类型而是类型成员的对象
相反,您将使用类似
WhateverTypeTcpExstreamIs MyTCP = new WhateverTypeTcpExstreamIs();
MyTCP.Service1Client = tcpExstream.Service1Client();
代码看起来很好,您一定是忽略了带有问题的代码,并发布了完整的类代码。你这里的代码看起来不错。我看不出你的代码有任何问题。如果你想让这里的人帮助你,请发布调用者和被调用者函数的完整代码。你不需要名称空间(但它们必须在同一个类中才能在没有typename或object的情况下调用)。奇怪的是——我一直遇到上述错误,直到我删除了上面的(空)行代码和(空)行代码下面是一行代码。重新打了一遍,答对了!没有错误。奇怪。但是谢谢你的帮助!