Jump to content

Transferring .net experience in VB to a second .net language


Recommended Posts

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

}

}

}
Link to comment
Share on other sites

  • 2 weeks later...

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&lt;String&gt; myStrings = new List&lt;String&gt;(){
  "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&lt;myClass&gt; people = new List&lt;myClass&gt;(){
  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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...