I have some logic and a certain if condition is not being reached. I have tried several variations of if/ifelse/else to create the logic I want, but nothing correctly works. Let me show you in some code...
If(a is true) Then
print("A is true")
Else If(b is true) Then
print("B is true")
if(c is true) Then
print("B and C are true")
else 'c is Not true
print("B is true, C is Not true)
if(d is true) Then
print("B and D are true")
else 'd is Not true
print("B is true, D is Not true")
End If
End If
End If
What is happening is that my
"If(d is true)" and "else 'd is not true"
conditions are not checked. That part of the logic is being "stepped over"
Expected output when A,B,C, and D are true:
"A is true"
Expected output when B, C, and D are true:
"B is true"
"B and C are True"
"B, C, and D are True"
Expected output when B and C are true but D is not:
"B is true"
"B and C are True"
"B is true, D is not true"
Expected outcome when B and D is true:
"B is true"
"B is True, C is not true"
"B, and D are true"
What I am currently seeing:
B,C and D are true:
"B is true"
"B and C are true"
Which leaves out "B and D are true"
Hopefully these outcomes help you understand!
I am not sure what a,b,c or d are but here is what I think you are trying to do. You just need to change the string only if"d" is either true or false based on your "use case". If you want more granularity, then I would suggest a string builder as mentioned before. At any rate, here is the code i used to create your "case" To be clear "c" and "d" are never checked if "b" is false. I based this of what your "use case" stated.
Private Sub Test()
Dim a = True
Dim b = False
Dim c = True
Dim d = True
Dim printout As String = ""
If a Then
printout = "a is true"
Else
If b Then
If c Then
printout = "b and c are true"
else
printout="b is true and c is not true"
End If
If d and c Then
printout = "b and c and d are true"
elseif d=true and c=false then
printout = "b and d are true , c is not true"
elseif c=true and d=false then
printout = "b and c is true ,d is not true"
else
printout = "b is true ,c and d are not true"
End If
End If
End If
Console.WriteLine(printout)
End Sub
Related
I keep getting the " Else without if" error in VBA when I clearly do not have that issue. Does anyone know how to fix it? It takes me to the elseif statement that begins with elseif memb= "platinum"
Below is my code:
ElseIf memb = "Platinum" Then d = 0.01
ElseIf memb = "Black" Then d = 0.03
End If
If st >= min Then cb = st * d Else cb = 0
End If
If cb >= thresh Then MsgBox ("cb is greater than thresh")
End If
tac = st + cb
Range("B5").Value = st
Range("B7").Value = cb
Range("B9").Value = tac
I'm going to assume your first If statement goes something like this:
If memb = "Gold" Then d = 0.005
ElseIf memb = "Platinum" Then d = 0.01
ElseIf memb = "Black" Then d = 0.03
End If
If some processing is performed on the same line as the Then keyword, VBA treats it as a single, non-nested If statement. This means that anything after that will be treated as a new statement and not related to prior If statement.
What you can do is put the processing statement(s) on the next line after each If-Then and ElseIf-Then statements.
Example,
If memb = "Gold" Then
d = 0.005
ElseIf memb = "Platinum" Then
d = 0.01
ElseIf memb = "Black" Then
d = 0.03
End If
With this in mind, you may want to fix the succeeding If-Then-Else statements in your code. The End If part becomes meaningless if your If-Then-Else is in a single line.
Your code seems to have syntax error and error message tells you that.
Or you did not post all code?
Have a look on MS documentation: https://msdn.microsoft.com/de-de/library/752y8abs.aspx
Do you really stick to the syntax?
Without even having MS OFfice this should (be better readable and) work:
If memb = "Platinum" Then
d = 0.01
ElseIf memb = "Black" Then
d = 0.03
End If
If st >= min Then
cb = st * d
Else
cb = 0
End If
If cb >= thresh Then
MsgBox ("cb is greater than thresh")
End If
tac = st + cb
Range("B5").Value = st
Range("B7").Value = cb
Range("B9").Value = tac
I'm trying to validate a postcode from a user input within a form. Using this class, i'm trying to search through every character and check it. Then to call in within the form I have used the latter code.
The problem i'm having is that an error pops up saying
"Conversion from string "True0" to type 'Double' is not valid."
Function VaidatePostCode(ByVal Post As String) As String
For C = 0 To Len(Post) - 1
If Char.IsLetter(Post(C)) & C = 0 Then
_boolvalid = True
Else
_boolvalid = False
End If
If Char.IsLetter(Post(C)) & C = 1 Then
_boolvalid = True
Else
_boolvalid = False
End If
If Char.IsNumber(Post(C)) & C = 2 Then
_boolvalid = True
Else
_boolvalid = False
End If
If Char.IsWhiteSpace(Post(C)) & C = 3 Then
_boolvalid = True
Else
_boolvalid = False
End If
If Char.IsNumber(Post(C)) & C = 4 Then
_boolvalid = True
Else
_boolvalid = False
End If
If Char.IsLetter(Post(C)) & C = 5 Then
_boolvalid = True
Else
_boolvalid = False
End If
If Char.IsLetter(Post(C)) & C = 6 Then
_boolvalid = True
Else
_boolvalid = False
End If
Next C
Return _boolvalid
End Function
This is the code within the VB.NET form
Private Sub txtPost_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPost.Validated
If myVal.VaidatePostCode(txtPost.Text) = False Then
MsgBox("Please enter correct data format into postcode")
End If
End Sub
To check if the value is a number you can use Integer.TryParse like this:
If Not Integer.TryParse(txtPost.Text, intTemp) Then
MsgBox ...
I am creating an EXP multiplier and I do not know how to make the original variable do be separated to do fulfill 2 statements.
Aim:
I want it so that when checkbox1.checked = true checkbox2.checked = true checkbox5.checked = true, and the input = 2, then the answer will be 2 x 2x1.5 + 2 x 1.1 = 8.2 NOT 2 x 2x1.5x1.1 which gives me 6.6.
However, I tried using the separator to separate them, then add them together in the result as exptotal, but I made a mistake using exp2 with exp as inputs, as the answer is now 2 x 2x1.5 + 2 + 2 x 1.1 as my code writes exp + exp2 and exp2 is also an input so the result is now 8.2 instead of 6.2 with that extra input.
I do not know how to get around so that i can use the ORIGINAL exp such that exp2 = (exp * 1.1) without using the exp from checkbox1 and checkbox2
My code:
dim exp as double
dim exp2 as double
dim exptotal as double
If IsNumeric(TextBox9.Text) Then
exp = CDbl(TextBox9.Text)
Else
MsgBox("Please input a number.")
End If
If CheckBox1.Checked = True Then
exp = exp * 2
End If
If CheckBox2.Checked = True Then
exp = exp * 1.5
End If
If CheckBox5.Checked = True Then
exp2 = exp2 * 1.1
End If
exptotal = exp + exp2
This code makes the result includes the input 2 again as totalexp = exp + exp2 which is not what I want. I want to get rid of that extra input but still having the checkbox5 statement being ADDED together with checkbox1 and checkbox2 NOT multiplied. But I do not know how to do that. This is the closest I can come to. I hope you can understand me!
I am very confused right now, please help!!
Reading your question is soo confusing. Your variable also don't tell any story, what is CheckBox1, TextBox9 ? These are bad variable name.
Since your not telling what the logic should be but only talk about one scenario, I have to guess.
Dim input, total As Double
total = 0
If Not Double.TryParse(TextBox9, input) then
MessageBox.Show("Please input a number.")
Else
Dim part1, part2 As Double
part1 = 0
part2 = 0
If CheckBox1.Checked = True And CheckBox2.Checked = True Then
part1 = 3
Else If CheckBox1.Checked = True Then
part1 = 2
Else If CheckBox2.Checked = True Then
part1 = 1.5
End If
If CheckBox5.Checked = True Then
part2 = 1.1
End If
total = (input * part1) + (input * part2)
End If
I have 4 strings in variables a,b,c and d. I need to randomly order these variables in such a way so that I can input them into 4 different text boxes but not the same ones every time the program is ran.
I've tried to simplify it for myself by putting the strings into an array. Tell me what I'm doing wrong or if there's a way I could do it much easier.
Private Sub Random()
For i = 1 To 4
If a = 0 Then
a = r.Next(2, 5)
ElseIf b = 0 Then
Do Until b <> a
b = r.Next(2, 5)
Loop
ElseIf c = 0 Then
Do Until c <> a Or c <> b
c = r.Next(2, 5)
Loop
ElseIf d = 0 Then
Do Until d <> a Or d <> b Or d <> c
d = r.Next(2, 5)
Loop
End If
Next
End Sub
Here is one way to do it:
Dim a As String = "a"
Dim b As String = "b"
Dim c As String = "c"
Dim d As String = "d"
Dim all As String() = {a, b, c, d}
Dim random As New Random
Dim allRandom As String() = all.OrderBy(Function() random.Next).ToArray
If, for example, I wanted to define a function that returned true if a=b and b=c, and false if neither one of those equalities were true in Poly ML, how would I write it? I'm not sure how to do more than one conditional simultaneously.
Isn't
a = b andalso b = c
what you want?
I believe this does what you need:
fun f (a, b, c) =
if a = b andalso b = c
then true
else
if a <> b andalso b <> c
then false
else ... (* you haven't specified this case *)
The main points here are:
You can nest conditionals, i.e. have one if expression inside another's then or else case
The operator andalso is the boolean conjunction, meaning x andalso y is true if and only if x evaluates to true and y evaluates to true.
You can put this more concisely using a case expression:
fun f (a, b, c) =
case (a = b, b = c) of
(true, true) => true
| (false, false) => false
| _ => (* you haven't specified this case *)