SomethingToChatWith Posted May 12, 2009 Share Posted May 12, 2009 So i'm taking on C# as my second programming language and its been pretty fun so far. Having experience in VB has helped a lot, so much so I think everything I know about C# I've learned in the last week or so, but... C#'s not as easy to transfer to as I thought. I'm doing a fun little console application that creates two string collections that reference a single array (one for names and one for colors), assigns the string values, and than finally outputs each person's favorite color to the console. I did it first in VB and its working fine: Module Module1 Public Structure Lists 'Holds a list of strings Dim ItemsList As System.Collections.Generic.List(Of String) End Structure Public Sub Main() 'Create an reference to the Lists structure Dim NewLists(1) As Lists 'Create two new instances to the List structure and two new 'instances of System.Collections.Generic Lists for the ItemsList 'member of Lists. NewLists(0) = New Lists 'List of names NewLists(1) = New Lists 'List of colors NewLists(0).ItemsList = New System.Collections.Generic.List(Of String)NewLists(1).ItemsList = New System.Collections.Generic.List(Of String) 'Assign the names to the first list. Assign the favorite colors of 'each person to the second. NewLists(0).ItemsList.Add("Jim") NewLists(1).ItemsList.Add("Blue") NewLists(0).ItemsList.Add("Jack") NewLists(1).ItemsList.Add("Black") 'Output what color each person likes. Console.WriteLine(NewLists(0).ItemsList(0) & " likes " & NewLists(1).ItemsList(0)) Console.Write(NewLists(0).ItemsList(1) & " likes " & NewLists(1).ItemsList(1)) Console.Read() 'So I can see the output when debugging End Sub End Module So basically when I run the app, I get two lines: Jim likes Blue Jack likes Black I seem to be having trouble with C# array declarations of a custom type (in my case a structure). Anyone know what I'm doing wrong here? C# version: using System; using System.Collections.Generic; namespace ConsoleApplication2 { class Program { public struct Lists { //Holds a list of strings public List<String> ItemsList; } static void Main() { //Create two references to the Lists structure //Error: "; expected" Lists[2] NewLists; //Rest of program appears to have been written correctly?... //Create two new instances to the List structure and two new //instances of System.Collections.Generic Lists for the ItemsList //member of Lists. NewLists[0] = new Lists(); NewLists[1] = new Lists(); NewLists[0].ItemsList = new List<String>(); NewLists[1].ItemsList = new List<String>(); //Assign the names to the first list. Assign the favorite colors of //each person to the second. NewLists[0].ItemsList.Add("Jim"); NewLists[1].ItemsList.Add("Blue"); NewLists[0].ItemsList.Add("Jack"); NewLists[1].ItemsList.Add("Black"); //Output what color each person likes. Console.WriteLine(NewLists[0].ItemsList[0] + " likes " + NewLists[1].ItemsList[0]); Console.Write(NewLists[0].ItemsList[1] + " likes " + NewLists[1].ItemsList[1]); Console.Read(); //So I can see the output when debugging } } } Quote Link to comment Share on other sites More sharing options...
SomethingToChatWith Posted May 12, 2009 Author Share Posted May 12, 2009 Never mind, found the solution: Lists[] NewLists; NewLists = new Lists[2]; OR Lists[] NewLists = new Lists[2]; Declaration of the array without a size won't give me errors. Hope this helps someone else :) Quote Link to comment Share on other sites More sharing options...
shawty Posted May 25, 2009 Share Posted May 25, 2009 Heh... always the simplest of things ain't it :-) If your messing with arrays and such, you might want to try using the new generic list types in .NET 3.5 EG: List<String> myStrings = new List<String>(){ "Jim", "Blue", "Jack", "Black" } It's makes things much easier beacuse you can use them like a stack, also there great for custom data: public class myClass { string name = ""; string color = ""; } List<myClass> people = new List<myClass>(){ new myClass(){ name = "Jim", color = "Blue" }, new myClass(){ name = "Jack",color = "Black"} } Because it's a class, it can have methods too, so each element in the list can still access it's methods... EG: string color = people[1].getColor(); // Assuming you'd defined it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.