开发者

DataGrid Issue - Value always shows 0?

开发者 https://www.devze.com 2023-02-04 00:53 出处:网络
I have a couple of functions that I know work correctly. One just counts the number of words in a textbox using a regular expression, and the other also uses a regex to count the instances of a partic

I have a couple of functions that I know work correctly. One just counts the number of words in a textbox using a regular expression, and the other also uses a regex to count the instances of a particular word (string) in the same textbox. I've made sure that these both return the values expected.

However, I'm obviously screwing up the function to populate my datagrid somehow, because the keywordcount always returns as zero, as does the density.

Here's my code:

public List<KeywordDensity> LoadCollectionData()
{
   string thearticle = txtArticle.Text;
   string[] keywordsarray = txtKeywords.Text.Split('\n');

   bool isincluded = false;
   int keywordcount = 0;
   int thedensity = 0;

   List<开发者_StackOverflowKeywordDensity> lsikeywords = new List<KeywordDensity>();
   foreach (string s in keywordsarray)
   {
      if (s.Trim() != "")
      {
         keywordcount = KeywordCount(thearticle, s);
         thedensity = keywordcount / WordCount(thearticle);

         if (thearticle.Contains(s))
         {
            isincluded = true;
         }
         else
         {
            isincluded = false;
         }

         lsikeywords.Add(new KeywordDensity()
         {
            included = isincluded,    
            keyword = s,
            occurences = keywordcount.ToString(),
            density = thedensity.ToString()
         });
      }
   }
   return lsikeywords;
}

EDIT @ 8:30AM MST: Figured out part of the issue. Using "\n" as the split character wasn't working. Apparently it was simply "splitting" the entire thing into one massive chunk that included the carriage returns and submitting that. I changed it to "\r" and now at least the "Included" and "Occurences" part works fine.

Now the only problem I'm having is that the "density" isn't working. I realize it can't be declared as an int; but it still always returns 0 - even when I declare thedensity as a float or var.

Is there something wrong with the following?

thedensity = keywordcount / wordcount;

Also - does the .ToString() function truncate the decimal point plus everything past it? For instance, if the value of a variable it 0.43 it would just convert to 0? If that's not the case, what variable type should I be using? I would think that float would be appropriate.

Thanks again!

EDIT @ 8:45 MST: Too bad I can't accept multiple answers. Both the '\r' and the var types were issues. I hadn't realized that you can't calculate a float by dividing two integers. When I changed the source variables to floats as well as having the density var it as well it worked.

Double thanks!

-Sootah


You're calculating with integers. WordCount sounds to me as a big number, keywordcount not. Maybe that's why you have not the result you expect?

Another question, what if WordCount returns zero? This will throw an exception. Does this never happen (assume not, but does it?)? If the exception will be thrown in binding, the app will not hang but the result will be false.


I would change a few things here. First would be to reassign the keyword to make sure there are no spaces attached to it:

foreach (string s in keywordsarray)
{
   string key_word = s.Trim();
   if (key_word != "")
   {
      // Replace all uses of s with key_word
      if(thearticle.Contains(key_word))
      // ...

Second would be to directly assign isincluded:

isincluded = thearticle.Contains(key_word);

Then I would just make sure that the txtKeywords is actually a list seperated by newlines:

// Is this really strings separated by newlines?
string[] keywordsarray = txtKeywords.Text.Split('\n');


Yes, SwDevMan81 has a good point - make sure that keywordsarray[] actually has some words in it.

I'm pretty sure that if a user types in a Silverlight text box, the separator is '\r' (at least I know I've seen it behave that way).

Might be better to use a regular expression to do the splitting.


The key lies in where are you filling this data? The call to LoadCollectionData should be in the constructor of your code behind because you are using a List, otherwise use an ObservableCollection. Also, posting the Grid's XAML will also help.

0

精彩评论

暂无评论...
验证码 换一张
取 消