i want to konw which code is more faster.
compare this two code
Dim x As Boolean
Dim y As Boolean
If x = True Then
If y = True Then
Else
End If
Else
If y = True Then
Else
End If
End If
'=============================================
Enum xx
o = 0
x = 1
xy = 2
y = 3
End Enum
Sub Script_validate()
Dim nn As xx
If nn.o Then
ElseIf nn.x Then
ElseIf nn.xy Then
ElseIf nn.y Then
End If
End Sub
which code is faster and which code had low memory usage
My program is a WIP RPG battle game, where the user has to battle monsters to progress. I want to make it so each time the player uses their melee attack, the attack amount is randomized. However each time I load the program, the first time the player presses the button to trigger the attack, no attack occurs, even though other things are supposed to happen when the button is clicked. Such as other buttons becoming disabled.
This is the stub for the button. If necessary I can post the full code.
Public Sub btnMeleeAttack_Click(sender As Object, e As EventArgs) Handles btnMeleeAttack.Click
tmrRoundCount.Start()
tmrTurn.Start()
btnMeleeAttack.Enabled = False
btnRangedAttack.Enabled = False
btnDefend.Enabled = False
btnUseItem.Enabled = False
If intRoundCountTicks = 0 Then
dblEnemyHealthOld = dblEnemyHealthMax
End If
dblEnemyHealth = dblEnemyHealth - (dblMelee / dblEnemyDefense)
lblEnemyStats.Text = dblEnemyHealth & "/" & dblEnemyHealthMax & " HP" & vbCrLf & dblEnemyDefense & " DEF" & vbCrLf & dblEnemyAttack & " ATK"
Randomize()
dblRandomAttackVar = CInt(Int((9 - 1 + 1) * Rnd() + 1))
If dblRandomAttackVar = 1 Then
dblMelee = 3
End If
If dblRandomAttackVar = 2 And intMeleeAttackCount <> 0 Then
dblMelee = 3.25
End If
If dblRandomAttackVar = 3 And intMeleeAttackCount <> 0 Then
dblMelee = 3.5
End If
If dblRandomAttackVar = 4 And intMeleeAttackCount <> 0 Then
dblMelee = 3.75
End If
If dblRandomAttackVar = 5 And intMeleeAttackCount <> 0 Then
dblMelee = 4
End If
If dblRandomAttackVar = 6 And intMeleeAttackCount <> 0 Then
dblMelee = 4.25
End If
If dblRandomAttackVar = 7 And intMeleeAttackCount <> 0 Then
dblMelee = 4.5
End If
If dblRandomAttackVar = 8 And intMeleeAttackCount <> 0 Then
dblMelee = 4.75
End If
If dblRandomAttackVar = 9 And intMeleeAttackCount <> 0 Then
dblMelee = 5
End If
If intMeleeAttackCount = 0 Then
dblMelee = 4.5
End If
End Sub
Without the full code, this might be a guess but I think your conditions are the reason of this problem:
If dblRandomAttackVar = 2 And intMeleeAttackCount <> 0 Then
dblMelee = 3.25
End If
I suppose that, for the first attack, intMeleeAttackCount actually is 0. This means that, unless the first attack has a roll 1, the attack will never be executed, as intMeleeAttackCount doesn't fit the condition.
Unrelated: you can pretify your code like this:
If dblRandomAttackVar = 1 Then
dblMelee = 3
ElseIf dblRandomAttackVar = 2 And intMeleeAttackCount <> 0 Then
dblMelee = 3.25
ElseIf dblRandomAttackVar = 3 And intMeleeAttackCount <> 0 Then
dblMelee = 3.5
'...
End If
I am fiddling a bit with your code to help you on your way but let's try this one here first:
Answer
You say "no attack occurs". Is the only feedback you get the status message in lblEnemyStats?
Then you should update that after your calculations and not before.
Public Sub btnMeleeAttack_Click(sender As Object, e As EventArgs) Handles btnMeleeAttack.Click
tmrRoundCount.Start()
tmrTurn.Start()
btnMeleeAttack.Enabled = False
btnRangedAttack.Enabled = False
btnDefend.Enabled = False
btnUseItem.Enabled = False
If intRoundCountTicks = 0 Then
dblEnemyHealthOld = dblEnemyHealthMax
End If
dblEnemyHealth = dblEnemyHealth - (dblMelee / dblEnemyDefense)
Randomize()
dblRandomAttackVar = CInt(Int((9 - 1 + 1) * Rnd() + 1))
If dblRandomAttackVar = 1 Then
dblMelee = 3
End If
If dblRandomAttackVar = 2 And intMeleeAttackCount <> 0 Then
dblMelee = 3.25
End If
If dblRandomAttackVar = 3 And intMeleeAttackCount <> 0 Then
dblMelee = 3.5
End If
If dblRandomAttackVar = 4 And intMeleeAttackCount <> 0 Then
dblMelee = 3.75
End If
If dblRandomAttackVar = 5 And intMeleeAttackCount <> 0 Then
dblMelee = 4
End If
If dblRandomAttackVar = 6 And intMeleeAttackCount <> 0 Then
dblMelee = 4.25
End If
If dblRandomAttackVar = 7 And intMeleeAttackCount <> 0 Then
dblMelee = 4.5
End If
If dblRandomAttackVar = 8 And intMeleeAttackCount <> 0 Then
dblMelee = 4.75
End If
If dblRandomAttackVar = 9 And intMeleeAttackCount <> 0 Then
dblMelee = 5
End If
If intMeleeAttackCount = 0 Then
dblMelee = 4.5
End If
lblEnemyStats.Text = dblEnemyHealth & "/" & dblEnemyHealthMax & " HP" & vbCrLf & dblEnemyDefense & " DEF" & vbCrLf & dblEnemyAttack & " ATK"
End Sub
To clarify
I think your code does what it is supposed to do, but yout UI get's updated too late. Let's say each Button is a "Tick" on the timeline of the game. Then it's
0 -> Click -> 1 -> Click -> 2 -> Click -> 3 and so on.
That is the current state of your "round". But the User Interface lag's behind, so your Interpretation of whatever is displayed will be wrong, too because what you SEE is not what you HAVE - your Question proves that - you thought the button did nothing and then started working, which is "mysterious" and will lead to an avalanche of mistakes.
Game: 0->1->2->3->4->Monster dead?!?
UI: 0->0->1->2->3->Monster not dead yet?!?
Bonus
This might make things easier to debug. Use comments, they are your best friend in this state of learning to program.
I do however not claim to understand more than half of your code.
Private Sub toggleFightButtons(onOff As Boolean)
btnMeleeAttack.Enabled = onOff
btnRangedAttack.Enabled = onOff
btnDefend.Enabled = onOff
btnUseItem.Enabled = onOff
End Sub
Private Sub showStats()
Dim stats As String = String.Format("{0}/{1} HP {2} DEF {3} ATK", dblEnemyHealth, dblEnemyHealthMax, dblEnemyDefense, dblEnemyAttack)
lblEnemyStats.Text = stats
End Sub
Public Sub btnMeleeAttack_Click(sender As Object, e As EventArgs) Handles btnMeleeAttack.Click
'Suspend interaction with the game
tmrRoundCount.Start()
tmrTurn.Start()
'????
If intRoundCountTicks = 0 Then
dblEnemyHealthOld = dblEnemyHealthMax
End If
'Introduce a little bit of RNG
dblEnemyHealth = dblEnemyHealth - (dblMelee / dblEnemyDefense)
Randomize()
dblRandomAttackVar = CInt(Int(9 * Rnd() + 1))
'Calculate the result
If intMeleeAttackCount = 0 Then
dblMelee = 4.5
Else
dblMelee = 3 + (0.25 * (dblRandomAttackVar - 1))
End If
'Update UI to show what it is we did
showStats()
End Sub
This does the same as your code and could still be simplified more.
Sorry i think this is pretty basic but I was wondering if somebody could tell me why only 1 of these IF statements seem to run. The 3rd IF statement for the "CASH" option works but the other 2 unfortunately don't.
Sub HideUnhide_Discount()
If Range("Payment_Option") = "Subscription" Then
Range("MnthD_Row").EntireRow.Hidden = False
Range("MnthD").Value = 0
Else
Range("MnthD_Row").EntireRow.Hidden = True
End If
If Range("Payment_Option") = "Lease" Then
Range("OOD_Row").EntireRow.Hidden = False
Range("Leasing_Info").EntireRow.Hidden = False
Range("OOD").Value = 0
Else
Range("OOD_Row").EntireRow.Hidden = True
Range("Leasing_Info").EntireRow.Hidden = True
End If
If Range("Payment_Option") = "Cash" Then
Range("OOD_Row").EntireRow.Hidden = False
Range("MnthD_Row").EntireRow.Hidden = False
Range("OOD").Value = 0
Else
Range("OOD_Row").EntireRow.Hidden = True
Range("MnthD_Row").EntireRow.Hidden = True
End If
End Sub
Try replacing your multiple If >> Else condition with the Select Case below:
Sub HideUnhide_Discount()
' first reset all rows to be visible , later according to the value, unhide specific rows
Range("MnthD_Row").EntireRow.Hidden = True
Range("OOD_Row").EntireRow.Hidden = True
Range("Leasing_Info").EntireRow.Hidden = True
Select Case Range("Payment_Option")
Case "Subscription"
Range("MnthD_Row").EntireRow.Hidden = False
Range("MnthD").Value = 0
Case "Lease"
Range("OOD_Row").EntireRow.Hidden = False
Range("Leasing_Info").EntireRow.Hidden = False
Range("OOD").Value = 0
Case "Cash"
Range("OOD_Row").EntireRow.Hidden = False
Range("MnthD_Row").EntireRow.Hidden = False
Range("OOD").Value = 0
End Select
End Sub
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 trying to add a property called NumDBsToDisplay that gives the option of selecting between 0 and 3 databases to be displayed on my custom control. I can't get the property to show up in the designer, am I missing something here?
Here's the code:
Public Property NumDBsToDisplay(ByVal i As Integer) As Integer
Get
Dim count As Integer = 0
If cboPridb.Visible = True Then
count += 1
End If
If cboSecdb.Visible = True Then
count += 1
End If
If cboTridb.Visible = True Then
count += 1
End If
Return count
End Get
Set(ByVal value As Integer)
If value = 0 Then
cboPridb.Visible = False
cboSecdb.Visible = False
cboTridb.Visible = False
ElseIf value = 1 Then
lblPridB.Text = "Primary Database"
cboPridb.Visible = True
cboSecdb.Visible = False
cboTridb.Visible = False
ElseIf value = 2 Then
lblPridB.Text = "Primary Database"
lblSecDB.Text = "Secondary Database"
cboPridb.Visible = True
cboSecdb.Visible = True
cboTridb.Visible = False
Else
lblPridB.Text = "Primary Database"
lblSecDB.Width = 131
lblSecDB.Text = "Secondary Database"
lblTriDB.Text = "Tertiary Database"
cboPridb.Visible = True
cboSecdb.Visible = True
cboTridb.Visible = True
End If
End Set
End Property
Thank you for the help.