18

Visual basic deo 5

Embed Size (px)

Citation preview

Page 1: Visual basic   deo 5
Page 2: Visual basic   deo 5

Programske petlje

U VB-u postoje četiri tipa programskih petlji:

Do...Loop: Petlja se izvršava sve dok je (while) ili dok nije (until) uslov ispunjen (True).

While...Wend: Petlja se izvršava sve dok je (while) uslov ispunjen (True).

For...Next: Koristi brojač da izvršava naredbe specificirani broj puta.

For Each...Next: Ponavlja grupu naredbi za svaku stavku u kolekciji ili za svaki element u nizu.

Page 3: Visual basic   deo 5

Do...Loop Programske petlje Sintaksa:

Do [{While | Until} uslov]    [naredbe]    [Exit Do]    [naredbe]Loop

Ili se koristi ispitivanje uslova na karaju petlje:

Do    [naredbe]    [Exit Do]    [naredbe]Loop [{While | Until} uslov]

Page 4: Visual basic   deo 5

Primeri Do...Loop petlje

Do Until DefResp = vbNo MyNum = Int (6 * Rnd + 1) ' Generiše slučajan broj između 1 i 6. DefResp = MsgBox (MyNum & " Da li zelite drugi broj?", vbYesNo)Loop_______________________________________________________________

Dim Check, CounterCheck = True: Counter = 0 ' Inicijalizuje varijable.Do ' Spoljna petlja. Do While Counter < 20 ' Unutrašnja petlja. Counter = Counter + 1 ' Inkrementira Counter. If Counter = 10 Then ' Ako je Counter=10... Check = False ' postavlja vrednost zastavice na False. Exit Do ' Prinudni izlazak (Exit) iz unutrašnje petlje. End If LoopLoop Until Check = False ' Prinudni izlazak (Exit) iz spoljne petlje.

Page 5: Visual basic   deo 5

While...Wend Programske petlje Sintaksa:

While uslov   [naredbe]Wend

Primer:

Dim CounterCounter = 0 ' Inicijalizuje varijable.While Counter < 20 ' Ispituje vrednost varijable Counter. Counter = Counter + 1 ' Inkrementira Counter. Alert CounterWend ' Kraj While petlje kada je Counter >19.

Page 6: Visual basic   deo 5

For...Next Programske petlje Sintaksa:

For brojac = start To kraj [Step korak]    [naredbe]    [Exit For]    [naredbe]Next

Primer:For I = 1 To 10 For J = 1 To 10 For K = 1 To 10 . . . Next K Next JNext I

Page 7: Visual basic   deo 5

For Each...Next Programske petlje

Sintaksa:

For Each element In grupa

    [naredbe]

    [Exit For]

    [naredbe]

Next [element]

Page 8: Visual basic   deo 5

Primer For Each...Next petlje

Dim fso, f, f1, fc, s

Set fso = CreateObject("Scripting.FileSystemObject")

Set f = fso.GetFolder(folderspec)

Set fc = f.Files

For Each f1 in fc

s = s & f1.name

s = s & vbCrLf

Next

ShowFolderList = s

Page 9: Visual basic   deo 5

Programske procedure

U VB-u postoje dva tipa programskih procedura:

Sub procedure: Predstavlja niz naredbi koje počinju naredbom Sub i završavaju sa End Sub.

Function procedure: Predstavlja niz naredbi koje počinju naredbom Function i završavaju sa End Function.

Page 10: Visual basic   deo 5

Sub Programska procedura

Sintaksa:

[Public [Default]| Private] Sub naziv [(arglist)]

    [naredbe]

    [Exit Sub]

    [naredbe]

End Sub

Sintaksa arglist-e:

[ByVal | ByRef] varname[( )]

Page 11: Visual basic   deo 5

Function Programska procedura Sintaksa:

[Public [Default] | Private] Function naziv [(arglist)]

    [naredbe]

    [naziv = izraz]

    [Exit Function]

    [naredbe]

    [naziv = izraz]

End Function

Sintaksa arglist-e:

[ByVal | ByRef] varname[( )]

Page 12: Visual basic   deo 5

Primer Function procedure

Function BinarySearch(. . .)

. . .

' Value not found. Return a value of False.

If lower > upper Then

BinarySearch = False

Exit Function

End If

. . .

End Function

Page 13: Visual basic   deo 5

Primer Sub i Function procedura

Sub ConvertTemp()

temp = InputBox("Please enter the temperature in degrees F.", 1)

MsgBox "The temperature is " & Celsius(temp) & " degrees C."

End Sub

Function Celsius(fDegrees)

Celsius = (fDegrees - 32) * 5 / 9

End Function

Napomena:

Call MyProc(firstarg, secondarg)

MyProc firstarg, secondarg

Page 14: Visual basic   deo 5

Konvencije o načinu pisanja kôda

Konvencije se odnose na:

imenovanje objekata, varijabli i procedura

pisanje komentara i

formatiranje teksta i indentiranje (uvlačenje redova)

Page 15: Visual basic   deo 5

Konvencije o imenovanju konstanti i varijabli

Konstante: USER_LIST_MAX ili conLicnoVasaKonstanta NEW_LINE

Varijable:Podtip Prefix Primer

Boolean bln blnFound

Byte byt bytRasterData

Date (Time) dtm dtmStart

Double dbl dblTolerance

Error err errOrderNum

Integer int intQuantity

Long lng lngDistance

Object obj objCurrent

Single sng sngAverage

String str strFirstName

Page 16: Visual basic   deo 5

Konvencije o imenovanju varijabli i njihova vidljivost

Scope Where Variable Is Declared

Visibility

Procedure-level Event, Function, or Sub procedure

Visible in the procedure in which it is declared

Script-level HEAD section of an HTML page, outside any procedure

Visible in every procedure in the script

Scope Prefix Example

Procedure-level None dblVelocity

Script-level s sblnCalcInProgress

Page 17: Visual basic   deo 5

Konvencije o imenovanju objekataTip objekta Prefix Primer

3D Panel pnl pnlGroup

Animated button ani aniMailBox

Check box chk chkReadOnly

Combo box, drop-down list box cbo cboEnglish

Command button cmd cmdExit

Common dialog dlg dlgFileOpen

Frame fra fraLanguage

Horizontal scroll bar hsb hsbVolume

Image img imgIcon

Label lbl lblHelpMessage

Line lin linVertical

List Box lst lstPolicyCodes

Spin spn spnPages

Text box txt txtLastName

Vertical scroll bar vsb vsbRate

Slider sld sldScale

Page 18: Visual basic   deo 5

Konvencije o komentarima i uvlačenju teksta'********************************************************* ' Purpose: Locates the first occurrence of a specified user ' in the UserList array. ' Inputs: strUserList(): the list of users to be searched. ' strTargetUser: the name of the user to search for. ' Returns: The index of the first occurrence of the strTargetUser ' in the strUserList array. ' If the target user is not found, return -1. '*********************************************************

Function intFindUser (strUserList(), strTargetUser) Dim i ' Loop counter. Dim blnFound ' Target found flag intFindUser = -1 i = 0 ' Initialize loop counter Do While i <= Ubound(strUserList) and Not blnFound If strUserList(i) = strTargetUser Then blnFound = True ' Set flag to True intFindUser = i ' Set return value to loop count End If i = i + 1 ' Increment loop counter Loop End Function