Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[TUT]Improve your Coding[VB]
#1
Improve your Coding


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

Rename your controls


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.

Comment your code

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

Use your datatypes correctly
Code:
Dim int as integer = "5"
Dim str as string = "Sample Text"

txtResult.text = int

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

Shorten your code

Instead of writing:
Code:
txtResult.text = txtResult.text + int
You better use:
Code:
txtResult.text += int

Using
Code:
Dim str as IO.Streamreader = New Streamreader("C:\test.txt")

txtResult.text = str.readtoend

str.dispose

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

txtResult.text = str.readtoend

End Using

Case instead of If

Code:
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

Code:
Select Case txtresult.Text

Case "a"
Msgbox("Newb")

Case "b"
Msgbox("Newbie")

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

End Select

If-Structures

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

Code:
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...

Code:
Select Case Textbox1.Text
Case "meh"
'do something
Case "Duh"
'do something else
Case Else
'something
end select
Code:
If Textbox1.text <> "" then 'if it's not empty
end if

If not...

Code:
If not textbox1.text = "" then
end if

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

Structure

Code:
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

Code:
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

Booleans
Code:
Dim b as boolean 'by default the boolean is FALSE
Code:
If b = true then
'do something
end if

If b = false Then
'do something
end if

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

Try-Catch
To avoid your program from closing when an error appears, you can use Try-Catch Blocks.
Code:
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.
Code:
Try
Timer.Value = nudValue.Value 'NumericUpDown
Catch ex as exception
end try

Regions
You can use Regions to make your code more clear.
Code:
#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

Good Luck Smile, i hope you will like IT.!
HakerDz
Reply
#2
That was helpful. I'm learning VB currently, will keep this is in mind.
Thanks for sharing btw.
Reply
#3
While I don't understand much about code, I learned a little bit from this. Thank you.
"Why judge a life you can't change?"
You are an important person who deserves to be happy.
Reply
#4
Thanks a lot for this, I am not the best VB coder but I've totally realized how important it is to stay organised and clean whilst coding simple programs. Thanks a lot for the share, I've bookmarked this so when I am next making a application I'll revert back to this before finishing it to clean the code up a little. Big Grin
Reply
#5
Not bad and everything seems correct. Nice post.
I like to use if's rather than case. Big Grin
[Image: burninglove4.png]
Reply
#6
Yeh I tend to use If more than Case aswell.

Other than that I do everything you mentioned here. Good post.
It can make so much difference cleaning up your code, not only performance wise, but also just so that you know where things are, and what they do. You can end up confusing yourself if you have lots of code.

What I tend to do is work step by step, and then I'll group various parts together using Regions.
For example if I had a few functions, I'd group them all together into a Region named "Functions", same for Timers, Declarations etc..
Reply
#7
I didn't really get the code. This helped me a bit but I do not understand the code.
Reply
#8
Thank you, very useful!
Reply
#9
Some of the basic codes, but helpful. Thanks for sharing. ^_^
Reply
#10
welcom Guys Smile, Thank's for The Feedbacks.
[Image: skyk.png]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [TUT] How to make a simple WebBrowser in VB 2010! [TUT] - [ Pictures] Statics 95 61,754 10-07-2012, 06:56 AM
Last Post: a99
  [TUT]Auto-Update System[TUT] HB Virus 3 2,425 01-07-2012, 02:21 PM
Last Post: Mastermrz
  Help with coding jerrojack 1 877 12-29-2011, 03:27 AM
Last Post: AceInfinity
  [TUT]Enable and Disable TaskManger in vb.net [TUT] HB Virus 4 3,163 12-19-2011, 10:10 AM
Last Post: euverve
  Coding-Central.net - Coding, cracking and webmastering. w00pz 5 2,430 09-02-2011, 01:43 PM
Last Post: AceInfinity

Forum Jump:


Users browsing this thread: 1 Guest(s)