IHateMdi
IHateMdi

Reputation: 1

How do i fill a list with objects that are passed to a class(2nd class) from another class(3rd class)?

public class E
{
    public static List<Tranzactie> tranzactie = new List<Tranzactie>();
}

public class Tranzactie
{
    public string name;
    public string contract;
}


static void main()
{
    where i have a method with a loop that parses a.txt file
         and passes extracted values to Tranzactie
}

In class E there is a list i want to populate with values extracted from parsing a txtfile in Main. The data collected in Main fills fields from class Tranzactie.

How do i put the values passed to "Tranzactie" into the list found in class E?

Upvotes: 0

Views: 31

Answers (1)

CoolBots
CoolBots

Reputation: 4889

Well, somewhere in your loop, you'll need to:

  • instantiate a new Tranzactie object
  • populate the name and contract fields
  • add this object to your List<Tranzactie>

So, here's the code to do just that part:

// assuming you have variables "name" and "contract" that hold necessary data
var tranzactie = new Tranzactie() 
{
   name = name, 
   contract = contract
};
E.tranzactie.Add(tranzactie);

As an aside, I would rename the List<Tranzactie> in class E to tranzactii, because it represents multiple transactions - a collection - so plural form of the word is typically preferred.

Upvotes: 1

Related Questions