0

I have a issue that i cant solve.

Here is the code

 sik input = new sik();
        for (int i = 0; i < 5; i ++)
        {
            input.skId = securitiesArray[i].skId;
            input.country = securitiesArray[i].country;

        }
   sik[] inputs = new sik[]
            {
                input
            };

Now i know this will only put 1 value in the sik[] list.

How can i put all the 5 values in this list.

Thanks

Note: i cannot initialize ski[] first. This has to be done in that order.

2
  • 1
    What is the type of securitiesArray? Is it sik[]? Commented Mar 12, 2013 at 22:15
  • 1
    I changed your title. sik[] is an array. An ArrayList is an untyped dynamic list from .NET 1.1. Commented Mar 12, 2013 at 22:17

6 Answers 6

5

Any reason that it has to be an array?

List<sik> input = new List<sik>();

for (int i = 0; i < 5; i ++)
{
    var newInput = new sik();        
    newInput.skId = securitiesArray[i].skId;
    newInput.country = securitiesArray[i].country;
    input.Add(newInput);
}

The reason that the List is useful is that it can dynamically grow with you, so you have no need to worry about how many instances you may need to add.

MSDN Documentation for List and all of it's glorious methods http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

Sign up to request clarification or add additional context in comments.

Comments

1
    sik[] inputs = new sik[5];
    for (int i = 0; i < 5; i ++)
    {
        sik input = new sik();
        input.skId = securitiesArray[i].skId;
        input.country = securitiesArray[i].country;
        inputs[i] = input;
    }

Comments

1

You can use Linq to do this.

sik[] inputs = securitiesArray.Select(item => 
    new sik() 
    { 
        skId = item.skId, 
        country = item.country 
    }).ToArray();

Comments

1

You can't have variable size array, instead you can use List.

    List<sik> siks = new List<sik>();
    sik input = new sik();
    for (int i = 0; i < 5; i ++)
    {
        input.skId = securitiesArray[i].skId;
        input.country = securitiesArray[i].country;

        siks.Add(input);
    }

If you want array yet, use sik[] inputs = skis.ToArray();

Comments

1

You can also do this,

List<sik> input=new List<sik>();
for(int i=0;i<securitiesArray.Length;i++)
{
   input.Add(new{skId=securitiesArray[i].skid,country=securitiesArray[i].country});
}

Comments

1

For what it's worth, here the Linq approach:

sik[] inputs = Enumerable.Range(0, 5)
    .Select(i => new sik{ kId = securitiesArray[i].skId, country = securitiesArray[i].country})
    .ToArray();

If securitiesArray is of type sik(the properties suggest), you can select directly from it:

sik[] inputs = securitiesArray.Take(5).ToArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.