0

I have some public const strings in c# console application as shown below:

//Account one
    public const string POP_USER1 = "[email protected]";
    public const string POP_PWD1 = "abc";

    //Account two
    public const string POP_USER2 = "[email protected]";
    public const string POP_PWD2 = "xyz";

    //Account three
    public const string POP_USER3 = "[email protected]";
    public const string POP_PWD3 = "pqr;

We are using c# MailMan to retrieve emails present in those accounts. I simply wrote a for loop 3 times:

for (int i = 1; i <= 3; i++)
 {
    eEmails obj = new eEmails (i);
 }

In the constructor of eEmails, I am writing the below logic:

public eEmails (int counter)
{
   MailMan obj = new MailMan()
   obj.PopUsername =  "POP_USER" + counter;
   obj.PopPassword = "POP_PWD" + counter;
}

The lines where I am assigning user name and passwords, I need to fetch the exact const variable (i.e., POP_USER1, POP_USER2, POP_USER3 etc.,)

However I am not able to get the variable dynamically. I can simply write 3 if blocks in eEmails (int counter), but I didnt like that way. can somebody advise a better way of handling this situation without using separate if blocks for each user??

6
  • 1
    Define the data as Array or List? List<POPUserData> for example? Commented Jan 27, 2014 at 10:28
  • Defining your constants as Arrays of String(String[] POPUSER = new String[] {"User1","User2","User3"}; and the same for passwords) would do the trick. I think it is the simplest solution. Note, however, that defining the passwords unencrypted in the source code is not safe. Commented Jan 27, 2014 at 10:31
  • Your current eEmails implementation has a bug. You are assigning the password to the username property. Commented Jan 27, 2014 at 10:32
  • @Tim, thanks for noticing, thats a type in SO. Commented Jan 27, 2014 at 10:33
  • @ThunderGr: don't do like that. Your data structure implies that all usernames belong together and all password belong together. That's not a good idea. Instead a username and a password belongs together and you want a list of that. See Tim's answer. Commented Jan 27, 2014 at 10:58

1 Answer 1

2

Use a class instead of strings, then your code becomes more redable and maintainable and it'll also be less error-prone. Here is an example using a List<PopServerAccount> as container:

public class PopServerAccount
{ 
    public string Username {get;set;}
    public string Password {get;set;}

    public override bool  Equals(object obj)
    {
        PopServerAccount p2 = obj as PopServerAccount;
        if (p2 == null) return false;
        return Username == p2.Username;
    }

    public override int GetHashCode()
    {
        return Username.GetHashCode();
    }

    public override string ToString()
    {
        return Username;
    }
}

now change the signature of your method:

public eEmails (PopServerAccount pop)
{
    MailMan obj = new MailMan()
    obj.PopUsername =  pop.Username;
    obj.PopPassword = pop.Password;
}

Sample data:

var myPopServers = new List<PopServerAccount> 
{
    new PopServerAccount{ Username = "[email protected]", Password = "abc"},new PopServerAccount{ Username = "[email protected]", Password = "xyz"}
};

Use a loop and call your method:

foreach (PopServerAccount pop in myPopServers)
{ 
    eEmails(pop);
}
Sign up to request clarification or add additional context in comments.

5 Comments

not sure whats wrong, in eEmails(pop){} , I am not getting the properties of pop. when I say pop and put dot (.) intelligence not showing anything?
@mmssaann: i cannot reproduce this issue. Maybe you need to show your code in your question.
Intelligence is not showing up only in that particular file, however the properties are accessible if I type them explicitly I am not getting any compile error. I think this is more of intelligence issue.
@mmssaann: are these properties the only which you are missing or are no properties visible? Is this a separate class library, have you recompiled all (also in debug mode)?
none of the properties are visible I am afraid. All files are in the same application under same name space everything compiles successfully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.