0

So I made this class to get frequencies of a given string:Gender from a List ->

public static List<GenderFrequency> GenderFrequencies(List<Bird> birds)
{
    List<GenderFrequency> result = new List<GenderFrequency>();
    foreach (Bird b in birds)
        UpdateGenderList(b, result);
    return result;
}

private static void UpdateGenderList(Bird b, List<GenderFrequency> freqs)
{
    foreach (GenderFrequency freq in freqs)
        if (freq.Gender.Equals(b.Gender, StringComparison.CurrentCultureIgnoreCase))
        {
            freq.FrequencyPlus1();
            return;
        }
    GenderFrequency newfr = new GenderFrequency() { Gender = b.Gender, Frequency = 1 };
    freqs.Add(newfr);
    //Console.WriteLine(newfr);

}

private void FrequencyPlus1()
{
    Frequency = Frequency + 1;
}
public override string ToString()
{
    return Gender + " (" + Frequency + ")";
}

And I want to put those results in a combobox using this method:

 private void showGenderFreq()
        {
            cboGenderFreq.Items.Clear();
            foreach (GenderFrequency g in GenderFrequency.GenderFrequencies())
            {
                cboGenderFreq.Items.Add(g);
            }

        }

But it gives me an error because I don't know what list parameter to put into Genderfrequencies() to make it work, VS then sugested to add this code to my Genderfrequency class:

 internal static IEnumerable<GenderFrequency> GenderFrequencies()
    {
        throw new NotImplementedException();
    }

But I don't think it's much help, the problem is probably somewhere in showGenderFreq()

thank you in advance


Your GenderFrequencies method is defined as needing a parameter (List<Bird>), and you don't supply that. You need to supply the parameter:

foreach (GenderFrequency g in GenderFrequency.GenderFrequencies(birds))

where birds would be a (theoretically) defined List<Bird> object.

However, I would get the List returned from GenderFrequencies in a temporary variable and then iterate through that temporary variable, rather than calling GenderFrequencies each time through the loop. Something like this:

List<GenderFrequency> genderFrequencies = GenderFrequencies.GenderFrequencies(birds);

foreach (GenderFrequency g in genderFrequencies)
{

}

Also, I would make a couple of small changes to your program for readability.

First, change your static GenderFrequencies method to something like GetGenderFrequencies. It's more descriptive (in my opinion).

Secondly, the GenderFrequencyPlus1 method could be replaced with a simple Frequency++;.

So it could look something like this:

foreach (GenderFrequency freq in freqs)
{
    if (freq.Gender.Equals(b.Gender, StringComparison.CurrentCultureIgnoreCase))
    {
        freq.Frequency++;
        return;
    }
}
4

1 回答 1

0

您的GenderFrequencies方法被定义为需要一个参数 ( List<Bird>),而您不提供该参数。您需要提供参数:

foreach (GenderFrequency g in GenderFrequency.GenderFrequencies(birds))

wherebirds将是(理论上)定义的List<Bird>对象。

但是,我会从GenderFrequencies一个临时变量中返回 List,然后遍历该临时变量,而不是GenderFrequencies每次都通过循环调用。像这样的东西:

List<GenderFrequency> genderFrequencies = GenderFrequencies.GenderFrequencies(birds);

foreach (GenderFrequency g in genderFrequencies)
{

}

此外,为了便于阅读,我会对您的程序进行一些小改动。

首先,将您的静态GenderFrequencies方法更改为类似GetGenderFrequencies. 它更具描述性(在我看来)。

其次,该GenderFrequencyPlus1方法可以用简单的Frequency++;.

所以它可能看起来像这样:

foreach (GenderFrequency freq in freqs)
{
    if (freq.Gender.Equals(b.Gender, StringComparison.CurrentCultureIgnoreCase))
    {
        freq.Frequency++;
        return;
    }
}
于 2013-09-02T20:03:53.330 回答