我对C#有点新,并且不太确定如何调用子程序。这就是我想要做的事情:
private void button1_Click(object sender, EventArgs e)
{
// Call whatever subroutine you like
StartExstream();
}
public void StartExstream()
{
// Do Stuff Here
}
对我来说不幸的是,这不起作用。我得到一个“只有赋值,调用,递增,递减和新对象表达式可以用作语句”错误。
如何从我的Button1_Click事件调用我的StartExstream子?
谢谢, 杰森
修改
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";
}
}
}
答案 0 :(得分:1)
你必须将这一切都放在一个命名空间中,然后放在一个类中才能使它工作。
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
}
}
}
答案 1 :(得分:1)
首先
如果例程与那里的例程相同,你可以直接调用例程,只需要写出它的名字
Example
private void AnotherMethod()
{
// Call whatever subroutine you like
MyRutine();
}
<强>第二强>
如果它不在同一个类中,你需要创建包含例程的类的实例,而不是你可以使用该对象来调用例程
Example
MyClass c = new MyClass();
c.MyRutine();
答案 2 :(得分:0)
好的,根据更多帖子,你的代码
tcpExstream.Service1Client MyTCP = new tcpExstream.Service1Client();
正在尝试创建一个类型不是类型但是类型成员的对象。
相反,你会使用像
这样的东西WhateverTypeTcpExstreamIs MyTCP = new WhateverTypeTcpExstreamIs();
MyTCP.Service1Client = tcpExstream.Service1Client();