altaaf.hussein
altaaf.hussein

Reputation: 282

Collections in C# over VB.NET

Hi I am new to C# and have been learning it. Actually think I am getting use to it there are a few things which I am unsure about but will try to research them before asking. That said one thing I cannot find it collections. I come from a VB.NET back ground and previously I used collections of another class to store its properties instead of having to wade through a datatable. My previous code was something like the following

Public class UserDetails

 Public Property Username() As String
   Get
      Return sUsername
   End Get
   Set(ByVal value As String)
      sUsername = value
   End Set
 End Property

 Public Property Forename() As String
   Get
      Return sForename
   End Get
   Set(ByVal value As String)
      sForename = value
   End Set
 End Property

 Public Property Surname() As String
   Get
      Return sSurname
   End Get
   Set(ByVal value As String)
      sSurname = value
   End Set
 End Property

End Class

you get the idea, a class with properties for what ever reason (this is easy in c# and somthing I have managed to do).

public string Username { get { return sUsername; } set { sUsername = value; } }
public string Forename { get { return sForename; } set { sForename = value; } }
public string Surname { get { return sSurname; } set { sSurname = value; } }

Then I have another class and create an object to this class and store each object in a collection, again as below;

clUserDetails = New Collection
Dim objUserDetails as new UserDetails
objUserDetails.Username = "SomeonesUsername"
objUserDetails.Forename = "SomeonesForename"
objUserDetails.Surname = "SomeonesSurname"
clUserDetails.Add(objUserDetails)

and the class also have a function which returns the collection

Public Function Items() As Collection
   Return clUserDetails
End Function

Now I retrieve this either by looping through each object and assigning an object to get to the properties or do the following

objUsers.Items(1) 

Now in C# I have tried to use List<> as I cannot find the collection object. I am assuming this is VB thing so would like some help as how to accomplish this in C#.

I have tried the following

private List <UserDetails> clUserDetails;
UserDetails objUserDetails = new UserDetails();
objUserDetails.Username = "Username";
objUserDetails.Forename = "Forename";
objUserDetails.Surname = "Surname";
clUserDetails.Add(objUserDetails);

but I am stuck as to how to get the single object of of this list/collection.

Can somebody help

Upvotes: 5

Views: 182

Answers (3)

Unknown Coder
Unknown Coder

Reputation: 392

Working with collections, as well as with arrays in C# is like it is on VB.NET, with the exception that in C#, we use [ and ] characteres instead of ( and ) as indexer.

Example on how to use a List (or any other collection):

// Create a list and add some items to it.
var myList = new List<string>();
myList.Add("Item A");
myList.Add("Item B")

// Now, use the list.
string itemA = myList[0];
string itemB = myList[1];

Example on how to use a Dictionary:

// Create a dictionary and add some items to it.
var myDic = new Dictionary<string, string>();
myDic.Add("google.com", "Search engine");
myDic.Add("stackoverflow.com", "An awesome site");

// Now, use the dictionary (get its keys value).
string google = myDic["google.com"];
string stackOverflow = myDic["stackoverflow.com"];

Example on how to use arrays:

// Declare an array and add some items to it.
string[] myFirstArray = { "One", "Two", "Three" }
// Declare a second array similar to the first one.
string[3] mySecondArray;
mySecondArray[0] = "One";
mySecondArray[1] = "Two";
mySecondArray[2] = "Three";

// Now, use values of the arrays.
string one = myFirstArray[0];
string two = mySecondArray[1];
string three = myFirstArray[2];

Upvotes: 2

uTeisT
uTeisT

Reputation: 2266

Well, you are on the spot. List in System.Collections.Generic is what you need.

I simplified your class a bit:

class UserDetails
{
    public string Username { get; set; }
    public string Forename { get; set; }
    public string Surname { get; set; }
}

Declare a new List of UserDetails:

List<UserDetails> clUserDetails = new List<UserDetails>();

You can add more UserDetails to your list like this:

clUserDetails.Add(new UserDetails { Username = "UserName1", Forename = "Forename1", Surname = "Surname1" });
clUserDetails.Add(new UserDetails { Username = "UserName2", Forename = "Forename2", Surname = "Surname2" });
clUserDetails.Add(new UserDetails { Username = "UserName3", Forename = "Forename3", Surname = "Surname3" });

And then you can loop with a foreach

foreach(UserDetails userDet in clUserDetails)
{
    Console.WriteLine($"Current user details --> Username : {userDet.Username} Forename : {userDet.Forename} Surname : {userDet.Surname} ");
}

The output will be:

Current user details --> Username : UserName1 Forename : Forename1 Surname : Surname1 Current user details --> Username : UserName2 Forename : Forename2 Surname : Surname2 Current user details --> Username : UserName3 Forename : Forename3 Surname : Surname3

Upvotes: 3

Bryan Woodford
Bryan Woodford

Reputation: 126

Is this what you want to be able to do?

Your model:

public class UserDetails
{
    public string Username { get; set; }
    public string Forename { get; set; }
    public string Surname { get; set; }
}

And create/add a user to a List<>

var userDetails = new UserDetails
{
    Username = "Username",
    Forename = "Forename",
    Surname = "Surname"
}; // object initialiser

var listOfUsers = new List<UserDetails>();
listOfUsers.Add(userDetails);

And then retrieve using an index (provided it exists), like so:

listOfUsers[0].Forename

Upvotes: 5

Related Questions