C # vs vb7 syntax and programming

zhaozj2021-02-17  74

C # and VB7 is the main programming tool for Microsoft .NET. This article compares the C # language and VB7 language and its programming. Each program is written in VB7 language, and then rewrite with C # language. It is seen that the current VB7 is actually C #, which will be (different languages) To the .NET class function library is the essence of Microsoft .NET.

First, variable declaration

C # and VB7 variables are basically consistent. Unlike VB6, VB7 can assign initial values ​​while declaring variables, no declaration of DIM X, VB7's integer variable is 4-byte (32-bit integers, -2147483648 --2147483647 ), Long integer (long) is 8 bytes (64-bit integers, -9, 223, 372, 036, 854, 775, 808-9, 223, 372, 036, 854, 775, 807).

C #

VB7

INT X;

Long Y

String S;

String S1, S2;

Object O;

Object obj = new object ();

Public String Name;

DIM X as integer

DIM Y As Long

DIM S As String

DIM S1, S2 AS STRING

DIM OBJ AS New Object ()

Public Name As String

The following VB7 is compiled with the C # program.

X = 99999999

S = VB7 programming 100 cases

S1 = VB7 Programming, S2 = "VB7 programming 

VB7 routines (01_vb.vb):

Imports system

Public Module LY

Public Name As String, PUBLIC NAME AS STRING

Sub main ()

DIM X as integer = 99999999

DIM S as String = "100 cases of VB7 programming"

DIM S1, S2 AS STRING

'All variable declarations must have AS, not allowed to: DIM O declaration; declaration variables can be assigned

DIM OBJ AS New Object ()

S1 = "VB7 Programming": S2 = "VB7 programming"

Console.WriteLine ("x = {0}", x) 'display variable x

Console.writeLine ("s = {0}", s) 'Display variable S

Console.writeLine ("S1 = {0}, S2 = {1}", S1, S2) 'Display Variables S1, S2

End Sub

End module

The C # routine (01_c # .cs) is almost identical to the VB7 routine:

Using system;

Class LY {

Public String Name;

Static void main () {

INT x = 9999999;

String s = "100 cases of VB7 programming";

String S1, S2;

Object O;

Object obj = new object ();

S1 = "VB7 programming"; s2 = "VB7 programming";

Console.WriteLine ("x = {0}", x); // Display variable X

Console.WriteLine ("s = {0}", s); // Displays the variable sconsole.writeLine ("S1 = {0}, S2 = {1}", S1, S2); // Display variables S1, S2

}

}

Second, the NET class function library call

C # and VB7 call the same .NET class function library, so C # is completely the same as the VB7 program call .NET class function library.

The following example defines how the method INPUT () implements a long integer M, method CalcsQRT (mm as long) calculates and displays the square root of M; the input and output method for console CONSOLE in the .NET class function library And mathematical calculation class MATH (seek square root) method SQRT; in addition, the method of converting the digital string CC into long intensity using the method of INT64.

Program operation results (assuming input is 999, note that the math function return value is generally double precision)

Type a positive integer:

Calculation results: SQRT (999) = 31.606, 961, 258, 558, 215

It is easy to see that this C # is almost exactly the same as the VB7 program, and it is easy to change to the C # program. It will be unified to the .NET class function library, which is the .NET class function library. The essence.

VB7 routines (02_vb.vb):

Imports system

Public Module LY

DIM m as long 'is not case sensitive, long can be written as long, long, or long

DIM Q As Double

'Enter a long integer M in the keyboard

Public Sub INPUT ()

Console.write ("Please type a positive integer:")

DIM cc as string = console.readline () gets the digital string input by the keyboard

M = int64.fromstring (cc) 'Convert this string to 64-bit integer M

End Sub

'Calculate and display the square root of MM

Public Sub Calcsqrt (mm as long)

Q = Math.SQRT (mm) 'Mathematical function SQRT In System's class MATH, return to Double, not available SINGLE

Console.write ("calculation results: SQRT ({0}) = {1}", m, q) 'display result

End Sub

Sub main ()

INPUT

Calcsqrt (m)

End Sub

End module

C # routines (02_C # .cs) almost identical to VB7 routines:

Using system;

Class LY {

STATIC long m; // Check: Do you write long as long

Static Double Q;

/ / Enter a long integer M from the keyboard

Public static void input () {

Console.Write ("Please type a positive integer:");

String cc = console.readline (); // Get the number string input by the keyboard

m = int64.fromstring (cc); // convert this string to 64-bit integer M

}

/ / Calculated and display MM square root Q

Public Static Void Calcsqrt (long mm) {

Q = Math.SQRT (mm); // Mathematics function SQRT In System's class Math, return to Double, not available SINGLE

Console.write ("Calculation Result: SQRT ({0}) = {1}", M, Q); // Display result}

Static void main () {

INPUT ();

Calcsqrt (m);

}

}

Third, object-oriented programming

In the above example, the method INPUT () is defined from the keyboard to enter a long integer M, method calcsqrt (mm as long) calculates and displays the square root of M, and then calls in the main () method; the method INPUT () Method CalcsQRT (MM As long) is independently placed in a special class (LY0), then generates instance objects in the class LY's main () method:

The program operation results are still exactly the same as (b).

In the case of object-oriented, this C # and VB7 program are almost exactly the same. From this sense: VB7 is C #, C # is also VB7.

VB7 routines (03_vb.vb):

Imports system

Class LY0

The 'variable M and Q are now encapsulated in the class LY0, because another class LY wants to reference M, and M plus public

Public Dim M As Long

DIM Q As Double

'Enter a long integer M in the keyboard

Public Sub INPUT ()

Console.write ("Please type a positive integer:")

DIM cc as string = console.readline () gets the digital string input by the keyboard

M = int64.fromstring (cc) 'Convert this string to 64-bit integer M

End Sub

'Calculate and display the square root of MM

Public Sub Calcsqrt (mm as long)

Q = Math.SQRT (mm) 'Mathematical function SQRT In System's class MATH, return to Double, not available SINGLE

Console.write ("calculation results: SQRT ({0}) = {1}", m, q) 'display result

End Sub

END CLASS

Public Module LY

Sub main ()

DIM OBJ AS LY0 = New LY0 ()

Obj.input

Obj.calcsqrt (obj.m)

End Sub

End module

C # routines (02_C # .cs) almost identical to VB7 routines:

Using system;

Class LY0 {

// Variable M and Q are now encapsulated in the class LY0, because another class LY wants to reference M, and M plus public

Public long M;

Double Q;

/ / Enter a long integer M from the keyboard

Public void input () {

Console.Write ("Please type a positive integer:");

String cc = console.readline (); // Get the number string input by the keyboard

m = int64.fromstring (cc); // convert this string to 64-bit integer M

}

/ / Calculated and display MM square root Q

Public void Calcsqrt (long mm) {

Q = Math.SQRT (mm); // Mathematics function SQRT In System's class Math, return to Double, not available SINGLE

Console.write ("Calculation Result: SQRT ({0}) = {1}", M, Q); // Display result

}

}

Class LY {

Static void main () {

LY0 OBJ = New LY0 (); obj.input ();

Obj.calcsqrt (obj.m);

}

}

转载请注明原文地址:https://www.9cbs.com/read-31255.html

New Post(0)