|
-
Jun 18th, 2008, 03:24 AM
#1
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:34 AM
#2
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
#3
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
#4
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
#5
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
|