0

I have this project:

enter image description here

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.
Is it possible to do what I want? If so how should I do it?

UPDATE:

I have this variable declares in TransactionHandler.cs

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

And I need to use it on the Windows Form "Enviar Faturas".

UPDATE 2:

I have this code to select from a datagridview and write a textfile:

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

2 Answers2

1

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;
    }
}
0

Ok, from what I understand and having no idea of the execution flow you probably need something like this in the TransactionHandler (a property)

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.

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 e.g.

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.

I strongly suggest you to learn more about C# and Object Oriented Programming as Alexey Zimarev stated in the comments. 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.

marto
  • 420
  • 1
  • 4
  • 15