Senin, 01 Juli 2013

Contoh Program VB.Net

Codingan 1
Create directory
Imports System.IO
Module Module1
Sub Main()
Console.WriteLine("Creating directories...")
Directory.CreateDirectory("D:\test01")
Directory.CreateDirectory("D:\Temp\Sample02")
Directory.CreateDirectory("Sample03")
Console.WriteLine("Directories created")
End Sub
End Module



Codingan 2

Imports System.IO
Module Module1
Sub Main()
Try
Directory.Delete("C:\Sample01", True)
Catch E As Exception
Console.WriteLine("Error deleting directory C:\Sample01")
Console.WriteLine("Error (0)", E.Message)
End Try
End Sub
End Module



Codingan 3

Imports System.IO
Module Module1
Sub Main()
Try
If (Directory.Exists("D:\test01")) Then
Console.WriteLine("D:\test01 already exists")
Else
Directory.CreateDirectory("C:\test01")
End If
Console.WriteLine("Directories created")
Catch E As Exception
Console.WriteLine("Error creating directory")
Console.WriteLine("Error: (0)", E.Message)
End Try
End Sub
End Module
 



Codingan 4

Imports System.IO
Module Module1
Sub Main()
Dim Root As New DirectoryInfo("C:\")
Dim Files As FileInfo() = Root.GetFiles("*.*")
Dim Dirs As DirectoryInfo() = Root.GetDirectories("*.*")
Console.WriteLine("Root Files")
Dim Filename As FileInfo
For Each Filename In Files
Try
Console.Write(Filename.FullName)
Console.Write(" Size: (0) bytes", Filename.Length)
Console.WriteLine(" Last use: (0)", Filename.LastAccessTime)
Catch E As Exception
Console.WriteLine("Error accessing File")
End Try
Next
End Sub
End Module
 



Codingan 5
 

Contoh Unconditional Branching

Option Strict On
Imports System
Module Module1
Sub Main()
Console.WriteLine("Ini method utamanya,,") SomeMethod()
Console.WriteLine("Kembali ke method utama...")
Console.ReadLine()
End Sub
Sub SomeMethod()
Console.WriteLine("Sekarang tampil deh method percabangannya!!")
End Sub
End Module

 

Codingan 6
 

Contoh If Statement

Module Module1
Sub Main()
Dim nilai1 As Integer = 10
Dim nilai2 As Integer = 20
Dim maxValue As Integer

maxValue = Cint(IIf((nilai1 > nilai2), nilai1, nilai2))

Console.WriteLine("Nilai pertama: (0), Nilai kedua (1),
maxValue: (2)", nilai1, nilai2, maxValue)
Console.ReadLine()
End Sub
End Module
 



Codingan 7

Module Module1
Dim kal As String
Dim a() As Char
Dim x, i, k As Integer
Dim hrf(20) As Char
Dim bnyk(20) As Byte
Sub Main()
Dim m As Byte
Console.Write("masukan kalimat : ")
kal = Console.ReadLine
x = kal.Length()
ReDim a(x + 1)
For i = 0 To x
a(i) = Mid(kal, i + 1, 1)
Console.Write(a(i))
Next i
urut(a)
Console.WriteLine("data yang telah urut")
For i = 0 To x
Console.WriteLine(a(i))
Next
Console.WriteLine()
Console.WriteLine()
HitungKata(a)
For i = 0 To k - 1
If (hrf(i) = " ") Then
Console.WriteLine("spasi banyaknya = (0)", bnyk(i))
Else
Console.WriteLine("huruf (0) banyaknya = (1)", hrf(i), bnyk(i))
End If
Next
Console.ReadLine()
End Sub
Sub urut(ByRef a() As Char)
Dim i, j As Byte
Dim temp As Char
Console.WriteLine("banyak huruf : (0)", x)
For i = 0 To x
For j = i + 1 To x
If (a(i) > a(j)) Then
temp = a(j)
a(j) = a(i)
a(i) = temp
End If
Next
Next
End Sub
Sub HitungKata(ByVal a() As Char)
i = 1
Dim m As Byte
k = 0
m = 0
While (i < x + 1)
bnyk(k) = 1
hrf(k) = a(i)
While (a(i) = a(i + 1))
bnyk(k) += 1
i += 1
End While
k += 1
i += 1
End While

End Sub

End Module
 




Codingan 8

Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Collections.Specialized

Public Class MainClass

Shared Sub Main(ByVal args As String())
Dim dict As New StudentListDictionary
dict.Add("111-11-1111", New Student("A", "A"))
dict.Add("333-22-3333", New Student("B", "B"))
dict.Add("444-44-4444", New Student("C", "C"))
dict.Add("555-55-5555", New Student("D", "D"))

Dim student As Student = dict.Item("111-11-1111")
Console.WriteLine(student.ToString)
Console.ReadLine()

End Sub
End Class

Public Class Student
Private m_FirstName As String
Private m_LastName As String
Public Sub New(ByVal first_name As String, ByVal last_name As String)
m_FirstName = first_name
m_LastName = last_name
End Sub
Public Overrides Function ToString() As String
Return m_FirstName & " " & m_LastName
End Function
End Class

Public Class StudentListDictionary
Inherits ListDictionary

'Add a Dictionary entry.
Public Shadows Sub Add(ByVal new_key As String, ByVal new_student As Student)
MyBase.Add(new_key, new_student)
End Sub

'Return an object with the given key.
Default Public Shadows Property Item(ByVal key As String) As Student
Get
Return DirectCast(MyBase.Item(key), Student)
End Get
Set(ByVal Value As Student)
MyBase.Item(key) = Value
End Set
End Property
'Return True if the Dictionary contains this Student.
Public Shadows Function Contain(ByVal key As String) As Boolean
Return MyBase.Contains(key)
End Function
'Remove this entry.
Public Shadows Sub Remove(ByVal key As String)
MyBase.Remove(key)
End Sub
End Class
 

2 komentar:

  1. Yuk Merapat Best Betting Online Hanya Di AREATOTO
    Dalam 1 Userid Dapat Bermain Semua Permainan
    Yang Ada :
    TARUHAN BOLA - LIVE CASINO - SABUNG AYAM - TOGEL ONLINE ( Tanpa Batas Invest )
    Sekedar Nonton Bola ,
    Jika Tidak Pasang Taruhan , Mana Seru , Pasangkan Taruhan Anda Di areatoto
    Minimal Deposit Rp 20.000 Dan Withdraw Rp.50.000
    Proses Deposit Dan Withdraw ( EXPRES ) Super Cepat
    Anda Akan Di Layani Dengan Customer Service Yang Ramah
    Website Online 24Jam/Setiap Hariny

    BalasHapus