Difference between revisions of "Visual Basic .net"

From ScienceZero
Jump to: navigation, search
m
Line 27: Line 27:
 
Print(f1, "This is line 2")<br />
 
Print(f1, "This is line 2")<br />
 
FileClose(f1)<br />
 
FileClose(f1)<br />
 +
</code>
 +
 +
 +
'''loading data files into an array of bytes'''<br />
 +
<code>
 +
Dim oFile As System.IO.FileInfo<br />
 +
oFile = New System.IO.FileInfo(My.Application.Info.DirectoryPath & "\file.dat")<br />
 +
 +
Dim oFileStream As System.IO.FileStream = oFile.OpenRead()<br />
 +
Dim lBytes As Long = oFileStream.Length<br />
 +
 +
If (lBytes > 0) Then<br />
 +
ReDim fileData(lBytes - 1)<br />
 +
:oFileStream.Read(fileData, 0, lBytes)<br />
 +
:oFileStream.Close()<br />
 +
End If<br />
 
</code>
 
</code>
  

Revision as of 09:29, 27 December 2009

Graphics

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

Loops

Conditionals

Math

RS-232

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)
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
Long System.Int64 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
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
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