Visual Basic 学习笔记

CInt

CInt() 类型转换函数,将数据转换成它最接近的整数类型。

MyNumber = CInt(99.8)    ' Returns 100.
MyNumber = CInt(-99.8)   ' Returns -100.
MyNumber = CInt(-99.2)   ' Returns -99.

Val

val() 类型转换函数 将字符串中包含的数字作为适当类型的数值返回

public static double Val (object? Expression);

Val 在第一个不能解释为数字、数字修饰符、数字标点符号或空格的字符处停止转换。

Dim valResult As Double
' The following line of code sets valResult to 2457.
valResult = Val("2457")
' The following line of code sets valResult to 2457.
valResult = Val(" 2 45 7")
' The following line of code sets valResult to 24.
valResult = Val("24 and 57")

Input

从打开的顺序文件中读取数据,并将数据赋值给变量。

public static void Input (int FileNumber, ref object Value);
Imports System
Imports System.IO
Imports VB = Microsoft.VisualBasic
Imports Microsoft.Office.Interop.Access
Imports System.Linq
Imports System.Collections.Generic

Module Program
    Sub Main(args As String())
        Dim s As String = Nothing
        Dim s2 As String = Nothing
        Dim s3 As String = Nothing
        Dim s4 As String = Nothing
        Dim path1 As String = "C:\Study\VBStudy\VBStudy\bin\dt1.txt"
        Dim path2 As String = "C:\Study\VBStudy\VBStudy\bin\dt2.txt"


        FileOpen(1, path1, OpenMode.Input) 'open dt1.txt
        FileOpen(2, path2, OpenMode.Input) 'open dt2.txt
        Input(1, s) ' read dt1.txt
        Console.WriteLine($"dt1 print:")
        Console.WriteLine($"first print: {s}")

        Input(1, s) ' read dt1.txt
        Console.WriteLine($"second print: {s}")

        Input(1, s) ' read dt1.txt
        Console.WriteLine($"third print: {s}")

        Console.WriteLine()

        Console.WriteLine($"dt2 print:")
        Input(2, s3) ' read dt2.txt

        Console.WriteLine(s3)
    End Sub
End Module

Input 函数是以英文逗号为分割符来取值的。下面截图更能直观说明。
在这里插入图片描述
在这里插入图片描述

LineInput

public static string LineInput (int FileNumber);

读取一行数据

Imports System
Imports System.IO
Imports VB = Microsoft.VisualBasic
Imports Microsoft.Office.Interop.Access
Imports System.Linq
Imports System.Collections.Generic

Module Program
    Sub Main(args As String())
        Dim s As String = Nothing
        Dim s2 As String = Nothing
        Dim s3 As String = Nothing
        Dim s4 As String = Nothing
        Dim path1 As String = "C:\Study\VBStudy\VBStudy\bin\dt1.txt"
        Dim path2 As String = "C:\Study\VBStudy\VBStudy\bin\dt2.txt"


        FileOpen(1, path1, OpenMode.Input) 'open dt1.txt
        FileOpen(2, path2, OpenMode.Input) 'open dt2.txt

        Console.WriteLine($"dt1 print:")

        s = LineInput(1) ' read a line
        Console.WriteLine($"first print: {s}")

        s = LineInput(1)
        Console.WriteLine($"second print: {s}")

        s = LineInput(1)
        Console.WriteLine($"third print: {s}")
    End Sub
End Module

在这里插入图片描述

dim & redim & redim preserve

        'dim redim redim preserve
        Dim testStr() As String = {"a", "b"} ' 用dim声明teststr数组
        Console.WriteLine(testStr(0) + testStr(1))
        ReDim testStr(10) '重新分配数据容量,并清空数组内容
        Console.WriteLine(testStr(0) + testStr(1))

        Dim testStr1() As String = {"aa", "bb"}  ' 用dim声明teststr数组
        Console.WriteLine(testStr1(0) + testStr1(1))
        ReDim Preserve testStr1(10) '重新分配数据容量,不清空数组内容
        Console.WriteLine(testStr1(0) + testStr1(1))

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值