1

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?

Levi
  • 661
  • 7
  • 26
  • 2
    `string` will not have the properties your trying to instansiate, surely this should be an object that you have created with the `weekYear` property inside? – LukeHennerley Dec 03 '12 at 09:09
  • 1
    Or, if you actually want a string array, you leave the "fieldnames" out. As in `campaigns.Add(new String[] { something[i], info[i], ... })` and later access them by index. – Wutz Dec 03 '12 at 09:11
  • I'd create an object called `Campaign` and set each property on it to the type it needs to be. – Adrian Thompson Phillips Dec 03 '12 at 09:14
  • 1
    Also, there's no need, in this case, to first create a `List<>`, and then eventually call `.ToArray()`, because you know from the beginning that there's going to be exactly `C` elements in you list. – Jeppe Stig Nielsen Dec 03 '12 at 09:22
  • I prefeer to use List because I'm quite new to C# - Using only indexes workes great, thanks @Wutz – Levi Dec 03 '12 at 09:37

7 Answers7

2

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], 
    });
Adil
  • 146,340
  • 25
  • 209
  • 204
  • Yes, I know this because I took this code from somwhere and the codes used a custom data type. However I have no idea how to make this myself, but it doesn't matter now that I made it work. – Levi Dec 03 '12 at 09:40
1
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]});
        }
sreejithsdev
  • 1,202
  • 12
  • 26
0

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.

LukeHennerley
  • 6,344
  • 1
  • 32
  • 50
0

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

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
0

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], 
    });
}
Captain Kenpachi
  • 6,960
  • 7
  • 47
  • 68
0

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();
Tartori
  • 287
  • 3
  • 14
0

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();
i100
  • 4,529
  • 1
  • 22
  • 20