Post: [TUT]Improve your Coding[VB]
05-10-2011, 12:11 AM #1
(adsbygoogle = window.adsbygoogle || []).push({});
Originally posted by kingdeath360 View Post
Improve your Coding[/align][/size][/color]

This is a small 'guide' on how to improve your coding and make it more clear. You can keep the overview, following these tips.

[size=large]Rename your controls[/size]


This is important to actually keep your code clean.

Do not keep the default control names like Button1/Textbox1.

Rename them.

Textbox1 = txtResult
Button1 = cmdCalculate

You can now easily recognize your events in your code, instead of having to search them.

[size=large]Comment your code[/size]

This is obvious. You may forget stuff later on, therefore you can just add comments into your code.
    '[color=#32CD32]comment  [/color]


[size=large]Use your datatypes correctly[/size]
    Dim int as integer = "5"
Dim str as string = "Sample Text"

txtResult.text = int


Integer <> String. Therefore the correct way is:
    Dim int as integer = 5 


[size=large]Shorten your code[/size]

Instead of writing:
    txtResult.text = txtResult.text + int

You better use:
    txtResult.text += int  


[size=large]Using[/size]
    Dim str as IO.Streamreader = New Streamreader("C:\test.txt")

txtResult.text = str.readtoend

str.dispose


    Using str as new Io.Streamreader("C:\test.txt")

txtResult.text = str.readtoend

End Using


[size=large]Case instead of If[/size]

    If txtResult.text = "a" Then
Msgbox("Newb")
End if

If txtResult.Text = "b" Then
Msgbox("Newbie")
End if

If txtResult.text = "c" Then
Msgbox("Noob")
End if


    Select Case txtresult.Text

Case "a"
Msgbox("Newb")

Case "b"
Msgbox("Newbie")

Case "c"
Msgbox("PM Me xD")

End Select


[size=large]If-Structures[/size]

If condition Then
[statements]
ElseIf condition Then
[statements]
Else
[statements]
End If

    If textbox1.text = "HakerDz"
'do something
elseif Textbox1.text = "HakerDz"
'do something else
else 'if it's neither HakerDz nor HakerDz
'something
end if


Using Case...

    Select Case Textbox1.Text
Case "meh"
'do something
Case "Duh"
'do something else
Case Else
'something
end select

    
If Textbox1.text <> "" then 'if it's not empty
end if


If not...

    If not textbox1.text = "" then
end if


[size=large]Declare your own Subs/Function[/size]
You may have a lot of repetitive code. Therefore, you can shorten your code a lot by building functions or subs.

[size=large]Structure[/size]

    Dim firstPos as point
Dim firstDir as string
Dim firstFrame as integer

Dim secondPos as point
Dim secondDir as string
Dim secondFrame as integer

Dim thirdPos as point
Dim thirdDir as string
Dim thirdFrame as integer


    Structure meh
Dim Pos As Point
Dim Dir As String
Dim Frame As Integer
end structure

dim first as meh
dim second as meh
dim third as meh


[size=large]Booleans[/size]
    Dim b as boolean 'by default the boolean is FALSE  

    
If b = true then
'do something
end if

If b = false Then
'do something
end if


    if b then 'true
'do something
else 'false
'do something
end if


[size=large]Try-Catch[/size]
To avoid your program from closing when an error appears, you can use Try-Catch Blocks.
    Try
Timer.Value = txtResult.Text 'user may enter a letter instead of a number
Catch ex as exception
Msgbox(ex.Tostring) 'optional
End try


However, do not use them if they are not necessary.
    Try
Timer.Value = nudValue.Value 'NumericUpDown
Catch ex as exception
end try


[size=large]Regions[/size]
You can use Regions to make your code more clear.
    #Region "Settings"

Private Sub spStartup_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles spStartup.CheckedChanged
if spstartup then
my.settings.spStartup = true 'run singleplayer on startup
my.settings.save
end if
End sub

Private Sub mpStartup_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mpStartup.CheckedChanged
if spstartup then
my.settings.mpStartup = true 'run multiplayer on startup
my.settings.save
end if
End sub

#End region


[align=center][size=large]Good Luck Smile, i hope you will like IT.![/size][/align]
HakerDz
[/QUOTE]
source
You must login or register to view this content.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo