build an array in C#

175 Views Asked by At

I cannot get this code to work for me:

List<String[]> campaigns = new List<String[]>();
for(int i = 0; i < C; i++){
    campaigns.Add(new String[]{
        weekYear = something[i], 
        campaign = info[i], 
        totalBids = Total1[i], 
        highestBid = Highest[i], 
    });
}
list = campaigns.ToArray();

I am pretty sure I need to replace String[] with something else, but what?

7

There are 7 best solutions below

0
On BEST ANSWER
string[] a1 = new string[] { "a1", "b1", "c1", "d1" };
string[] a2 = new string[] { "a2", "b2", "c2", "d2" };
string[] a3 = new string[] { "a3", "b3", "c3", "d3" };
List<String[]> campaigns = new List<String[]>();
for (int i = 0; i <4; i++)
        {
            campaigns.Add(new String[]{
        a1[i],a2[i],a3[i]});
        }
1
On

You need to use some custom data type (class) which has attributes, weekYear, campaign, totalBids, highestBid, with required data types.

   List<YourDataType> campaigns = new List<YourDataType>();

   campaigns.Add(new YourDataType{
        weekYear = something[i], 
        campaign = info[i], 
        totalBids = Total1[i], 
        highestBid = Highest[i], 
    });
0
On

To do an array here, what you need to think of is what your initial List is going to be OF. The type parameter in the List where you put a type inbetween <> is what your list is going to be of. Sometimes when your first starting with this sort of thing, you should think in literal english terms inside your head.

So if you wanted to put Campaign in your list, I would be saying okay I want:

A List Of Campain(s)

Which in code would be List<Campaign>. You are adding the list the right way but what your adding isn't the correct type, so hopefully my answer will explain this to you. It is invevitably going to be a list of object, so you could just have List<Object> and then add whatever you like to this list.

0
On

Write a class that has the members you want, and make an array (append []) of that type.

Or use an anonymous type:

var list = Enumerable.Range(0, C).Select(i => new {
    weekYear = something[i], 
    campaign = info[i], 
    totalBids = Total1[i], 
    highestBid = Highest[i], 
    }).ToArray();

You might not need to call ToArray() if all you want is an IEnumerable<>.

0
On

The best thing to do is to create a campaign class with the properties you are trying to save and create a List

List<Campaign> campaigns = new List<Campaign>();
for(int i = 0; i < C; i++){
campaigns.Add(new Campaign{
    weekYear = something[i], 
    campaign = info[i], 
    totalBids = Total1[i], 
    highestBid = Highest[i], 
    });
}
0
On

You have to create a class with the properties in it. as follows:

class MyStrings
{
    public string WeekYear { get; set; }
    public string Campaign { get; set; }
    public string TotalBids { get; set; }
    public string HighestBid { get; set; }

}

Then you can use it like this:

        var campaigns = new List<MyStrings>();
        for (int i = 0; i < C; i++)
        {
            campaigns.Add(new MyStrings
            {
                WeekYear = something[i], 
                Campaign = info[i], 
                TotalBids = Total1[i], 
                HighestBid = Highest[i], 
            });
        }

I don't know why you want this, cause I have not enough context

        list = campaigns.ToArray();
0
On

It doesn't work because this is not a String array you try to add

Instead try this:

  List<dynamic> campaigns = new List<dynamic>();
  for (int i = 0; i < C; i++)
  {
    campaigns.Add(new {
      weekYear = "a", 
      campaign = "b", 
      totalBids = "c", 
      highestBid = "d", 
    });
  }
  dynamic[]  list = campaigns.ToArray();