0

-- EDIT --

Worth to point out. While having a problem with different homework problem I see than I am able to use separate objects as an entry (which I probably should do). This would make working on my final problem much easier. This is a problem that might be usefull for me.

I am trying to display data to text box using foreach loop. The data should be displayed coresponding to the selection of combo box. For example if I want to display PC I should see only UserName and Password and if I add another entry for example WebSite I should see previous entry in it's format and new entry with fields URL, Username, and Password. I have tried IF-statement in my previous question but didn't seem to work properly.

            StringBuilder sb = new StringBuilder();
            foreach (AddEntry list in addedEntry)
            {
                sb.AppendLine();
                sb.AppendLine("URL: " + list.URL);
                sb.AppendLine("Software Name: " + list.SoftwareName);
                sb.AppendLine("Serial Code: " + list.SerialCode);
                sb.AppendLine("User Name: " + list.UserName);
                sb.AppendLine("Password: " + list.Password);
                sb.AppendLine();
            }

            mainWindow.ChangeTextBox = sb.ToString();

Regards.

Community
  • 1
  • 1
HelpNeeder
  • 6,383
  • 24
  • 91
  • 155

2 Answers2

1
   StringBuilder sb = new StringBuilder();
    foreach (AddEntry list in addedEntry)
    {
        sb.AppendLine();
        if (!string.IsNullOrEmpty(list.URL))
             sb.AppendLine("URL: " + list.URL);
        if (!string.IsNullOrEmpty(list.SoftwareName))
             sb.AppendLine("Software Name: " + list.SoftwareName);
        if (!string.IsNullOrEmpty(list.SerialCode))
             sb.AppendLine("Serial Code: " + list.SerialCode);
        if (!string.IsNullOrEmpty(list.UserName))
             sb.AppendLine("User Name: " + list.UserName);
        if (!string.IsNullOrEmpty(list.Password))
             sb.AppendLine("Password: " + list.Password);
        sb.AppendLine();
    }

    mainWindow.ChangeTextBox = sb.ToString();

Edit: I have used UnhandledException's version as it is much more legible than my solution was (and the conditional operator is generally frowned upon in most cases).

I also want to point out that your AddEntry class could be easier written using auto properties (assuming you're using .NET 3.0+).

See:

namespace Store_Passwords_and_Serial_Codes
{
    class AddEntry
    {
        // Auto properties make this class a lot easier to read.
        public string type { get; set; }
        public string url { get; set; }
        public string softwareName { get; set; }
        public string serialCode { get; set; }
        public string userName { get; set; }
        public string password { get; set; }

        // Non-default constructor.
        public AddEntry(string type, string url, string softwareName, string serialCode, string userName, string password)
        {
            this.type = type;
            this.url = url;
            this.softwareName = softwareName;
            this.serialCode = serialCode;
            this.userName = userName;
            this.password = password;
        }
    }
}

And lastly, as you have said, it's important that you don't save information for one entry type that would belong in another (for instance, you shouldn't save URL into a PC entry type, as it makes no sense to). This entire solution would probably be better off using stronger typed objects (i.e. WebPassword, PCPassword, SoftwareSerialCode, etc). These could all inherit from a base class (Entry or something to that effect) to make it easier to strongly type the list, as well.

For instance:

class Entry { }

class PCPassword : Entry
{
    string userName { get; set; }
    string password { get; set; }

    public PCPassword(string uName, string pass)
    {
        this.userName = uName;
        this.password = pass;
    }

    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine();
        sb.AppendLine("User Name: " + this.userName);
        sb.AppendLine("Password: " + this.password);
        sb.AppendLine();
        return sb.ToString();
    }
}

You would then refer to it in your code as such:

 private void btnAddEntry_Click(object sender, EventArgs e)
 {
     // Making sure that type is selected.
     if (cmbType.SelectedIndex != -1)
     {
         if (cmbType.SelectedIndex == 0)
         {
             if(textUserName.Text == String.Empty || textPassword.Text == String.Empty)
                    MessageBox.Show("Please fill all the fields!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
             else
             {
                 addedEntry.Add(new PCPassword(textUserName.Text, textPassword.Text));
                 MessageBox.Show("Entry was successfully added!", "Entry Added!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 ClearFields();
             }
         }

         // etc, etc

         // Print our items
         StringBuilder sb = new StringBuilder();
         foreach (Entry item in addedEntry)
         {
             sb.Append(item.ToString());
         }

         mainWindow.ChangeTextBox = sb.ToString();
     }
 }

Just thought I'd throw that out there ;)

Paul Zaczkowski
  • 2,848
  • 1
  • 25
  • 26
  • Clear, not clear, I see your message. Let me tweak it a little bit and I'll respond. – HelpNeeder Nov 02 '11 at 07:10
  • Ok. This would be perfect. But when added first entry it display **all fields**. The other thing "\n" must be Environment.NewLine instead. How could I fix the problem with the first entry? – HelpNeeder Nov 02 '11 at 07:13
  • OK, never mind. I have declared all variables for easier input. Thank you!! – HelpNeeder Nov 02 '11 at 07:21
1
StringBuilder sb = new StringBuilder();
foreach (AddEntry list in addedEntry)
{
    sb.AppendLine();
    if (!string.IsNullOrEmpty(list.URL))
    sb.AppendLine("URL: " + list.URL);
    if (!string.IsNullOrEmpty(list.SoftwareName))
    sb.AppendLine("Software Name: " + list.SoftwareName);
    if (!string.IsNullOrEmpty(list.SerialCode))
    sb.AppendLine("Serial Code: " + list.SerialCode);
    if (!string.IsNullOrEmpty(list.UserName))
    sb.AppendLine("User Name: " + list.UserName);
    if (!string.IsNullOrEmpty(list.Password))
    sb.AppendLine("Password: " + list.Password);
    sb.AppendLine();
}

mainWindow.ChangeTextBox = sb.ToString();

2nd Option

Add following method to AddEntry class

  public override string ToString() 
  {
    StringBuilder sb = new StringBuilder();
    sb.AppendLine();
    if (!string.IsNullOrEmpty(this.URL))
    sb.AppendLine("URL: " + list.URL);
    if (!string.IsNullOrEmpty(this.SoftwareName))
    sb.AppendLine("Software Name: " + this.SoftwareName);
    if (!string.IsNullOrEmpty(this.SerialCode))
    sb.AppendLine("Serial Code: " + this.SerialCode);
    if (!string.IsNullOrEmpty(this.UserName))
    sb.AppendLine("User Name: " + this.UserName);
    if (!string.IsNullOrEmpty(this.Password))
    sb.AppendLine("Password: " + this.Password);
    sb.AppendLine();
    return sb.ToString();
  }

then you can show all the added Entry as below

StringBuilder sb = new StringBuilder();
foreach (AddEntry entry in addedEntry)
{
    sb.Append(entry.ToString());
}

mainWindow.ChangeTextBox = sb.ToString();
Damith
  • 62,401
  • 13
  • 102
  • 153
  • Only if I knew this from the beginning. I feel bad for copying n paste, but you do use OOP, which is a worth of an answer points. – HelpNeeder Nov 02 '11 at 07:51
  • Just for close point. I will not learn something that I have never experienced. Thank you again. – HelpNeeder Nov 02 '11 at 07:59