|
-
Sep 2nd, 2012, 07:45 AM
#1
Thread Starter
Lively Member
[RESOLVED] Avoiding long painful conditionals
Hi guys,
I have to test for different conditions something like:
Code:
If IsFontColour = True And IsFontSize = True And IsWidth = True And IsHeight = True And IsMarginL = True _
And IsMarginR = True And IsSpeed = True And IsEasing = True Then
LongStr = Fcolor & Fsize & width & height & marginL & marginR & speed & easing
ElseIf IsFontColour = False And IsFontSize = True And IsWidth = True And IsHeight = True And IsMarginL = True _
And IsMarginR = True And IsSpeed = True And IsEasing = True Then
LongStr = Fsize & width & height & marginL & marginR & speed & easing
ElseIf IsFontColour = True And IsFontSize = False And IsWidth = True And IsHeight = True And IsMarginL = True _
And IsMarginR = True And IsSpeed = True And IsEasing = True Then
LongStr = Fcolor & width & height & marginL & marginR & speed & easing
End If
Is there a way to write this more efficiently and quicker? I have to test for all possibilities, but i've been there before and
I know it's very long, painful and easy to get confused and lost in the spaghetti.Is there an easy way to do this?
Any help is welcome,
Mike
Thanks
-
Sep 2nd, 2012, 08:52 AM
#2
Re: Avoiding long painful conditionals
Well I don't know whether it's an improvement but here's one quirky way to do it,
vb.net Code:
Dim a1 As Boolean = False 'obviously these values would normally be set in the program
Dim a2 As Boolean = False
Dim a3 As Boolean = False
Dim a4 As Boolean = True
Dim a5 As Boolean = True
Dim a6 As Boolean = True
Dim c = Tuple.Create(a1, a2, a3, a4, a5, a6) ' Tuples! Huh! What are they good for?
If c.Equals(Tuple.Create(False, False, False, True, True, True)) Then
MsgBox("Yay!")
ElseIf c.Equals(Tuple.Create(False, True, False, True, False, True)) Then
MsgBox("Nay")
Else
MsgBox("Confused Yet?")
End If
-
Sep 2nd, 2012, 09:16 AM
#3
Lively Member
Re: Avoiding long painful conditionals
Well, one way to do what I think you are doing in your example is to loop through each boolean value, check if it is true, and if it is true add the cooresponding value to the LongStr.
Example;
Code:
Dim booleanArray As Boolean() = {IsFontColour, IsFontSize, IsWidth, IsHeight, IsMarginL, IsMarginR, IsSpeed, IsEasing}
Dim objectArray As Object() = {Fcolor, Fsize, Width, Height, marginL, marginR, speed, easing}
Dim LongStr As String = Nothing
For i As Integer = 0 To booleanArray.Length - 1
If booleanArray(i) = True Then
LongStr += objectArray(i).ToString
End If
Next
MsgBox(LongStr)
-
Sep 2nd, 2012, 10:29 AM
#4
Thread Starter
Lively Member
Re: Avoiding long painful conditionals
 Originally Posted by MotoX646
Well, one way to do what I think you are doing in your example is to loop through each boolean value, check if it is true, and if it is true add the cooresponding value to the LongStr.
Example;
Code:
Dim booleanArray As Boolean() = {IsFontColour, IsFontSize, IsWidth, IsHeight, IsMarginL, IsMarginR, IsSpeed, IsEasing}
Dim objectArray As Object() = {Fcolor, Fsize, Width, Height, marginL, marginR, speed, easing}
Dim LongStr As String = Nothing
For i As Integer = 0 To booleanArray.Length - 1
If booleanArray(i) = True Then
LongStr += objectArray(i).ToString
End If
Next
MsgBox(LongStr)
Hi,
I tried but cannot get the LongStr printed out. I put in a MsgBox to test and nothing comes out. I even deleted the "objectArray(i).ToString" and wrote "Something is true". Plain string but still the msgbox is empty.
-
Sep 2nd, 2012, 10:33 AM
#5
Re: Avoiding long painful conditionals
 Originally Posted by MotoX646
Code:
Dim booleanArray As Boolean() = {IsFontColour, IsFontSize, IsWidth, IsHeight, IsMarginL, IsMarginR, IsSpeed, IsEasing}
Dim objectArray As Object() = {Fcolor, Fsize, Width, Height, marginL, marginR, speed, easing}
Dim LongStr As String = Nothing
For i As Integer = 0 To booleanArray.Length - 1
If booleanArray(i) = True Then
LongStr += objectArray(i).ToString
End If
Next
MsgBox(LongStr)
Huh? Doesn't that give exactly the same result for (forgive the shorthand) aT bT cF, aF bT cT and aT bF cT?
-
Sep 2nd, 2012, 10:51 AM
#6
Hyperactive Member
Re: Avoiding long painful conditionals
 Originally Posted by dunfiddlin
Well I don't know whether it's an improvement but here's one quirky way to do it,
vb.net Code:
Dim a1 As Boolean = False 'obviously these values would normally be set in the program
Dim a2 As Boolean = False
Dim a3 As Boolean = False
Dim a4 As Boolean = True
Dim a5 As Boolean = True
Dim a6 As Boolean = True
Dim c = Tuple.Create(a1, a2, a3, a4, a5, a6) ' Tuples! Huh! What are they good for?
If c.Equals(Tuple.Create(False, False, False, True, True, True)) Then
MsgBox("Yay!")
ElseIf c.Equals(Tuple.Create(False, True, False, True, False, True)) Then
MsgBox("Nay")
Else
MsgBox("Confused Yet?")
End If
Wow, what a great solution! I had no idea this Tuple object existed until now. It's a great one to because I can see this being extremely useful for forms that display data for editing, either pulled from a database or some sort of data source. With this it would be soo much easier to determine if a form is dirty and has unsaved changes.
Are there any perfomance improvements from using
Code:
Tuple.Create(True, True, True).Equals(Tuple.Create(True, True, True))
over
Code:
If (True And True And True) Then
?
-
Sep 2nd, 2012, 10:57 AM
#7
Lively Member
Re: Avoiding long painful conditionals
 Originally Posted by MikeSpider
Hi,
I tried but cannot get the LongStr printed out. I put in a MsgBox to test and nothing comes out. I even deleted the "objectArray(i).ToString" and wrote "Something is true". Plain string but still the msgbox is empty.
Your booleans are not declared correctly then... So the "If booleanArray(i) = True Then" is always False.
This works for me:
Code:
Dim IsFontColour As Boolean = True
Dim IsFontSize As Boolean = True
Dim IsWidth As Boolean = True
Dim IsHeight As Boolean = True
Dim IsMarginL As Boolean = False
Dim IsMarginR As Boolean = True
Dim IsSpeed As Boolean = True
Dim IsEasing As Boolean = True
Dim Fcolor As String = "Red"
Dim Fsize As Integer = "777"
Dim Width As Integer = "512"
Dim Height As Integer = "128"
Dim marginL As Integer = "123"
Dim marginR As String = "456"
Dim speed As Single = "1.234"
Dim easing As Single = "5.678"
Dim booleanArray As Boolean() = {IsFontColour, IsFontSize, IsWidth, IsHeight, IsMarginL, IsMarginR, IsSpeed, IsEasing}
Dim objectArray As Object() = {Fcolor, Fsize, Width, Height, marginL, marginR, speed, easing}
Dim LongStr As String = ""
For i As Integer = 0 To booleanArray.Length - 1
If booleanArray(i) = True Then
LongStr += objectArray(i).ToString
End If
Next
MsgBox(LongStr)
Last edited by MotoX646; Sep 2nd, 2012 at 11:08 AM.
-
Sep 2nd, 2012, 11:05 AM
#8
Lively Member
Re: Avoiding long painful conditionals
 Originally Posted by dunfiddlin
Huh? Doesn't that give exactly the same result for (forgive the shorthand) aT bT cF, aF bT cT and aT bF cT?
No, it doesn't give the same result.
All he (and my code) is doing is building a string of data based on what is true or false. If a boolean is false, it just doesn't add the data to the string.
Here is another way to do the same thing...
Code:
Dim IsFontColour As Boolean = True
Dim IsFontSize As Boolean = True
Dim IsWidth As Boolean = True
Dim IsHeight As Boolean = True
Dim IsMarginL As Boolean = False
Dim IsMarginR As Boolean = True
Dim IsSpeed As Boolean = True
Dim IsEasing As Boolean = True
Dim Fcolor As String = "Red"
Dim Fsize As Integer = "777"
Dim Width As Integer = "512"
Dim Height As Integer = "128"
Dim marginL As Integer = "123"
Dim marginR As String = "456"
Dim speed As Single = "1.234"
Dim easing As Single = "5.678"
Dim LongStr As New System.Text.StringBuilder
If IsFontColour Then LongStr.Append(Fcolor.ToString)
If IsFontSize Then LongStr.Append(Fsize.ToString)
If IsWidth Then LongStr.Append(Width.ToString)
If IsHeight Then LongStr.Append(Height.ToString)
If IsMarginL Then LongStr.Append(marginL.ToString)
If IsMarginR Then LongStr.Append(marginR.ToString)
If IsSpeed Then LongStr.Append(speed.ToString)
If IsEasing Then LongStr.Append(easing.ToString)
MsgBox(LongStr.ToString)
Last edited by MotoX646; Sep 2nd, 2012 at 11:27 AM.
-
Sep 2nd, 2012, 12:22 PM
#9
Thread Starter
Lively Member
Re: Avoiding long painful conditionals
I'm trying to use the For loop solution but I'm getting an error "Conversion from string "" to type 'Integer' is not valid."
Code:
Dim BooleanArray As Boolean() = {IsWidth, IsHeight, IsMarginL, IsMarginR, IsFontColour, IsFontSize, IsSpeed, IsEasing, IsCallBack, IsReset}
Dim PropertyArray As Object() = {prpt1, prpt2, prpt3, prpt4, prpt5, prpt6, speed, easing, callBackF, resetId}
Dim AnimMainStr As String = ""
Private Sub Button1_Click(.....)
....
If fontColor <> "" Then
BooleanArray(IsFontColour) = True
PropertyArray(prpt1) = "fontColour:'" & fontColor & "'" ---->> the error is in this line
End If
...
For i As Integer = 0 To BooleanArray.Length - 1
If BooleanArray(i) = True Then
AnimMainStr += "$('#" & triggerId & "').click(function(){" & vbCrLf & _
vbTab & " $('#" & targetId & "').animate({ " & vbCrLf & _
vbTab & vbTab & PropertyArray(i).ToString & "," & vbCrLf & _
vbTab & vbTab & "}) " & vbCrLf & _
vbTab & ";});"
...
Anyways, I will experiment with the other sugestions,
Thanks,
Mike
-
Sep 2nd, 2012, 12:33 PM
#10
Lively Member
Re: Avoiding long painful conditionals
 Originally Posted by MikeSpider
I'm trying to use the For loop solution but I'm getting an error "Conversion from string "" to type 'Integer' is not valid."
Code:
BooleanArray(IsFontColour) = True
PropertyArray(prpt1) = "fontColour:'" & fontColor & "'" ---->> the error is in this line
Arrays only accept integers for their index position, not strings. The index starts at 0 and counts up.
So to change the value of "IsFontColour" in "BooleanArray", you need to provide the correct index number.
Code:
BooleanArray(4) = True
To change "prpt1" in "PropertyArray", do this:
Code:
PropertyArray(0) = "fontColour:'" & fontColor & "'"
Last edited by MotoX646; Sep 2nd, 2012 at 12:48 PM.
-
Sep 2nd, 2012, 01:08 PM
#11
Thread Starter
Lively Member
Re: Avoiding long painful conditionals
Yes you are right.
Thanks everybody for the suggestions and all the help 
Mike
-
Sep 2nd, 2012, 01:47 PM
#12
Thread Starter
Lively Member
Re: [RESOLVED] Avoiding long painful conditionals
I still have a problem.
Sorry for marking it resolved so early.
The conditionals are not working as I want to.
The values appear empty but still appear.
Code:
If marginL <> "" Then
IsMarginL = True
prpt5 = "marginLeft:" & marginL
Else
IsMarginL = False
End If
If IsMarginL = True Then
AnimMainStr.Append("," & vbCrLf & vbTab & vbTab & prpt5)
End If
although it is false it still appends the word "marginLeft:" although with no value.
It should not append anything at all bc is set to false.
I dont know what i'm doing wrong here.
Thanks,
Mike
-
Sep 2nd, 2012, 02:15 PM
#13
Lively Member
Re: [RESOLVED] Avoiding long painful conditionals
 Originally Posted by MikeSpider
Code:
If marginL <> "" Then
IsMarginL = True
prpt5 = "marginLeft:" & marginL
Else
IsMarginL = False
End If
If IsMarginL = True Then
AnimMainStr.Append("," & vbCrLf & vbTab & vbTab & prpt5)
End If
although it is false it still appends the word "marginLeft:" although with no value.
It should not append anything at all bc is set to false.
I dont know what i'm doing wrong here.
What is the value of marginL when you get the error?
Because, obviously, if it appends the word "marginLeft:" then IsMarginL must be true, which means marginL must contain text.
If marginL is a string, I would try this:
Code:
If Not marginL = "" Then
IsMarginL = True
prpt5 = "marginLeft:" & marginL
Else
IsMarginL = False
End If
-
Sep 2nd, 2012, 02:53 PM
#14
Re: [RESOLVED] Avoiding long painful conditionals
One thing that will save you a bit of typing and be less odd is to not explicitly state boolean values in your test:
Use:
Code:
If IsFontColour Then
rather than:
Code:
If IsFontColour = True Then
The latter sounds awkward - e.g., you don't ask "is it raining equals true?", you ask "is it raining?".
-
Sep 2nd, 2012, 02:55 PM
#15
Re: [RESOLVED] Avoiding long painful conditionals
In situations such as this one, I usually prefer to use good old-fashioned bit-fields, as examplified by:
VB.NET Code:
<FlagsAttribute()> Public Enum MyBitfield IsFontColour = &H1 'Value gets doubled for each field so each one occupy a single bit of a 32 bit number (ie. max 32 entries). IsFontSize = &H2 IsWidth = &H4 IsHeight = &H8 IsMarginL = &H10 IsMarginR = &H20 IsSpeed = &H40 IsEasing = &H80 End Enum Public Class MyBitClass Public Function TestBitfields(ByVal sBitValue As MyBitfield) As Boolean 'Set test to hold True for IsFontSize, IsSpeed and IsHeight and False for all others fields. Dim test As MyBitfield = MyBitfield.IsFontSize Or MyBitfield.IsSpeed Or MyBitfield.IsHeight If (sBitValue And test) = test Then 'Simultaneous test for 3 True and 5 False booleans '... End If 'Can also be used as: If (sBitValue And &HCB) <> 0 Then 'I get to here, if one of IsEasing, IsSpeed, IsHeight, IsFontSize or IsFontColour is True. End If Return test = MyBitfield.IsMarginL End Function End Class
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Sep 2nd, 2012, 03:07 PM
#16
Thread Starter
Lively Member
Re: [RESOLVED] Avoiding long painful conditionals
 Originally Posted by MotoX646
What is the value of marginL when you get the error?
Because, obviously, if it appends the word "marginLeft:" then IsMarginL must be true, which means marginL must contain text.
If marginL is a string, I would try this:
Code:
If Not marginL = "" Then
IsMarginL = True
prpt5 = "marginLeft:" & marginL
Else
IsMarginL = False
End If
I'm not getting any error. Just not the expected results.
And Yes it's a String datatype.
I tried your example still the same.
I dont understand Why is not detecting the boolean at all.
Code:
'setting properties vars
If txtResetId.Text <> "" Then
IsReset = True
Else
IsReset = False
End If
If fontColor <> "" Then
IsFontColour = True
prpt1 = "fontColour:'" & fontColor & "'"
Else
IsFontColour = False
End If
Dim FSize As String = CStr(fontSize)
If FSize <> "" Then
IsFontSize = True
prpt2 = "fontSize:'" & fontSize & "px'"
Else
IsFontSize = False
End If
If Not widthAnim = "" Then
IsWidth = True
prpt3 = "width:" & widthAnim
Else
IsWidth = False
End If
'If widthAnim <> "" Then
' IsWidth = True
' prpt3 = "width:" & widthAnim
'Else
' IsWidth = False
'End If
If heightAnim <> "" Then
IsHeight = True
prpt4 = "height:" & heightAnim
Else
IsHeight = False
End If
If marginL <> "" Then
IsMarginL = True
prpt5 = "marginLeft:" & marginL
Else
IsMarginL = False
End If
If marginR <> "" Then
IsMarginR = True
prpt6 = "marginRight:" & marginR
Else
IsMarginR = False
End If
'setting speed vars
If rbSlow.Checked = True Then
speed = "'" & rbSlow.Text & "'"
IsSpeed = True
ElseIf rbFast.Checked = True Then
speed = "'" & rbFast.Text & "'"
IsSpeed = True
End If
If txtSpeed.Text <> "" Then
speed = txtSpeed.Text
IsSpeed = True
End If
'Setting Easing vars
If rbSwing.Checked = True Then
easing = "'" & rbSwing.Text & "'"
IsEasing = True
ElseIf rbLinear.Checked = True Then
easing = "'" & rbLinear.Text & "'"
IsEasing = True
End If
'Setting callback function vars
If cbCallBack.CheckState = CheckState.Checked Then
callBackF = txtCallBack.Text
IsCallBack = True
End If
Dim firstParam As String = "$('#" & triggerId & "').click(function(){" & vbCrLf & _
vbTab & " $('#" & targetId & "').animate({ "
AnimMainStr.Append(firstParam.ToString)
If IsWidth = True Then
AnimMainStr.Append(vbCrLf & vbTab & vbTab & prpt3)
End If
If IsHeight = True Then
AnimMainStr.Append("," & vbCrLf & vbTab & vbTab & prpt4)
End If
If IsMarginL = True Then
AnimMainStr.Append("," & vbCrLf & vbTab & vbTab & prpt5)
End If
If IsMarginR = True Then
AnimMainStr.Append("," & vbCrLf & vbTab & vbTab & prpt6)
End If
If IsFontSize = True Then
AnimMainStr.Append("," & vbCrLf & vbTab & vbTab & prpt2)
End If
If IsSpeed = True Then
AnimMainStr.Append(vbCrLf & vbTab & "}," & speed)
End If
If IsEasing = True Then
AnimMainStr.Append("," & easing)
End If
If IsCallBack = True Then
AnimMainStr.Append("," & callBackF & ");")
End If
If IsFontColour = True Then
AnimMainStr.Append(vbCrLf & vbTab & "$('#" & targetId & "').css({""color"":""" & fontColor & """})" & vbCrLf _
& vbTab & "});")
End If
MsgBox(AnimMainStr.ToString)
Thanks,
Mike
-
Sep 2nd, 2012, 03:08 PM
#17
Re: [RESOLVED] Avoiding long painful conditionals
Well it's clever but it surely adds another level of complication in that now you need to remember what to set the values to in the program rather than just choosing from True & False. And where the values are for intrinsic properties, you also need to set an intermediary variable. As the original intention was to simplify in order to prevent errors I'm not sure this would be my favourite solution.
-
Sep 2nd, 2012, 03:15 PM
#18
Re: [RESOLVED] Avoiding long painful conditionals
 Originally Posted by MikeSpider
I'm not getting any error. Just not the expected results.
And Yes it's a String datatype.
I tried your example still the same.
I dont understand Why is not detecting the boolean at all.
Code:
'setting properties vars
If txtResetId.Text <> "" Then
IsReset = True
Else
IsReset = False
End If
If fontColor <> "" Then
IsFontColour = True
prpt1 = "fontColour:'" & fontColor & "'"
Else
IsFontColour = False
End If
Dim FSize As String = CStr(fontSize)
If FSize <> "" Then
IsFontSize = True
prpt2 = "fontSize:'" & fontSize & "px'"
Else
IsFontSize = False
End If
If Not widthAnim = "" Then
IsWidth = True
prpt3 = "width:" & widthAnim
Else
IsWidth = False
End If
'If widthAnim <> "" Then
' IsWidth = True
' prpt3 = "width:" & widthAnim
'Else
' IsWidth = False
'End If
If heightAnim <> "" Then
IsHeight = True
prpt4 = "height:" & heightAnim
Else
IsHeight = False
End If
If marginL <> "" Then
IsMarginL = True
prpt5 = "marginLeft:" & marginL
Else
IsMarginL = False
End If
If marginR <> "" Then
IsMarginR = True
prpt6 = "marginRight:" & marginR
Else
IsMarginR = False
End If
'setting speed vars
If rbSlow.Checked = True Then
speed = "'" & rbSlow.Text & "'"
IsSpeed = True
ElseIf rbFast.Checked = True Then
speed = "'" & rbFast.Text & "'"
IsSpeed = True
End If
If txtSpeed.Text <> "" Then
speed = txtSpeed.Text
IsSpeed = True
End If
'Setting Easing vars
If rbSwing.Checked = True Then
easing = "'" & rbSwing.Text & "'"
IsEasing = True
ElseIf rbLinear.Checked = True Then
easing = "'" & rbLinear.Text & "'"
IsEasing = True
End If
'Setting callback function vars
If cbCallBack.CheckState = CheckState.Checked Then
callBackF = txtCallBack.Text
IsCallBack = True
End If
Dim firstParam As String = "$('#" & triggerId & "').click(function(){" & vbCrLf & _
vbTab & " $('#" & targetId & "').animate({ "
AnimMainStr.Append(firstParam.ToString)
If IsWidth = True Then
AnimMainStr.Append(vbCrLf & vbTab & vbTab & prpt3)
End If
If IsHeight = True Then
AnimMainStr.Append("," & vbCrLf & vbTab & vbTab & prpt4)
End If
If IsMarginL = True Then
AnimMainStr.Append("," & vbCrLf & vbTab & vbTab & prpt5)
End If
If IsMarginR = True Then
AnimMainStr.Append("," & vbCrLf & vbTab & vbTab & prpt6)
End If
If IsFontSize = True Then
AnimMainStr.Append("," & vbCrLf & vbTab & vbTab & prpt2)
End If
If IsSpeed = True Then
AnimMainStr.Append(vbCrLf & vbTab & "}," & speed)
End If
If IsEasing = True Then
AnimMainStr.Append("," & easing)
End If
If IsCallBack = True Then
AnimMainStr.Append("," & callBackF & ");")
End If
If IsFontColour = True Then
AnimMainStr.Append(vbCrLf & vbTab & "$('#" & targetId & "').css({""color"":""" & fontColor & """})" & vbCrLf _
& vbTab & "});")
End If
MsgBox(AnimMainStr.ToString)
Thanks,
Mike
We seem to have gotten a long, long way from
to write this more efficiently and quicker
even if it did work!
-
Sep 2nd, 2012, 03:20 PM
#19
Thread Starter
Lively Member
Re: [RESOLVED] Avoiding long painful conditionals
The only boolean being detected is :
Code:
'Setting callback function vars
If cbCallBack.CheckState = CheckState.Checked Then
callBackF = txtCallBack.Text
IsCallBack = True
End If
All the <>"" are not working. I mean, all checks for empty fields are not working I dont undertand.
Last edited by MikeSpider; Sep 2nd, 2012 at 03:31 PM.
-
Sep 2nd, 2012, 03:25 PM
#20
Thread Starter
Lively Member
Re: [RESOLVED] Avoiding long painful conditionals
Well it's clever but it surely adds another level of complication in that now you need to remember what to set the values to in the program rather than just choosing from True & False. And where the values are for intrinsic properties, you also need to set an intermediary variable. As the original intention was to simplify in order to prevent errors I'm not sure this would be my favourite solution.
Yeah man, I was having a hard time appending everything in a for loop, but will need to practice in a spare time.
-
Sep 2nd, 2012, 03:48 PM
#21
Lively Member
Re: [RESOLVED] Avoiding long painful conditionals
 Originally Posted by David Anton
One thing that will save you a bit of typing and be less odd is to not explicitly state boolean values in your test:
Use:
Code:
If IsFontColour Then
rather than:
Code:
If IsFontColour = True Then
The latter sounds awkward - e.g., you don't ask "is it raining equals true?", you ask "is it raining?".
I knew someone would mention that. When giving example code I always explicitly state boolean values because I feel it is easier to read and follow for "newbies". But I agree that it's easier to not explicitly state them.
I disagree however that it sounds awkward. Your example sounds awkward only because you treated it like a question. An If statement is not a question, it's just a statement. It's not asking "is it raining?", it stating "If 'it is raining' equals true, then...". In long form it would mean, "If the statement that "it is raining" is true, then...".
Not explicitly stating the boolean value is what I think sounds awkward when you use "If Not"...
Code:
If ItsRaining = False Then
Is a lot more straight forward than:
Code:
If Not ItsRaining Then
If Not ItsRaining?

Anyway...
-
Sep 2nd, 2012, 03:54 PM
#22
Thread Starter
Lively Member
Re: [RESOLVED] Avoiding long painful conditionals
Now this is very weird!
I commented everything and just wrote these lines:
Code:
If txtMarginL.Text <> "" Then
MsgBox("It's not empty")
Else
MsgBox("It's empty")
End If
I dont write anything in the textbox but it still says "it's not empty!" if I put the cursor in the text field and press
"delete" even though i didn't write anything, it then says " it's empty!" .
Weird stuff!
-
Sep 2nd, 2012, 04:03 PM
#23
Re: [RESOLVED] Avoiding long painful conditionals
 Originally Posted by MotoX646
I knew someone would mention that. When giving example code I always explicitly state boolean values because I feel it is easier to read and follow for "newbies". But I agree that it's easier to not explicitly state them.
I disagree however that it sounds awkward. Your example sounds awkward only because you treated it like a question. An If statement is not a question, it's just a statement. It's not asking "is it raining?", it stating "If 'it is raining' equals true, then...". In long form it would mean, "If the statement that "it is raining" is true, then...".
Not explicitly stating the boolean value is what I think sounds awkward when you use "If Not"...
Code:
If ItsRaining = False Then
Is a lot more straight forward than:
Code:
If Not ItsRaining Then
If Not ItsRaining?
Anyway...
Well, yeah, but it's all totally arbitrary. There's nothing awkward about 'If Raining' and 'If Not Raining', is there? It's not meant to be read as an English essay. It's meant to be read as a computer program. And as a computer program, 'If VarName Then' and 'If Not VarName Then' are both functional and comprehensible.
-
Sep 2nd, 2012, 04:35 PM
#24
Lively Member
Re: [RESOLVED] Avoiding long painful conditionals
Your original question was about avoiding long conditional statements. You wanted a conditional statement for every possible combination of 8 booleans, so you can create a string with every possible combination of properties in it. That is a very large number of possible combinations. Boolean is True or False, 1 or 0, that is base-2. With 8 different booleans you have 2^8 combinations. That would mean 256 different combinations... and conditional statements. That is just NOT the right way to go about doing what you were doing.
That is why, after looking at your code, I realized you don't really need every possible combination... You were just adding values to a string if the corresponding boolean was true. That is a pretty simple task... But somehow you have made it more complex than it needs to be...
 Originally Posted by MikeSpider
The only boolean being detected is :
All the <>"" are not working. I mean, all checks for empty fields are not working I dont undertand.
That is because your fields are probably not empty.
Instead of:
Code:
If fontColor <> "" Then
End If
Try:
Code:
If Not fontColor = Nothing Then
IsFontColour = True
prpt1 = "fontColour:'" & fontColor & "'"
Else
IsFontColour = False
End If
Use MsgBox's to help debug your code... See what the values are set to. See what value IsFontColour is set to...
Code:
msgbox(fontColor)
If Not fontColor = Nothing Then
IsFontColour = True
prpt1 = "fontColour:'" & fontColor & "'"
Else
IsFontColour = False
End If
MsgBox(IsFontColour.tostring)
If you don't tell me/us the values, then we can't help you.
-
Sep 2nd, 2012, 04:41 PM
#25
Lively Member
Re: [RESOLVED] Avoiding long painful conditionals
 Originally Posted by dunfiddlin
It's not meant to be read as an English essay. It's meant to be read as a computer program..
Well, the original goal for Visual Basic was to make it read like a normal human would speak. Hence why it has this:
For x as integer = 0 to 10 Step 1
Next
...and not....
for (x=0;x<10:x++)
{
}
-
Sep 2nd, 2012, 04:49 PM
#26
Lively Member
Re: [RESOLVED] Avoiding long painful conditionals
 Originally Posted by MikeSpider
Now this is very weird!
I commented everything and just wrote these lines:
Code:
If txtMarginL.Text <> "" Then
MsgBox("It's not empty")
Else
MsgBox("It's empty")
End If
I dont write anything in the textbox but it still says "it's not empty!" if I put the cursor in the text field and press
"delete" even though i didn't write anything, it then says " it's empty!" .
Weird stuff!
Oh ok, I just now read your post...
Are you loading some non-unicode characters into your textbox? Some characters are invisible to the eye, but there is a character present.
-
Sep 2nd, 2012, 05:32 PM
#27
Thread Starter
Lively Member
Re: [RESOLVED] Avoiding long painful conditionals
 Originally Posted by MotoX646
Oh ok, I just now read your post...
Are you loading some non-unicode characters into your textbox? Some characters are invisible to the eye, but there is a character present.
Thanks I already sorted it out.
Your original question was about avoiding long conditional statements. You wanted a conditional statement for every possible combination of 8 booleans, so you can create a string with every possible combination of properties in it. That is a very large number of possible combinations. Boolean is True or False, 1 or 0, that is base-2. With 8 different booleans you have 2^8 combinations. That would mean 256 different combinations... and conditional statements. That is just NOT the right way to go about doing what you were doing.
That is why, after looking at your code, I realized you don't really need every possible combination... You were just adding values to a string if the corresponding boolean was true. That is a pretty simple task... But somehow you have made it more complex than it needs to be...
Can you show me the simpler way then, i could use it for the future. Is it the loop way?
Thanks
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
|