|
-
Jun 17th, 2008, 06:45 AM
#1
Thread Starter
Lively Member
[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
-
Jun 17th, 2008, 07:45 AM
#2
Thread Starter
Lively Member
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
-
Jun 17th, 2008, 07:46 AM
#3
Re: [2008]Prob in calculation...simple math!
do you just want the integer (whole number) part of the calculation?
vb Code:
math.floor(Val(char without spaces) / 65)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 17th, 2008, 07:47 AM
#4
Re: [2008]Prob in calculation...simple math!
 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?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 17th, 2008, 08:22 AM
#5
Re: [2008]Prob in calculation...simple math!
-
Jun 17th, 2008, 11:50 AM
#6
Frenzied Member
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 
-
Jun 17th, 2008, 12:11 PM
#7
Hyperactive Member
Re: [2008]Prob in calculation...simple math!
also you dont need it as it is already an integer.
-
Jun 18th, 2008, 01:34 AM
#8
Thread Starter
Lively Member
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
-
Jun 18th, 2008, 01:54 AM
#9
Thread Starter
Lively Member
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
-
Jun 18th, 2008, 02:22 AM
#10
Re: [2008]Prob in calculation...simple math!
You should use a Regex to get word counts.
-
Jun 18th, 2008, 02:52 AM
#11
Thread Starter
Lively Member
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
-
Jun 18th, 2008, 03:01 AM
#12
Thread Starter
Lively Member
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
-
Jun 18th, 2008, 03:24 AM
#13
Frenzied Member
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 
-
Jun 18th, 2008, 03:33 AM
#14
Re: [2008]Prob in calculation...simple math!
 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.
-
Jun 18th, 2008, 03:34 AM
#15
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.
-
Jun 18th, 2008, 03:43 AM
#16
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.
-
Jun 18th, 2008, 03:56 AM
#17
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.
-
Jun 19th, 2008, 03:17 AM
#18
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|