Difference between revisions of "Visual Basic .net"
Line 25: | Line 25: | ||
PictureBox1.Image = bm | PictureBox1.Image = bm | ||
</code> | </code> | ||
− | |||
Color.FromArgb(255, 255, 0, 0) is in the format of Alpha, Red, Green, Blue<br /> | Color.FromArgb(255, 255, 0, 0) is in the format of Alpha, Red, Green, Blue<br /> | ||
Line 31: | Line 30: | ||
==Files== | ==Files== | ||
− | '''Reading text files line by line | + | '''Reading text files line by line '''<br /> |
<code> | <code> | ||
Dim f1 As Long | Dim f1 As Long | ||
Line 38: | Line 37: | ||
FileOpen(f1, My.Application.Info.DirectoryPath & "\filename.txt", OpenMode.Input) | FileOpen(f1, My.Application.Info.DirectoryPath & "\filename.txt", OpenMode.Input) | ||
Do Until EOF(f1) | Do Until EOF(f1) | ||
− | + | filestring=LineInput(f1) | |
Loop | Loop | ||
FileClose(f1) | FileClose(f1) | ||
</code> | </code> | ||
− | + | Note My.Application.Info.DirectoryPath & "\filename.txt" may be replaced by "C \some_path\file.txt"<br /> | |
− | Note | + | |
− | '''Writing text files line by line | + | '''Writing text files line by line '''<br /> |
<code> | <code> | ||
Dim f1 As Long | Dim f1 As Long | ||
Line 55: | Line 53: | ||
FileClose(f1) | FileClose(f1) | ||
</code> | </code> | ||
− | |||
Line 65: | Line 62: | ||
Dim lBytes As Long = oFileStream.Length | Dim lBytes As Long = oFileStream.Length | ||
If (lBytes > 0) Then | If (lBytes > 0) Then | ||
− | + | ReDim fileData(lBytes - 1) | |
− | + | oFileStream.Read(fileData, 0, lBytes) | |
− | + | oFileStream.Close() | |
End If | End If | ||
</code> | </code> | ||
− | |||
==Arrays== | ==Arrays== | ||
Line 82: | Line 78: | ||
Parallel.For(0, yRes - 1, Function(n) mandelbrotline(n, cr + xr, ci + yr, mandN, xRes, xStp, yStp, picarray)) | Parallel.For(0, yRes - 1, Function(n) mandelbrotline(n, cr + xr, ci + yr, mandN, xRes, xStp, yStp, picarray)) | ||
</code> | </code> | ||
− | |||
n will iterate from 0 to yres -1 in no particular order<br /> | n will iterate from 0 to yres -1 in no particular order<br /> | ||
Requires .net 4 or Parallel extensions on .net 3.5<br /> | Requires .net 4 or Parallel extensions on .net 3.5<br /> | ||
Line 103: | Line 98: | ||
msgbox(CStr(CInt(1 / ((tStop - tStart) / curFreq))) & " Frames per second") | msgbox(CStr(CInt(1 / ((tStop - tStart) / curFreq))) & " Frames per second") | ||
</code> | </code> | ||
− | |||
==RS-232== | ==RS-232== | ||
Line 110: | Line 104: | ||
SerialPort1.Write(bytearray, 0, arraylength) | SerialPort1.Write(bytearray, 0, arraylength) | ||
</code> | </code> | ||
− | |||
0 indicated the index to start reading the array, arraylength is a variable containing the number of bytes to transmit.<br /> | 0 indicated the index to start reading the array, arraylength is a variable containing the number of bytes to transmit.<br /> | ||
This operation is incredibly fast compared to looping through the array and sending 1 byte at a time. This is especially useful for high speed serial ports. <br /> | This operation is incredibly fast compared to looping through the array and sending 1 byte at a time. This is especially useful for high speed serial ports. <br /> | ||
==Strings== | ==Strings== | ||
− | '''Finding text in a string | + | '''Finding text in a string '''<br /> |
<code> | <code> | ||
InStr("this is a string", "is") | InStr("this is a string", "is") | ||
</code> | </code> | ||
− | |||
returns (character position number) 6. This function returns 0 when not found.<br /> | returns (character position number) 6. This function returns 0 when not found.<br /> | ||
− | '''Using the start of a string | + | '''Using the start of a string '''<br /> |
<code> | <code> | ||
dim str as string | dim str as string | ||
Line 130: | Line 122: | ||
outputstr=str.Substring(0, 4) | outputstr=str.Substring(0, 4) | ||
</code> | </code> | ||
− | |||
Starts at character index 0 (the first character), and reads 4 characters (including the first one), resulting in "this".<br /> | Starts at character index 0 (the first character), and reads 4 characters (including the first one), resulting in "this".<br /> | ||
− | '''Using the middle of a string | + | '''Using the middle of a string '''<br /> |
<code> | <code> | ||
dim str as string | dim str as string | ||
Line 141: | Line 132: | ||
outputstr=str.Substring(5, 4) | outputstr=str.Substring(5, 4) | ||
</code> | </code> | ||
− | |||
Starts at character index 5 (the 6th character), and reads 4 characters, resulting in "is a".<br /> | Starts at character index 5 (the 6th character), and reads 4 characters, resulting in "is a".<br /> | ||
− | '''Using the end of a string | + | '''Using the end of a string '''<br /> |
<code> | <code> | ||
dim str as string | dim str as string | ||
Line 152: | Line 142: | ||
outputstr=str1.Substring(str1.Length - 4, 4) | outputstr=str1.Substring(str1.Length - 4, 4) | ||
</code> | </code> | ||
− | |||
Starts 4 chacters before the end, and reads 4 characters, resulting in "ring".<br /> | Starts 4 chacters before the end, and reads 4 characters, resulting in "ring".<br /> | ||
Revision as of 10:11, 27 December 2009
Contents
Graphics
setting up graphics and drawing pixels, shapes, and colours
Dim bm As New Bitmap(PictureBox1.Width, PictureBox1.Height) Dim gr As Graphics = Graphics.FromImage(bm) bm.SetPixel(10, 10, Color.FromArgb(255, 255, 0, 0)) Dim greenthickpen As New Pen(Color.FromArgb(255, 0, 255, 0), 10) gr.DrawLine(greenthickpen, 20, 20, 40, 40) gr.DrawArc(Pens.Blue, 80, 80, 20, 20, 0, 360) gr.FillPie(Brushes.Black, 150, 150, 20, 20, 0, 360) Dim rect As Rectangle rect.Width = 10 rect.Height = 10 rect.X = 200 rect.Y = 200 gr.DrawRectangle(Pens.Black, rect) Dim triangle(2) As Point triangle(0).X = 5 triangle(0).Y = 0 + 30 triangle(1).X = 0 triangle(1).Y = 5 + 30 triangle(2).X = 10 triangle(2).Y = 5 + 30 gr.FillPolygon(Brushes.Blue, triangle) PictureBox1.Image = bm
Color.FromArgb(255, 255, 0, 0) is in the format of Alpha, Red, Green, Blue
Files
Reading text files line by line
Dim f1 As Long dim filestring as string f1 = FreeFile() FileOpen(f1, My.Application.Info.DirectoryPath & "\filename.txt", OpenMode.Input) Do Until EOF(f1) filestring=LineInput(f1) Loop FileClose(f1)
Note My.Application.Info.DirectoryPath & "\filename.txt" may be replaced by "C \some_path\file.txt"
Writing text files line by line
Dim f1 As Long f1 = FreeFile() FileOpen(f1, My.Application.Info.DirectoryPath & "\filename.txt", OpenMode.Output Print(f1, "This is line 1") Print(f1, "This is line 2") FileClose(f1)
loading data files into an array of bytes
Dim oFile As System.IO.FileInfo oFile = New System.IO.FileInfo(My.Application.Info.DirectoryPath & "\file.dat") Dim oFileStream As System.IO.FileStream = oFile.OpenRead() Dim lBytes As Long = oFileStream.Length If (lBytes > 0) Then ReDim fileData(lBytes - 1) oFileStream.Read(fileData, 0, lBytes) oFileStream.Close() End If
Arrays
Threading / Parallel
Distribute tasks automatically on all available cores
Imports System.Threading Imports System.Threading.Tasks
Parallel.For(0, yRes - 1, Function(n) mandelbrotline(n, cr + xr, ci + yr, mandN, xRes, xStp, yStp, picarray))
n will iterate from 0 to yres -1 in no particular order
Requires .net 4 or Parallel extensions on .net 3.5
Loops
Conditionals
Math
Calling Windows API
Public Declare Function QueryPerformanceCounter Lib "kernel32.dll" (ByRef Counter As Long) As Integer Public Declare Function QueryPerformanceFrequency Lib "kernel32.dll" (ByRef counter As Long) As Integer
Dim curFreq, tStart, tStop As UInt64 QueryPerformanceFrequency(curFreq) QueryPerformanceCounter(tStart) ...your code QueryPerformanceCounter(tStop) msgbox(CStr(CInt(1 / ((tStop - tStart) / curFreq))) & " Frames per second")
RS-232
sending an entire array of bytes
SerialPort1.Write(bytearray, 0, arraylength)
0 indicated the index to start reading the array, arraylength is a variable containing the number of bytes to transmit.
This operation is incredibly fast compared to looping through the array and sending 1 byte at a time. This is especially useful for high speed serial ports.
Strings
Finding text in a string
InStr("this is a string", "is")
returns (character position number) 6. This function returns 0 when not found.
Using the start of a string
dim str as string dim outputstr as string str="this is a string" outputstr=str.Substring(0, 4)
Starts at character index 0 (the first character), and reads 4 characters (including the first one), resulting in "this".
Using the middle of a string
dim str as string dim outputstr as string str="this is a string" outputstr=str.Substring(5, 4)
Starts at character index 5 (the 6th character), and reads 4 characters, resulting in "is a".
Using the end of a string
dim str as string dim outputstr as string str="this is a string" outputstr=str1.Substring(str1.Length - 4, 4)
Starts 4 chacters before the end, and reads 4 characters, resulting in "ring".
Datatypes
VB type | .net type | Size | Range |
---|---|---|---|
Boolean
|
System.Boolean
|
4 bytes | True or False |
Byte
|
System.Byte
|
1 byte | 0 to 255 (unsigned) |
SByte
|
System.SByte
|
1 byte | -128 through 127 (signed) |
Char
|
System.Char
|
2 bytes | 0 to 65535 (unsigned) |
Date
|
System.DateTime
|
8 bytes | January 1, 1 CE to December 31, 9999 |
Decimal
|
System.Decimal
|
12 bytes | +/-79,228,162,514,264,337,593,543,950,335 with no decimal point; +/-7.9228162514264337593543950335 with 28 places to the right of the decimal; smallest non-zero number is +/-0.0000000000000000000000000001 |
Double
|
System.Double
|
8 bytes | -1.79769313486231E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values |
Integer
|
System.Int32
|
4 bytes | -2,147,483,648 to 2,147,483,647 |
UInteger
|
System.UInt32
|
4 bytes | 0 through 4,294,967,295 (unsigned) |
Long
|
System.Int64
|
8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
ULong
|
System.UInt64
|
8 bytes | 0 through 18,446,744,073,709,551,615 (1.8...E+19) (unsigned) |
Object
|
System.Object (class)
|
4 bytes | Any type can be stored in a variable of type Object
|
Short
|
System.Int16
|
2 bytes | -32,768 to 32,767 |
UShort
|
System.UInt16
|
2 bytes | 0 through 65,535 (unsigned) |
Single
|
System.Single
|
4 bytes | -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values |
String
|
System.String (class)
|
10 bytes + (2 * string length) | 0 to approximately two billion Unicode characters |
User-Defined Type (structure) | (inherits from System.ValueType )
|
Sum of the sizes of its members | Each member of the structure has a range determined by its data type and independent of the ranges of the other members |