Checking an array of booleans in VB.NET - vb.net

I need to check an array of booleans and for each value act accordingly.
Current code is something like this, but i want to make it read easier
If heater_check(0) = true Then
get_temp(0)
End If
If heater_check(1) = true Then
get_temp(1)
End If
...
And so on. Is there a better solution?

I guess this is what you are looking for
For i As Integer = 0 To heater_check.length - 1
If heater_check(i) then
get_temp(i)
End If
Next

"For a case like this, could a For loop still work? If node_num = "1" Then Temperature(0) = C_D(Convert.ToChar(raw_result(byte_num + 22))) + C_D(Convert.ToChar(raw_result(b........ "chia kang ren
And the answer :
"As long as there is a 'pattern' on your variable changes, you can always use loop to simplify your If-Else statement"TheQuickBrownFox
..or use some logic to tie together two variables of different types having some sort of relation between, whenever required.
' ...
Dim TempIndex As Int32 = Integer.Parse(node_num) - 1
' Converts node_num to an Integer and substract 1.
Temperature(TempIndex) = _
C_D(Convert.ToChar(raw_result(byte_num + 22))) + _
C_D(Convert.ToChar(raw_result(byte_num + 21))) * 16 + _
C_D(Convert.ToChar(raw_result(byte_num + 20))) * 256
' ...
By the way, if you're asking A, only answers to A are relevant, or answers that covers A and anything directly related to A like A', -A, A² or |A|.
Here I'm talking about converting a String to Integer. That has nothing to do with :
"I need to check an array of booleans and for each value act accordingly."
=> Please mark TheQuickBrownFox's answer as the valid answer. (And avoid asking other questions than the original one - :) )

Related

Visual Basic passing the contents of one variable from one sub to another sub and round to d.p

I have created a sub that works out a quadratic equation after inputting 3 coefficients.
However, I have to round that answer to whatever the user wishes to between 1 decimal point to 5.
Module Module1
Public property MyAnswer As Object
Sub myQuadraticEquation()
... 'enter 3 coefficients
Dim d As Integer = b ^ 2 - 4 * a * c
Console.Write("Your roots are: ")
MyResult = (((-b + d) / (2 * a) & " , " & (-b - d) / (2 * a)))
Console.WriteLine(MyAnswer)
End Module
My question is, how can I round the answer from this mathematical feature to a certain amount of d.p based on users' liking?
Can I create another sub and then call it later using if statement inside the Quadratic equation?
It's giving me the following error
"System.FormatException: 'Input string was not in a correct format.'
I would suggest always declaring variables as the datatype you want instead of relying on the program to figure them out at runtime. So change Public property MyAnswer As Object to Public property MyAnswer As String. Because you know you expect MyAnswer to be a string at the end.
As far as the rounding is concerned, I believe you were attempting to do it too late. I would change
MyAnswer = (((-b + d) / (2 * a) & " , " & (-b - d) / (2 * a)))
to
Dim part1 As Decimal = Math.Round((-b + d) / (2 * a), DigitsToRound)
Dim part2 As Decimal = Math.Round((-b - d) / (2 * a), DigitsToRound)
'This line can be simplified, look up string interpolation if you are using VS 2017
MyAnswer = part1 & " , " & part2
Where DigitsToRound is indicated by the user somehow. A bit more code and it technically can be simplified down quite a bit but I was trying to be as explicit as possible.
**Edit:
Just realized there is a name discrepancy in your code. I'm assuming you meant MyAnswer instead of MyResult in the code snippet you posted. If I am mistaken then I apoligize.
** Edit #2:
How can I transfer the contents of these variables into another sub, so that when I call this other sub I'm able to round it to whatever d.p I want it to. e.g. 'would you like this into 3.d.p'... then it should output the answer in 3dp. I've tried to do it, but I tend to get 0 as an output
using a separate function for this seems like a bit of overkill but it could look something like this:
Public Function DoRounding(ByVal p_value as Decimal) as Decimal
Dim ret As Decimal = 0D
Dim DigitsToRound As Integer = 0
'This line prompts the user for input and attempts to parse the input into an integer field named DigitsToRound.
'If the parsing fails then whatever the user typed in was not an integer and we tell them we are unable to proceed. Default input is 1.
If Integer.TryParse(InputBox("How many digits would you like to round to?", "Decimal Places", 1), DigitsToRound) Then
ret = Math.Round(p_value, DigitsToRound)
Else
MessageBox.Show("Invalid Input Detected")
End If
Return ret
End Function
This would prompt the user every time the function was called which could be pretty annoying. It would be better to prompt them once and then pass the value that they enter around to whatever functions need it.

vb.net increment string with prefix

I'm sure this question has been asked before, but I can't find exactly what I'm after.
I have a string which has a string, then a dash, then a number, e.g. "TERM-01" which happens to be the name of an electrical terminal in a switchboard.
I want to increment it to "TERM-02", "TERM-03" etc.
I have come up with this:
TermNo = CStr(Mid(TermNo, 1, InStr(TermNo, "-")) & (CInt(Mid(TermNo, InStr(TermNo, "-") + 1, TermNo.Length - InStr(TermNo, "-")) + 1)))
Which seems to work fairly well, however I need to increment the number including the 0, so 08, 09, 10 instead of 8,9,10.
Any ideas?
You could use the standard Substring method to point to the part where the number starts, convert it to an integer and add your increment. The trick to return the number with the 0 prefix is done using the format specifier D2
Dim TermNo = "TERM-01"
for i = 1 To 15
Dim value = "TERM-" + (Convert.ToInt32( _
TermNo.SubString( _
TermNo.IndexOf("-"))) + i) _
.ToString("D2")
Console.WriteLine(value)
Next
This could also be written using a Regex expression in a more readable way
Dim TermNo = "TERM-01"
for i = 1 To 15
Console.WriteLine(Regex.Replace(TermNo, "\d+", i.ToString("D2")))
Next
If you always have a dash, simply split the string on the dash and deal with the pieces individually.
https://msdn.microsoft.com/en-us/library/system.string.split%28v=vs.110%29.aspx

VB sentence count

I am trying to write a program that will count the number of sentences in a string. I would like to better use the framework has much has possible but I really don't understand this msdn example. So could someone explain it if they understand or does someone know how to count the number of sentences in a sting accurately? I am open to all and any suggestions.
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.sentences.count.ASPX?cs-save-lang=1&cs-lang=vb#code-snippet-1
EDIT:
Ok, for anyone that has had a hard time with counting sentences (like me) here is what I have been able to come up with and I think this is has good as you can get it. So if anyone else has a idea on how to solve sentences counting let me know. But what I have and it works.
My idea is that a sentences is defined has ". or ! or ?" and followed by at least one space. So this what seems to work. (Sorry its not a fully working module because I copied it out of class I'm using).
'Period cehck
For i As Integer = 0 To _runFor
If (str(i) = _Dot And True = Char.IsWhiteSpace(str(i + 1))) Then
_sentence_count = _sentence_count + 1
End If
Next
'Question check
For i As Integer = 0 To _runFor
If (str(i) = _Question And True = Char.IsWhiteSpace(str(i + 1))) Then
_sentence_count = _sentence_count + 1
End If
Next
'Exclamation check
For i As Integer = 0 To _runFor
If (str(i) = _Exclamation And True = Char.IsWhiteSpace(str(i + 1))) Then
_sentence_count = _sentence_count + 1
End If
Next
It's not really possible to accurately count sentences programmatically. For instance, if you wrote a piece of code that just counted the number of occurrences of a period followed by a space followed by a capital letter, then it would incorrectly interpret "I gave my report to Dr. Johnson." as two sentences.

The do while loop structure

In the do while loop structure usually there's a part where you declare a variable equal to a number (in this case i) and then in a second part you make a increment (i+1). I've made this example in vba, but the structure could be repeated in several different programming languages like the for in php when you're getting data from a database. Now, what I would like to understand better is the relation between the previous mentioned declarations, that is i = some number and i = i + 1 . Wouldn't this generate a problem of interpretation since you're declaring a variable to something and then assigning a different value right after it? Is the second declaration of the variable value, i = i + 1, a new variable calling the previous one or both i's are the same? This is the general orientation I intend with this question. I think explaining the scoop of both variables would help understanding. Thanks!
Sub DoWhile()
Dim x, i, sum
x = 10
i = 1
sum = 0
Do While i < x
sum = sum + i
i = i + 1
Loop
MsgBox “Sum = ” & sum
End Sub
A variable is really just a location in memory. That location can have any value. By setting i=i+1, you're really saying "take the value at position i, add 1 to it, and store it at position i". No new variable is created. There's no problem with the computer interpreting this- what it cares about is the location of i, which isn't changing. It still knows where to find i, regardless of how many times you change the value there.
Since you have created the variable i as a global variable, any reference or modification to i in the sub will be on the same variable. That being said:
Dim i as int
i = 1
Do while i < 11
MsgBox("The value of i is: " & i)
i = i + 1
Loop
would display 10 messageboxes showing the value of i being between 1 and 10.
When the program encounters i = i + 1, the computer 'sees' this as take the value of i, add one to it, and store the result in the variable i.
Hope that helps.

Expression too complex error

I know this question gets asked quite a bit, but I haven't seen an answer that I can apply to my issue. It seems that this error can be caused by quite a few things.
First of all, here is the code:
SurfArea = 19.63495408
Volume = 12.2718463
DeSimpleFinal = 0.009336098
Counter = 13
pi = 4*atn(1)
tracker = 0
stepamount = (Range("A" & Counter + 1).Value) / 1000
If Range("XFD1048508").Value = 1 Then
For x = 0 To Range("A" & Counter + 1).Value Step Stepamount
tracker = tracker + 1
ActiveSheet.Range("XEY" & tracker).Value = ((2 * SurfArea) / Volume) * Sqr((DeSimpleFinal * x) / pi)
ActiveSheet.Range("XEX" & tracker).Value = x
Next
Else
End If
I've decided to leave (Range("A" & Counter + 1).Value) on, because I think it might be relevant to why the code is breaking down. That cell is A14 and has the value 11 inside of it.
The line that gets flagged when I debug is the first line of the For loop. The loop doesn't even go through one iteration.
Does anybody have an idea of what it could be? I changed all my data types to variant to see if that was the issue, but that did nothing. Thank you for your help!
EDIT: I should note that the value of that range SHOULD be one, so that it does go through the loop.
I don't know anywhere enough about VBA's internals to understand why, but I do know that simplifying the expression that sets the limit on a FOR loop will eliminate the Error 16 - Expression Too Complex problem. (The response to this SO post as well as discussion elsewhere on the web comes to pretty much the same conclusion.)
Just declare a new variable, say, StopAmount, assign to it the expression you used in the FOR condition, and then replace the expression in the FOR with the name of the new variable. You get something like:
StopAmount = Range("A" & Counter + 1).Value
......
For x = 0 To StopAmount Step Stepamount
......
That said, there are certainly some oddities here.
For example, your original FOR condition worked fine if the iterator variable x is declared as a Variant, either implicitly or explicitly. (I declared all the variables for my tests.)
However, if x is dimensioned as a Double, the error returned. This is despite the fact that TypeName(x) showed the Variant x as a Double after the Range(..).Value assignment is made.
For x = 0 To Range("A14").Value Step Stepamount also ran with no problem.
And For x = 0 To Cells(Counter + 1, 1).Value Step Stepamount worked, too.