Results 1 to 18 of 18

Thread: [2008]Prob in calculation...simple math!

  1. #1

    Thread Starter
    Lively Member esafwan's Avatar
    Join Date
    Apr 2008
    Location
    Bangalore-India
    Posts
    95

    Question [2008]Prob in calculation...simple math!

    I'm doing a project in which it calculates the characters with space and without space in RTF files.

    After that the

    Val(char without space is) / 65

    --that is val of char divided by 65

    But the problem is when i do the same calculation by getting the counts from msword and calculate it in windows calculator the result is different... i'm getting a small variation in result sometime in decimals maybe upto 1 value.
    ________________________________-
    I declared all the variables in decimal.
    __________________________________

    This is the code i used for getting the counts!

    Code:
    Dim words() As String = rtf1.Text.Split(" ")
            Dim charsInc As Integer = rtf1.Text.Length
            Dim charsExc As Integer = rtf1.Text.Replace(" ", "").Length
            lblword.Text = words.Length & " words"
            lblchar.Text = charsInc & " characters including spaces"
            lblnochar.Text = charsExc & " characters excluding spaces"
            lblline.Text = rtf1.Lines.Length.ToString() & " lines"
    ___________________________________________________

    Please help me out... is there anything else that is should know while working with division and decimals in VB?
    Men have become the tools of their tools. - Henry David Thoreau

  2. #2

    Thread Starter
    Lively Member esafwan's Avatar
    Join Date
    Apr 2008
    Location
    Bangalore-India
    Posts
    95

    Re: [2008]Prob in calculation...simple math!

    that is the char count from VB and Word differs by around 50-60 in both including space and excluding!
    Men have become the tools of their tools. - Henry David Thoreau

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2008]Prob in calculation...simple math!

    do you just want the integer (whole number) part of the calculation?

    vb Code:
    1. math.floor(Val(char without spaces) / 65)

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2008]Prob in calculation...simple math!

    Quote Originally Posted by esafwan
    that is the char count from VB and Word differs by around 50-60 in both including space and excluding!
    how are you getting the text in vb.net, and how in word?

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2008]Prob in calculation...simple math!

    What is 65? "A"? Why?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2008]Prob in calculation...simple math!

    I'm not sure about your problem, but I did notice something odd...You are using Val(). Why? It's legacy, carried over from VB6, yet you are still using it in VB9. Use CInt and TryParse instead.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  7. #7
    Hyperactive Member gnaver's Avatar
    Join Date
    Jul 2005
    Location
    Denmark/Sweden
    Posts
    289

    Re: [2008]Prob in calculation...simple math!

    also you dont need it as it is already an integer.

  8. #8

    Thread Starter
    Lively Member esafwan's Avatar
    Join Date
    Apr 2008
    Location
    Bangalore-India
    Posts
    95

    Question Re: [2008]Prob in calculation...simple math!

    but isn't val() and direct calling different?!!!


    say text.text = 5 and text2.text= 6

    I mean val(text.text)+val(text2.text) = 11
    and text.text + text2.text = 56

    this is what i understood...

    Is there any way?!!
    Men have become the tools of their tools. - Henry David Thoreau

  9. #9

    Thread Starter
    Lively Member esafwan's Avatar
    Join Date
    Apr 2008
    Location
    Bangalore-India
    Posts
    95

    Question Re: [2008]what i'm trying to do!

    See my project is to get word counts of RTFS file in Dir based 3 formulas.

    1. No of characters including space / 65
    2. No of characters excluding space/ 65
    3. No of words in the RTF / 11

    The result is calculated like this in a loop!
    ________________________________________
    In the firts formula:
    1st file counts / 65 + 2nd file counts / 65 + nth file counts / 65 = final result
    ________________________________________
    Men have become the tools of their tools. - Henry David Thoreau

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008]Prob in calculation...simple math!

    You should use a Regex to get word counts.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Lively Member esafwan's Avatar
    Join Date
    Apr 2008
    Location
    Bangalore-India
    Posts
    95

    Re: [2008]Prob in calculation...simple math!

    well whts that? any reference to it...

    like how i could implement it in this ?
    Men have become the tools of their tools. - Henry David Thoreau

  12. #12

    Thread Starter
    Lively Member esafwan's Avatar
    Join Date
    Apr 2008
    Location
    Bangalore-India
    Posts
    95

    Re: [2008]Prob in calculation...simple math!

    is there any way that i can get the word counts of ms word in vb?
    Men have become the tools of their tools. - Henry David Thoreau

  13. #13
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2008]Prob in calculation...simple math!

    Val is VB6, it's legacy, like, 100 year old legacy. CInt(TextBox1.Text) does the same thing, as does Integer.TryParse(TextBox1.Text), only it's newer, better and very likely faster. Regex stands for Regular Expressions, google it and you'll see. There are also plenty of tutorials on this forum about using Regex.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008]Prob in calculation...simple math!

    Quote Originally Posted by esafwan
    well whts that? any reference to it...
    I don't know, MSDN maybe? Did you put regex into the search box and see what comes back? Look first, ask questions later.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: [2008]Prob in calculation...simple math!

    Code:
    Imports System.Text
    Imports System.Text.RegularExpressions
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Load
            Const stringOfText As String = "The cat sat on the mat, hyphenated-test too."
            Dim regExParagraghInfo As New Regex(".")
            MessageBox.Show("Character count with spaces: " & regExParagraghInfo.Matches(stringOfText).Count.ToString)
        End Sub
    
    End Class
    Last edited by alex_read; Jun 18th, 2008 at 03:44 AM.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  16. #16
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: [2008]Prob in calculation...simple math!

    Code:
    Imports System.Text
    Imports System.Text.RegularExpressions
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Load
            Const stringOfText As String = "The cat sat on the mat, hyphenated-test too."
            MessageBox.Show("Characters count without spaces: " & _
                            Regex.Replace(stringOfText, " ", String.Empty).Count.ToString)
        End Sub
    
    End Class
    Those are the regex way. There is a discrepancy between the way regex and MSWord treat hyphenated words though - regex will treat "some-text" as 2 words whereas Word counts it as 1.

    Val when I've used it in the past performs some sort of rounding if you're using decimals and as everyone's stated above, there are far better current .Net framework methods you can use so there should be no need to ever use the older style Val() statement ever again.

    Instanciating Word and reading a count of characters is overkill. You'd possibly need to account for differing versions of Word, whether Word is installed or not, the memory hit and time delay of instanciating Word in order to just get this information. There are also many issues you can encounter when trying to kill the process also. This shouldn't be an option for just a textbox word count.

    You haven't really described why you're dividing by 65 there - are you trying to get the characters typed per minute and should this be a value of 60, or are you trying to accomplish something different with this part?
    Last edited by alex_read; Jun 18th, 2008 at 03:50 AM.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  17. #17
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: [2008]Prob in calculation...simple math!

    but isn't val() and direct calling different?!!!

    say text.text = 5 and text2.text= 6

    I mean val(text.text)+val(text2.text) = 11
    and text.text + text2.text = 56
    Yes they are in that instance. Perhaps someone can back me up here but sometimes the + symbol used like that adds the values and sometimes it concatenates them if numbers are encountered, I'm sure I've read of odd behaviour with using that on string numbers from these forums.

    For the top one, you can use any of the .Net framework calls shown below and there's probably others too:
    Code:
    directcast(text1.text, integer) + directcast(text2.text, integer) 
    ctype(text1.text, integer) + ctype(text2.text, integer)
    Convert.toint32(text1.text) + Convert.toint32(text2.text)
    integer.tryparse(text1.text) + integer.tryparse(text2.text)
    If ever you did want concatenation (the 56 result), use the & symbol instead of the + there also.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  18. #18

    Thread Starter
    Lively Member esafwan's Avatar
    Join Date
    Apr 2008
    Location
    Bangalore-India
    Posts
    95

    Smile thanx!

    that was very helpful...
    ___________________________

    I'm just a beginner in this....
    Men have become the tools of their tools. - Henry David Thoreau

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width