C# 类之间的返回值

C# 类之间的返回值,c#,C#,如何在表单上单击按钮,将被调用方法的返回发送给另一个类?这是我的伪代码,任何帮助都将不胜感激 [类库] using System; using System.Runtime.InteropServices; using System.Text; using System.Collections.Generic; using System.Linq; namespace Auto { GUID Info pu

如何在表单上单击按钮,将被调用方法的返回发送给另一个类?这是我的伪代码,任何帮助都将不胜感激

[类库]

    using System;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Collections.Generic;
    using System.Linq;

    namespace Auto
    {
        GUID Info
        public interface IAuto
    {
        string SendToOtherApp();

    }

    COM Info
    public class Auto : IAuto
    {

        public string tbox1;
        NAVForm frm1 = new NAVForm();

        public Auto()
        {
        }

        public string SendToOtherApp()
        {
            frm1.ShowDialog();
            tbox1 = NAVForm.UseThis();
            return tbox1;
        }
    }
}
[表格]

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

        private void NAVForm_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

            UseThis(textBox1.Text);

        }
        public string UseThis(string txt)
        {
            if (txt.Trim().Length != 0)
            {
                return txt;
            }
            else
            {
                return "didn't work";
            }
        }
    }
}
我想将publicstringusethis(stringtxt)的返回值输入到publicstringsendtootherapp()中,调用它的另一个系统可以看到它


我显然是C#新手,因此我也非常愿意接受对项目和最佳实践的全面批评。

我不确定我是否正确实现了您的目标,但下面的代码在从中调用时,会显示另一个带有文本框的模式表单,您可以在该文本框中输入一个值,然后关闭该模式表单,以在返回到第一个表单的文本框中找到该值,该表单调用模式表单的显示

类库

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections.Generic;
using System.Linq;

namespace Auto
{

    public interface IAuto
    {
        string SendToOtherApp();
    }

    public class Auto : IAuto
    {
        public string tbox1;
        NAVForm frm1 = new NAVForm();

        public Auto()
        {
        }

        public string SendToOtherApp()
        {
            frm1.ShowDialog();
            tbox1 = frm1.UseThis(frm1.textBox1.Text);
            return tbox1;
        }
    }
}
从中调用以显示模态形式的

namespace Auto
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Auto auto = new Auto();
            string returnedString = auto.SendToOtherApp(); // the string filled at the modal form text boxed will be returned to this variable
        }

    }
namespace Auto
{
    public partial class NAVForm : Form
    {
        public NAVForm()
        {
            InitializeComponent();
        }

        public string UseThis(string txt)
        {
            if (txt.Trim().Length != 0)
            {
                return txt;
            }
            else
            {
                return "didn't work";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            UseThis(textBox1.Text);
        }

    }
}
将显示为模态形式的形式

namespace Auto
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Auto auto = new Auto();
            string returnedString = auto.SendToOtherApp(); // the string filled at the modal form text boxed will be returned to this variable
        }

    }
namespace Auto
{
    public partial class NAVForm : Form
    {
        public NAVForm()
        {
            InitializeComponent();
        }

        public string UseThis(string txt)
        {
            if (txt.Trim().Length != 0)
            {
                return txt;
            }
            else
            {
                return "didn't work";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            UseThis(textBox1.Text);
        }

    }
}
请注意,NAVForm中textBox1的访问修饰符应设置为public,以便对class Auto可见


如果我误解了什么,请告诉我,以纠正它。

这就是我所做的,效果很好。在我们的ERP中,我运行codeunit,它调用与“OpenThis()”方法关联的自动化变量。我的表单打开,我在文本框中输入文本,单击OK,它关闭from,ERP弹出一个消息框,显示消息框中的文本。你的C#专家们对这个版本有什么看法?我对你对这个解决方案的想法很感兴趣,所以请让我知道

类库

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections.Generic;
using System.Linq;


namespace NavAutomation
{
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    [Guid("5D83B4FE-45E6-410E-A075-AD635F5F0354")]
    [ComVisible(true)]
    public interface INavAutomation
    {
        string HelloWorld();
        object OpenThis();
    }

    [ComVisible(true)]
    [Guid("B7806CE5-862A-4407-9A3E-14CE8A9FB83A")]
    [ClassInterface(ClassInterfaceType.None)]
    public class NavAutomation : INavAutomation
    {
        public NavAutomation()
        {
        }

        public object OpenThis()
        {
            using (var form = new NAVForm())
            {
                var result = form.ShowDialog();
                return form.RetVal1;
            }

        }
    }
}
形式

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 NavAutomation
{
    public partial class NAVForm : Form
    {

        public NAVForm()

        {

            InitializeComponent();
        }

        private void NAVForm_Load(object sender, EventArgs e)
        {

        }

        public string RetVal1 { get; set; }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim().Length != 0)
            {
                this.RetVal1 = textBox1.Text;
            }
            else
            {
                this.RetVal1 = "didn't work";
            }
            this.Close();
        }
    }
}

您知道如何访问其他类的函数吗?您的表单需要查看
Auto
类。要么它应该被传递给
NAVForm
的构造函数,要么它应该实例化
Auto
作为它自己的字段。不确定这是否是家庭作业,但我的提示是获取并保留对
IAuto
实例的引用,以便您调用方法……您说的“SendToOtherApp()是什么意思调用此“”的其他系统可以看到哪个?这不是作业,它是从我们的ERP(另一个系统)调用的。我们的目标是,当调用它时,一个表单打开,用户输入文本,然后单击“发送”,表单关闭并将文本从文本框返回到我们的ERP。因为这是我第一次真正进入C#,所以我已经走了这么远。如果有人觉得有一些示例代码,或者有一个博客对此进行了讨论,那就太好了,我也愿意添加一些伪代码,在这些代码中我可能会丢失调用或实例化。