Jhonatan Birdy
Jhonatan Birdy

Reputation: 227

How do i get the numbers from the List?

I have this code:

if (LR.Count > 0)
{
    for (int i = 0; i < LR.Count; i++)
    {

    }
}

LR is a List type of class. In this List I have 15 indexes for example in index [0] I have:

[0] = {Lightnings_Extractor.Lightnings_Region}

Now in this index[0] I have two int's variables end 88 and start 96

In this for loop up here what I need to do is:

if (LR.Count > 0)
{
    for (int i = 0; i < LR.Count; i++)
    {
        _fts.Add(
    }
}

_fts is a List<int> what I want is to add from each index in the LR List the two numbers. So if I'm trying to do just:

_fts.Add(LR[I]);

I'm getting two errors:

Error 31 The best overloaded method match for 'System.Collections.Generic.List.Add(int)' has some invalid arguments

And

Error 32 Argument 1: cannot convert from 'Lightnings_Extractor.Lightnings_Region' to 'int'

How can I just get the two numbers from each index of the List LR and add this two numbers each time to the List _fts ?

Upvotes: 0

Views: 87

Answers (1)

Richard Schneider
Richard Schneider

Reputation: 35477

If you want to add start and then end to _fts, then try

for (int i = 0; i < LR.Count; i++)
{
  _fts.Add(LR[I].start);
  _fts.Add(LR[I].end);
}

If you want one entry in _fts, then you need another class to contain a start and end and redefine the _fts variable.

public class Range
{
    public int Start { get; set; }
    public int End { get; set; }
}

public List<Range> _fts = new List<Range>();

_fts.Add(new Range {Start = LR[I].start, End = LR[I].end} );

You should also use a foreach instead of if then for:

foreach (var lr in LR)
{
   _fts.Add(new Range {Start = lr.start, End = lr.end} );
}

Upvotes: 3

Related Questions