简体   繁体   English

在Windows窗体中使用控制台中的一个变量

[英]Using one variable from a console in a windows form

I have this project: 我有这个项目:

在此处输入图片说明

And I need to use a variable that is in the "TransactionHandler.cs" in the "Enviar Faturas.cs" the TransactioHandler is a class library and the Enviar Faturas is a Windows Form. 我需要使用“ Enviar Faturas.cs”中“ TransactionHandler.cs”中的变量,TransactioHandler是类库,Enviar Faturas是Windows窗体。
Is it possible to do what I want? 可以做我想做的吗? If so how should I do it? 如果是这样,我该怎么办?

UPDATE: 更新:

I have this variable declares in TransactionHandler.cs 我在TransactionHandler.cs中声明了此变量

var numfatura = _transaction.TransDocument + _transaction.TransSerial + _transaction.TransDocNumber;

And I need to use it on the Windows Form "Enviar Faturas". 我需要在Windows窗体“ Enviar Faturas”上使用它。

UPDATE 2: 更新2:

I have this code to select from a datagridview and write a textfile: 我有以下代码可以从datagridview中选择并编写文本文件:

FileInfo arquivo = new FileInfo(@"C:\\Users\\HP8200\\Desktop\\faturas\\" + r.Index + ".txt");
And I want to change the "r.index" for the variable I showed on the first update 我想更改我在第一次更新中显示的变量的“ r.index”

I would suggest to use a property instead of a public field : 我建议使用属性而不是公共字段

public class TransactionHandler
{
   private static string numfatura = _transaction.TransDocument + _transaction.TransSerial + _transaction.TransDocNumber;

   public static string Numfatura
   { 
        get { return numfatura ; }
        set { numfatura = value; }
   }
}

From another class, you call your variable like this: 在另一个类中,您可以这样调用变量:

public class EnviarFaturas
{
    public void DoSomething()
    {
         string r.Index= TransactionHandler.Numfatura;
    }
}

Ok, from what I understand and having no idea of the execution flow you probably need something like this in the TransactionHandler (a property) 好的,据我所知并且不了解执行流程,您可能需要在TransactionHandler(一个属性)中使用类似的内容

public int Numfatura
        {
            get
            {
                return this._transaction.TransDocument + this._transaction.TransSerial + this._transaction.TransDocNumber;
            }
        }

you can change the type to the one that stands behing the "var" in your code example. 您可以在代码示例中将类型更改为代表“ var”的类型。

To access it in the form you need an instance of the class (again I dont know what your logic is) but once you get it eg 要以表格的形式访问它,您需要一个类的实例(同样,我不知道您的逻辑是什么),但是一旦获得它,例如

var transactionHandler = new TransactionHandler();

you can simply try 你可以简单地尝试

r.Index = transactionHandler.Numfactura;

Keep in mind that you can hit the default data value (for int is 0) if your methods depend upon other event to happen. 请记住,如果您的方法依赖于其他事件发生,则可以达到默认数据值(对于int为0)。

I strongly suggest you to learn more about C# and Object Oriented Programming as Alexey Zimarev stated in the comments. 我强烈建议您如评论中的Alexey Zimarev所述,了解有关C#和面向对象编程的更多信息。 Also you should consider how to get/inject a concrete instance in the view. 您还应该考虑如何在视图中获取/注入具体实例。 Another good and related read will be singleton pattern, mvp and dependency injection. 另一个很好且相关的读物是单例模式,mvp和依赖项注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM