0

I created my class

class TxtEmail
{
    public TxtEmail(string firtFirstmail, string domain)
    {
        this.Firstmail = firtFirstmail;
        this.Domain = domain;

    }

    public string Firstmail { get; set; }
    public string Domain { get; set; }


    public string RetOneString()
    {
        return Firstmail + "@" + Domain;
    }
}

Then my class add to List Class

     class EmailDP : List<TxtEmail>
{
    List<TxtEmail> txtemail = new List<TxtEmail>();
    public void Add(string path)
    {

        txtemail.Add(new TxtEmail("user1", "google.ru"));
        txtemail.Add(new TxtEmail("user5555", "google.com"));
        txtemail.Add(new TxtEmail("user252", "outlook.com"));
        txtemail.Add(new TxtEmail("user3", "gmail.com"));
    }
   another methods ......

But then i created object my classes, he show 0 Count, Why? Where i make mistake and how i can get object in it?

EmailDP em1 = new EmailDP();
MessageBox.Show(em1.Count.ToString()); -> this show 0
foreach (var myob in em1)
{
    MessageBox.Show(myob.RetOneString());
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Alex
  • 53
  • 12
  • 9
    You don't appear to call .Add() so nothing is added to the list so its length is 0. – Alex K. Oct 22 '18 at 15:20
  • 1
    You are adding to a private collection, not to the actual type. – Camilo Terevinto Oct 22 '18 at 15:21
  • 1
    You may want to read https://stackoverflow.com/questions/21692193/why-not-inherit-from-listt – Camilo Terevinto Oct 22 '18 at 15:21
  • There is *almost* never a need to extend (inherit from) the type `List`. Move the code you use in `Add` to the last block of code you have shown there and get rid of your type, use `List` directly. – Igor Oct 22 '18 at 15:21
  • 2
    Note: this is a perfect moment to learn how to use a debugger. Put a breakpoint in the part of code you assume should be run. See if it is. Add a breakpoint to where you think it should be called. Step into methods, run line by line. You’ll get a sense of what’s happening and can solve problems much faster by yourself. – Sami Kuhmonen Oct 22 '18 at 15:22
  • List txtemail = new List(); change this to List txtemail = new List(); – Prakash Oct 22 '18 at 15:33

3 Answers3

0

Your EmailDP is-a List and also has-a List and I don't think you mean for both of those to be true. It's a question of inheritance vs composition. Do you need your class to be a list or can it contain a list instead?

If you want your class to be-a List, then in your add method, you can do:

this.Add(new TxtEmail("user1", "google.ru")); ...

If you want your class to contain-a List then you can remove the inheritance of List

class EmailDP 
{
    ...

And then make your list public so it is accessible:

public List<TxtEmail> txtemail = new List<TxtEmail>();

And then get the count from the list contained within:

MessageBox.Show(em1.textemail.Count.ToString()); 

Hope that helps.

CPerson
  • 1,222
  • 1
  • 6
  • 16
0

You have two lists involved:

  1. The class EmailDP itself derives from List<TxtEmail> and is therefore a list whose count you are displaying
  2. The internal list txtemail to which you actually add the items, leaving EmailDP itself empty.

Change your class, so that it encapsulates the inner list.

class EmailDP
{
    private readonly List<TxtEmail> _txtemail = new List<TxtEmail>();

    public void Add(string path)
    {
        _txtemail.Add(new TxtEmail("user1", "google.ru"));
        _txtemail.Add(new TxtEmail("user5555", "google.com"));
        _txtemail.Add(new TxtEmail("user252", "outlook.com"));
        _txtemail.Add(new TxtEmail("user3", "gmail.com"));
    }

    public int Count => _txtemail.Count;

    public IEnumerable<TxtEmail> EmailTexts => _txtemail;

    // ... other methods
}

And of course you have to call Add at least once...

EmailDP em1 = new EmailDP();
em1.Add("some path");
em1.Add("another path");
MessageBox.Show(em1.Count.ToString());
foreach (var myob in em1.EmailTexts)
{
    MessageBox.Show(myob.RetOneString());
}

If you override ToString instead of creating a method RetOneString, you have the advantage that the TxtEmail objects will be displayed automatically at several places. I.e. in the debugger, in listboxes or in Console.WriteLine.

public override string ToString()
{
    return Firstmail + "@" + Domain;
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Thank you. I have question. If i use private readonly List _txtemail = new List(); Then i created method `code` public string ReturnString() { string tmpstr = string.Empty; foreach (var tx in EmailTexts) { tmpstr = tx.RetOneString(); } return tmpstr; } `code` I use richTextBox1.AppendText(em1.RetOneString()); But foreach return only last object. – Alex Oct 23 '18 at 14:48
  • Because you are not concatenating (putting together) the strings. You are only assigning strings. So the last assignment replaces the previous ones. Replace `tmpstr = tx.RetOneString()` by `tmpstr += tx.RetOneString()`. Example: for `int sum = 0; sum = 4; sum = 5;` sum will be `5`. For `int sum = 0; sum += 4; sum += 5;` sum will be `9`. – Olivier Jacot-Descombes Oct 23 '18 at 14:58
  • `x += y` is the same as `x = x + y`. – Olivier Jacot-Descombes Oct 23 '18 at 15:04
-1

You need to call the Add Method which fills your array. Also do not inherit the List as you do not need this.

 EmailDP em1 = new EmailDP();
em1.Add(string.Empty);
    MessageBox.Show(em1.Count.ToString()); -> You will get items you added in the Add Method
    foreach (var myob in em1)
    {
        MessageBox.Show(myob.RetOneString());
    }
Prakash
  • 357
  • 2
  • 14
  • 2
    Probably because it is wrong. `em1.Count` will still be zero as items are added to a private collection and NOT the object collection. – Franck Oct 22 '18 at 15:27
  • Oops... yes if he makes it public, the he would be able to access it – Prakash Oct 22 '18 at 15:32