Is an Enum or a 2d Array the solution im looking for?

G

Gary

I have a status text on my status bar which simply lists the logged in
user name.

the code that puts text into this is:

stUserName.Text = "Currently logged in user: " +
System.Environment.UserName;

However our login names here are firstname, surname initial, like so: -


freds

I'd like to write the status strip as:

Currently logged in user: Firstname Surname (logged in user)

e.g.

Currently logged in user: Fred Smith (freds)

So i need to way of linking a list which I can populate myself of names
with user names.

I looked at enums but it would seem they only allow you to assign names
to numbers.
Whereas i need to assign full names to user names.

I could use lots of 'if's' but id rather have one collection that
simply has Fullname, and associated username if possible.

Whats the best way to do this?

Thanks,

Gary.
 
D

Daniel

Hi

Yes you dont want an enum for this.

Don't you have a database that they are logging into and it checks their
details when they log in surely? And that contains the full name, surname
etc and you can retrieve the details from there?

If you dont have that, a little worrying, then i suppose a struct like:

struct Names
{
string loginname;
string FirstName;
string LastName;
}

and then a:

List<Names> NameList = new List();

and do tis to populate for each

public void AddName(string loginName, string firstName, string LastName)
{
Names n;
n.LoginName = loginName;
n.FirstName = firstName;
n.LastName = lastName;

NameList.Add(n);
}

And then once you have that list you can check the user against that by
iterating through and comparing the loginName part.
 
B

Bobbo

Daniel said:
Don't you have a database that they are logging into and it checks their
details when they log in surely? And that contains the full name, surname
etc and you can retrieve the details from there?

Gary,

Daniel's comments regarding your login procedure are seriously worth
noting, although if you must proceed with maintaining a list of users,
you could probably [unless anyone else knows of any limitations] extend
the List<> suggestion to use Dictionary<>. This allows the object to
be stored against a key, in your case the 'freds' string.

This assumes .net 2.0 by the way.

e.g. You might have a simple class to store the user details,
pre-empting the requirement to store extra information in future.

class Employee
{
string _forename;
string _surname;

public Employee(string forename, string surname)
{
Forename = forename;
Surname = surname;
}

public string Forename
{
get { return _forename; }
private set { _forename = value; }
}

public string Surname
{
get { return _surname; }
private set { _surname = value; }
}
}


Then at the point where you obtain your list of employees, you'd add
them to the dictionary list thus:

Dictionary<string, Employee> employeeList = new
Dictionary<string, Employee>();

// Loop through list of employees from database here
employeeList.Add("freds", new Employee("Fred", "Smith"));


And finally when you need to retrieve the employee:

string userId = "freds";
Employee thisUser = employeeList[userId];
string loggedInUser = string.Format("{0} {1} ({2})",
thisUser.Forename, thisUser.Surname, userId);
 
D

Daniel

....and in case your debating the 2 methods and no database option, Bobbo's
is more of a complete solution. Go for that.


Bobbo said:
Don't you have a database that they are logging into and it checks their
details when they log in surely? And that contains the full name, surname
etc and you can retrieve the details from there?

Gary,

Daniel's comments regarding your login procedure are seriously worth
noting, although if you must proceed with maintaining a list of users,
you could probably [unless anyone else knows of any limitations] extend
the List<> suggestion to use Dictionary<>. This allows the object to
be stored against a key, in your case the 'freds' string.

This assumes .net 2.0 by the way.

e.g. You might have a simple class to store the user details,
pre-empting the requirement to store extra information in future.

class Employee
{
string _forename;
string _surname;

public Employee(string forename, string surname)
{
Forename = forename;
Surname = surname;
}

public string Forename
{
get { return _forename; }
private set { _forename = value; }
}

public string Surname
{
get { return _surname; }
private set { _surname = value; }
}
}


Then at the point where you obtain your list of employees, you'd add
them to the dictionary list thus:

Dictionary<string, Employee> employeeList = new
Dictionary<string, Employee>();

// Loop through list of employees from database here
employeeList.Add("freds", new Employee("Fred", "Smith"));


And finally when you need to retrieve the employee:

string userId = "freds";
Employee thisUser = employeeList[userId];
string loggedInUser = string.Format("{0} {1} ({2})",
thisUser.Forename, thisUser.Surname, userId);
 
G

Gary

Hi thanks,

no database solution, this is a simple app and there are less than 20
users here.

thanks for your suggestions, i'll try to implement them!

Gary
...and in case your debating the 2 methods and no database option, Bobbo's
is more of a complete solution. Go for that.


Bobbo said:
Don't you have a database that they are logging into and it checks their
details when they log in surely? And that contains the full name, surname
etc and you can retrieve the details from there?

Gary,

Daniel's comments regarding your login procedure are seriously worth
noting, although if you must proceed with maintaining a list of users,
you could probably [unless anyone else knows of any limitations] extend
the List<> suggestion to use Dictionary<>. This allows the object to
be stored against a key, in your case the 'freds' string.

This assumes .net 2.0 by the way.

e.g. You might have a simple class to store the user details,
pre-empting the requirement to store extra information in future.

class Employee
{
string _forename;
string _surname;

public Employee(string forename, string surname)
{
Forename = forename;
Surname = surname;
}

public string Forename
{
get { return _forename; }
private set { _forename = value; }
}

public string Surname
{
get { return _surname; }
private set { _surname = value; }
}
}


Then at the point where you obtain your list of employees, you'd add
them to the dictionary list thus:

Dictionary<string, Employee> employeeList = new
Dictionary<string, Employee>();

// Loop through list of employees from database here
employeeList.Add("freds", new Employee("Fred", "Smith"));


And finally when you need to retrieve the employee:

string userId = "freds";
Employee thisUser = employeeList[userId];
string loggedInUser = string.Format("{0} {1} ({2})",
thisUser.Forename, thisUser.Surname, userId);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top