Reputation: 57
I'm trying to create an ATM simulator in C# and I kind of stuck with the log in. I'm using a class called User which I defined as follows:
public class User
{
public string userName { get; set; }
public string password { get; set; }
public int savingsAcct { get; set; }
public int checkAcct { get; set; }
}
I've created three instances of my class to represent the three accounts I will be using. So I was wondering how can I make a two dimensional array that will accept both strings and ints.
I think I can use a two dimensional array for the log because I can use a for loop to traverse the array and check the username and password of each account to see if they match. I know I could use a data base or something else but I'm still relatively new to C# so I'm not looking for something efficient, I'm just looking for something that works. Any help would be much appreciated.
Upvotes: 0
Views: 7142
Reputation: 13676
You should really learn some basics of working with databases. Your User
class is something that is called Model and what you're looking for is collection.
First of all i'd recommend to add id
field to make it easier to identify each object (though it's optional):
public class User
{
public int id { get; set; }
public string userName { get; set; }
public string password { get; set; }
public int savingsAcct { get; set; }
public int checkAcct { get; set; }
}
And you can use List<User>
:
List<User> users = new List<User>()
{
new User() { id = 0, userName = "Alex", etc... }
new User() { id = 1, userName = "Joshua", etc... }
new User() { id = 2, userName = "Phil", etc... }
};
To find user with specific userName you'd have to use LINQ here :
// returns null if no such user is found
User alex = users.FirstOrDefault(u => u.userName == "Alex");
Sometimes it's more suitable to use Dictionary<TKey, TValue>
than List<T>
:
Dictionary<string, User> users = new Dictionary<string, User>()
{
{ "Alex", new User() { id = 0, userName = "Alex", etc... } },
{ "Joshua", new User() { id = 1, userName = "Joshua", etc... } },
{ "Phil", new User() { id = 2, userName = "Phil", etc... } },
};
Then you can access users by their userName with help of indexer :
User alex = users["Alex"];
Upvotes: 2
Reputation: 1633
In C#, You cannot create a two dimensional array with two different data types, in your case, int and string. You can only create a two dimensional array of the same data type.
If you require a data structure to hold two data types, you can use a Dictionary pairs.
//e.g.
Dictionary<int, string> data = new Dictionary<int, string>
{
{1, "value 1"},
{2, "value 2"},
{3, "value 3"}
};
//With your POCO can create a Dictionary as below
Dictionary<int, User> data = new Dictionary<int, User>
{
{1, new User{ userName = "username1", password = "password1", savingsAcct = 1,checkAcct = 1 }},
{2, new User{ userName = "username2", password = "password2", savingsAcct = 1,checkAcct = 2 }},
{3, new User{ userName = "username3", password = "password3", savingsAcct = 1,checkAcct = 3}}
};
You can use a loop to initialize your dictionary
Upvotes: -1
Reputation: 3288
As clarified in the comments, you are looking for a collection that can store User
objects in your application.
If you need to be able to change the contents of the collection, a List<T>
is a good choice - it does not have a fixed size so it is easy to add and remove objects.
List<User> _allUsers = new List<User>();
_allUsers.Add(/* user object */); //populate the list with Users using .Add()
foreach (var user in _allUsers)
{
if(user.userName == someInputVariable && user.passWord == someOtherInputVariable)
{
...
}
}
MSDN documentation for List<T>
You could also use a 1-dimensional array of User
but I think a List<T>
makes more sense for what you are trying to do, and it is very easy to work with as someone who is just starting to learn the language.
Upvotes: 0
Reputation: 119056
As you are looking to store your User
objects, then keep them in that format rather than trying to squeeze them uncomfortably into some sort of string/int combo. So lets seay you have 3 user objects:
var bob = new User { userName = "Bob" };
var alice = new User { userName = "Alice" };
var marvin = new User { userName = "Marvin" };
Let's put them into a List<>
:
var users = new List<User> { bob, alice, marvin };
And now you can loop around it as you wish:
foreach(var user in users)
{
Console.WriteLine(user.userName);
}
And if you wanted to find a specific item, you can get it quickly:
var user = users.SingleOrDefault(u => u.userName = "Bob");
if (user != null)
{
//We found a user called Bob
}
Or even check specifically for username and password:
var userIsValid = users.Any(u => u.userName = "Bob" && u.password = "secret");
Upvotes: 0
Reputation: 2532
This shows a working example, but will need to be massaged into your code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class User
{
public string userName { get; set; }
public string password { get; set; }
public int savingsAcct { get; set; }
public int checkAcct { get; set; }
}
class Program
{
static void Main(string[] args)
{
// here you'll take the user and password from the form
string userNameEnteredIntoScreen = "???";
string passwordEnteredIntoScreen = "???";
// build list of valid users (amend to suit)
var users = new List<User>();
users.Add(new User
{
userName = "user1",
password = "abc",
savingsAcct = 1,
checkAcct = 2,
});
users.Add(new User
{
userName = "user2",
password = "xyz",
savingsAcct = 3,
checkAcct = 4,
});
// then to check if they're valid
var user = users.SingleOrDefault(u =>
u.userName == userNameEnteredIntoScreen &&
u.password == passwordEnteredIntoScreen
);
if (user != null)
{
// the user and password was valid, and 'user' variable
// now contains the details of the user
}
}
}
}
Upvotes: 1