Console.WriteLine("Hello, world!")
Console.Write("Test")
str = Console.ReadLine()
a = 123 : b = 456
' コメント
Dim name As String
Dim name1, name2 As String
Dim number As Integer = 123
Dim name As String = "Test"
- Boolean
- SByte (1byte : Int8)
- Short (2byte : Int16)
- Integer (4byte : Int32)
- Long (8byte : Int64)
- Single (4byte : Float32)
- Double (8byte : Float64)
- String
- Enum
- Tuple
- CByte
- CShort
- CInt
- CLng (Long)
- CSng (Single)
- CDbl (Double)
- CDec (Decimal)
- CChar
- CStr (String)
- CBool (Boolean)
- CDate
- CObj (Object)
Module Module1
Public Const MAX_VALUE = 100
End Module
Enum RGB
Red
Green
Blue
End Enum
Dim rgb As RGB = RGB.Red
Dim t As Tuple(Of String, Integer)
t = Tuple.Create("Apple", 12)
t.Item1
t.Item2
^
: 冪乗*
: 乗算/
: 除算(float)\
: 除算(int)Mod
: 剰余+
: 加算-
: 減算^=
,*=
,/=
,+=
,-=
: 代入演算子<<
,>>
: ビットシフト演算子
=
: 等しいか<>
: 異なるか>
,>=
,<
,<=
: 大なり小なり
- And
- Or
- Not
- Xor
- AndAlso : 短絡評価するAnd
- OrElse : 短絡評価するOr
+
: 数値同士では加算、文字列同士では結合&
: 文字列同士の結合 (String限定)
name = "Alice"
msg = $"Hello, {name}!"
- NameOf 演算子 :
NameOf(x)
- Null 条件演算子 :
x?.ToLower()
If cond Then
process1
ElseIf cond2 Then
process2
Else
process3
End If
Select Case expression
Case value1
process1
Case value2, value3, value4
process2
Case minValue To maxValue
process3
Case Else
process4
End Select
Select Case X
Case Is < 5
process1
Case Is < 10
process2
Case Else
process3
End Select
For i As Integer = 0 To 5
process
Next
Dim names() As String = {"foo", "bar", "baz"}
For Each name As String In names
process
Next
While cond
process
End While
Do While cond
process
Loop
Do
process
Loop While cond
Do Until cond
process
Loop
Do
process
Loop Until cond
Exit For
Exit While
Exit Do
Continue For
Continue While
Continue Do
Dim names() As String = {"spam", "ham", "egg"}
names(0) = "foobar"
Console.WriteLine(names(1))
ReDim names(10) '格納データは削除してリサイズする
ReDim Preserve names(10) '格納データを保持したままリサイズする
Dim matrix(5, 5) As Integer
Dim names(,) As String = {{"A", "B"}, {"C", "D"}}
Dim names As String()() = New String(2)() {}
names(0) = New String() {"A", "B", "C"}
names(1) = New String() {"D", "E"}
names(2) = New String() {"F", "G", "H", "I"}
Class クラス名
Private フィールド名 As 型
Public フィールド名 As 型
'自動実装プロパティ
Public Property Name As String
'プロパティ
Private _version As Integer
Public Property Version() As Integer
Get
Return _version
End Get
Set(value As Integer)
If value >= 1 And value <= 9 Then
_version = value
Else
_version = 0
End If
End Set
End Property
Public Sub メソッド名()
...
End Sub
Public Function メソッド名(a As Integer) As Integer
メソッド名 = a + 1
End Function
End Class
Dim インスタンス名 As クラス名
インスタンス名 = New クラス名()
Dim インスタンス名 As New クラス名()
List
Dim fruit As New List(Of String)
fruit.Add("Apple")
fruit.Add({"Banana", "Cherry"})
fruit.Insert(1, "Watermelon")
Console.WriteLine(fruit(0))
- SortedList
- Dictionary
Dim fruit As New SortedList(Of Integer, String)
fruit.Add(1, "Apple")
fruit.Add(3, "Cherry")
fruit.Add(2, "Banana")
'要素の取得
Dim tmp As String = String.Empty
fruit.TryGetValue(2, tmp)
Console.WriteLine(tmp) 'Banana
'全要素の表示
For Each data As KeyValuePair(Of Integer, String) In fruit
Console.WriteLine($"{data.Key} = {data.Value}")
Next
ジェネリック型
Public Class Sample(Of T)
...
End Class
Public Sub Swap(Of T)(ByRef x As T, ByRef y As T)
Dim tmp As T = x
x = y
y = tmp
End Sub
Imports System.Linq
Dim numbers() As Integer = {3,1,4,1,5,9,2,6,5,3}
Dim data = From num In numbers
Where num > 4
Select num
For Each num As Integer In data
Console.WriteLine(num)
Next
imports System
imports System.Collections.Generic
Imports System.Linq
Public Class Customer
Public Property id As Integer
Public Property name As String
End Class
Public Class Sales
Public Property no As Integer
Public Property customerID AS Integer
Public Property price As Integer
Public Property YMD As Date
End Class
Module Sample
Private customerList As List(Of Customer)
Private salesList As List(Of Sales)
Public Function GetCustomerList() As List(Of Customer)
customerList = New List(Of Customer)
customerList.Add(New Customer With {.id = 1, .name = "Alice"})
customerList.Add(New Customer With {.id = 2, .name = "Bob"})
Return customerList
End Function
Public Function GetSalesList() As List(Of Sales)
salesList = New List(Of Sales)
salesList.Add(New Sales With {.no = 1, .customerID = 1, .price = 1000, .YMD = CDate("2020/4/1")})
salesList.Add(New Sales With {.no = 2, .customerID = 2, .price = 1100, .YMD = CDate("2020/4/2")})
salesList.Add(New Sales With {.no = 3, .customerID = 1, .price = 1300, .YMD = CDate("2020/4/3")})
salesList.Add(New Sales With {.no = 4, .customerID = 2, .price = 1200, .YMD = CDate("2020/4/3")})
Return salesList
End Function
End Module
Public Class Application
Public Shared Sub Main()
Dim customers As List(Of Customer) = Sample.GetCustomerList()
Dim sales As List(Of Sales) = Sample.GetSalesList()
Dim records =
From customer In customers
Join profit In sales On customer.id Equals profit.customerID
Order By profit.no
Select profit.no, customer.name, profit.price, profit.YMD
For Each res As Object In records
Console.WriteLine($"{res.no}, {res.name}, {res.price}, {res.YMD}")
Next
End Sub
End Class
Dim sales As List(Of Sales) = Sample.GetSalesList()
Dim res = Aggregate sale In sales
Into Sum(sale.price), Average(sale.price), Max(sale.price), Min(sale.price)
Console.WriteLine($"sum: {res.Sum}, average: {res.Average}, max: {res.Max}, min: {res.Min}")
Public Class EventSample
Public Event OnDataChanged()
Public Sub Fire()
RaiseEvent OnDataChanged() 'イベント発火
End Sub
End Class
Module Sample
WithEvents EventSampleClass As New EventSample
Sub DataChanged() Handles EventSampleClass.OnDataChanged
Console.WriteLine("データが変更されました")
End Sub
End Module
Try
Dim x As Integer = CInt("abc")
Catch [ex As 例外クラス]
Console.WriteLine("Error!")
Finally
Console.WriteLine("Finally!")
End Try
- System.Exception
- ArgumentException
- OverflowException
- DivideByZeroException
- IndexOutOfRangeException
- OutOfMemoryException
- DirectoryNotFoundException
- FileNotFoundException
例外クラスで得られる情報
- ex.InnerException : 例外クラスのインスタンス
- ex.Message : 例外メッセージ
- ex.StackTrace : スタックトレース
例外の発生
Throw New 例外クラス([引数])