تفاوت ها ی بین VisualBasic.netو VisualC#.net(قسمت دوم و اخر)

5 04 2008

Overflow checking:

Vb.net داره یک تنظیم کننده پروژه برای بررسی خطای سریز. اگر چه امکان بررسی این خطا می تونه روشن یا خاموش باشه در پروژه ی شما , در سطح یک عبارت یا یک بلوک کد.

برای خاموش کردن این ویژگی در Vb.net مراحل زیر را دنبال کنید :

  • منوی Project , کلیک Properties
  • در Configuration Properties , انتخاب Optimizations و سپس انتخاب یا حذف Remove integer overflow checks

در Vc.net دستورات می تونن اجرا بشوند با عبارت checked یا unchecked. در یک عبارت checked, رخ دادن سریز باعث خطا می شود و در مقابل با عبارت uncheckedسریز نادیده گرفته می شود و نتیجه بریده می شود که می تونه دریک عبارت یا بلوک کد مورد استفاده قرار بگیرد .

Parameter passing:

Vb.net استفاده می کند ByVal را برای فرستادن پارامتر با مقدار و استفاده می کند ByRef برای فرستادن پارامترها با ارجاع.Vb.net همچنین می تونه مجبور کند تا پارامترها با مقدار فرستاده شوند , صرفنظر از چگونگی معرفی انها , با محصور کردن پارمترها در میان کمان ها ی اضافی . Vb.net همچنین گزینه هایی رو برای پارامتر ها فراهم می کنه که در Vc.net موجود نیست.

Vc.net نداره روشی برای فرستادن نوع های ارجاعی بوسیله ی ارزش . شما می تونید بفرستید (بر پایه ی اشاره گرها) یا ارجاع به ارجاع ( اشاره گر به اشاره گر).متدهای مدیریت نشده Vc.net می تونن بکار بگیرند اشاره گرها را همچون متدهای Visual C++. برای فرستادن یک پارامتر به صورت ارجاع Vc.net استفاده می کند کلمه ی کلیدی Ref را .برای استفاده از پارامتر Ref ارگومان باید به طور صریح فرستاده شود با ارگومان Ref. مقدار ارگومان Ref فرستاده می شود به پارامتر Ref.

Purpose Visual Basic .NET Visual C# .NET
Pass by value Public Sub ABC (ByVal y As Long)

End Sub
ABC(x)
ABC((x))
void ABC(int x)
{

}
ABC(i);
Pass by reference Public Sub ABC(ByRef y As Long)

End Sub
ABC(x)
void ABC(ref int x)
{

}
ABC(ref i);
Optional parameter Supported n/a

Late binding:

هم Vb.net و هم Vc.net می توانند پیاده سازی کنند late binding را ( مقید سازی دیر هنگام).اگر چه انجام این کار در Vb.net بسیار ساده تر از از C#.net است . در Vb.net همچون Visual Basic 6.0 کامپایلر فراخونی متد هایی کمک کننده به طور پنهان که بکار می گیرد انعکاس بدست اوردن نوع شیء را . ارگومان فرستاده شده به متد یاری رسان به کار می گیرد متد مناسب , که در هنگام زمان اجرا فعال گردد. این ارگومان های شییء هایی هستند که فعال می کنند متد ها را , نام متد فعال شده که یک رشته است , و ارگومان هایی که فرستاده می شوند برای فعال سازی متد که هست ارایه ای از اشیاء .علاوه بر ان شما می تونید پیاده سازی کنید مقید سازی دیر هنگام را بواسطه ی reflection.

Imports System

Module Hello

Sub Main()

‘ Set up variable.

Dim helloObj As Object

‘ Create the object.

helloObj = new HelloWorld()

‘ Invoke the print method as if it was early bound

‘ even though it is really late bound.

helloObj.PrintHello(”Visual Basic Late Bound”)

End Sub

End Module

در Vc.net پیاده سازی مقید سازی دیر هنگام بسیار متفاوت از Vb.net است در عوض پیاده سازی مقید سازی دیر هنگام توسط کامپایلر , شما باید به طور صریح late binding را در کد معرفی کنید بوسیله ی

Reflection.

Handling unmanaged code:

Vc.net به شما اجازه ی نوشتن کدهای مدیریت نشده را می دهد . در کدهای مدیریت نشده شما می توانید انجام بدید هر کاری مانند مقدار دهی و استفاده از اشاره گرهارا , تبدیل بین اشاره گرها و نوع های بی کسر و گرفتن ادرس متغیرها. تا حدی نوشتن کدهای مدیریت نشده در Visual C مشابه یک برنامه VC.net است .

به این دلیل که کد با استفاده از محتوای مدیریت نشده نوشته شده پس نمی توان ان را به طور ایمن محافظت نمود , و تنها وقتی اجرا می شود که کد کاملا معتبر باشد .به کار نگیرید کدهای مدیریت نشده زبان Visual c را در Vc.net. کدهای مدیریت نشده باید اشکارا مشخص شوند با اصلاع کننده های غیر ایمن (modifier unsafe )که توسعه دهندگان نمی توانند مدیریت کنند اتقاقات ناگهانی و پیش بینی نشده را , و وقتی اجرا می شود که اطمینان حاصل کند که کد مدیریت شده در محیط معتبری قراد دارد. هدف از محتوای مدیریت نشده توسعه ی پارامترهای لیست برای تابع نهایی است , همچنین از اشاره گرها نیز می توان در لیست پارامترها استفاده نمود.

در Vb.netشما کد مدیریت نشده نمی توانید بنویسید.

Keywords:

در جدول زیر لیست کلمات کلیدی در چندین دسته برای دو زبان Vc.net و vb.net نشان داده شده اند.

Purpose Visual Basic .NET Visual C# .NET
Object Oriented Programming
Indicates a class constructor Public Class Class1
Public Sub New(..)
MyBase.New

End Sub

End Class

Note: You have to call the base class constructor explicitly in Visual Basic .NET.

public class Class1
{
public Class1(..)
{

}

….

}

Note: The call to the base class constructor (base()) is generated automatically by the compiler in Visual C# .NET if you do not include constructor initializers.

Indicates a class destructor
Note: The Destructor or Finalize method is called by garbage collection.
Protected Overrides Sub Finalize()
m_Gadget = Nothing
m_Gear = Nothing
MyBase.Finalize()
End Sub
public class Class1
{
public ~Class1()
{
….
}

}

Declares a class Class class
Indicates class inheritance Public Class A
Inherits B

End Class
public class A : B
{

}
Indicates that the class can only be inherited and cannot be instantiated MustInherit abstract
Indicates that the class cannot be inherited NotInheritable sealed
Calls your own implementation of the method instead of an overridden method in the derived class MyClass None
Refers to a base class from the derived class MyBase base
Declares a type-safe reference to a class method Delegate delegate
Indicates that the method or the property overrides the implementation in its base class Overrides override
Indicates that these methods have no implementation and must be implemented in derived classes MustOverride
(in MustInherit
class)
abstract
(in abstract
class)
Indicates that the method or the property cannot be overridden in derived classes NotOverridable
Note: By default, methods are not overridable.
sealed
Indicates that the method or the property can be overridden in an inheriting class Overridable virtual
Overloads a procedure, a function, or a method Overloads None. Define functions with same name but different signatures.
Specifies that a variable can contain an object whose events you want to handle WithEvents No specific keyword
Specifies the events for which an event procedure will be called Handles
(Event procedures can still be associated with a WithEvents variable by naming pattern.)
n/a
Evaluates an object expression one time to access multiple members With objExpr
<.member>
<.member>
End With
n/a
Refers to the current object Me This
Declares an enumerated type Enum

End Enum
Enum
Declares an interface Interface interface
Implements an interface Implements class
C1 : I1
Indicates an indexer Default Property public string this[int index]
{
get {return List[index];}
set {List[index]=value;}
}
Class Access Modifiers
Indicates that the modifier is accessible outside the project or the assembly Public public
Indicates that the modifier is accessible inside the assembly only Friend internal
Indicates that the modifier is accessible only in the project (for nested classes, in the enclosing class) Private private
Class Member Access Modifiers
Indicates that the modifier is accessible outside the class and the project Public public
Indicates that the modifier is accessible outside the class, but in the project Friend internal
Indicates that the modifier is only accessible in a class or a module Private private
Indicates that the modifier is accessible only to current and derived classes Protected protected
Indicates the union of Protected and Friend or Internal Protected Friend protected internal
Indicates that the members are shared across all instances Shared static
Miscellaneous Lifetime
Preserves the local variables for the procedure Static n/a
Other
Calls the Windows API Declare statement use Platform Invoke
Indicates a comment ‘, Rem //, /* */ for miltine comments,
/// for XML comments
Indicates a constant Const Const, readonly
Creates a new object New, CreateObject new
Declares a function or a method with no return value Sub void
Declares that an object can be modified asynchronously n/a volatile
Declares a variable Private, Public, Friend, Protected, Static, Shared, Dim declarators (keywords include user-defined types and built-in types)
Declares a variable explicitly Option Explicit None (All variables must be declared before use)
Declares and raises an event Event, RaiseEvent event
Declares a structure Structure

End Structure
struct
Defines a default property Default by using indexers
Declares a null object Nothing null
Declares a namespace Namespace

End Namespace
Namespace
{

}
Indicates namespace usage Imports using
Retrieves a character from a string GetChar Function [ ]
Returns the address of a function AddressOf (For class members, this operator returns a reference to a function in the form of a delegate instance) delegate
Tests for a null object Obj Is Nothing obj == null
Tests for a database null expression IsDbNull n/a
Threads primitives SyncLock lock

مشاهده قسمت اول مقاله

استفاده از مطالب فارسی فوق با ذکر منبع بلامانع (http://w2kir.wordpress.com) است .

دانلود متن انگلیسی مقاله از سایت ماکروسافت


کارها

اطلاعات

یک پاسخ

25 05 2008
مهدي

از مطلب بسيار خوبتون ممنون
من به عنوان يك مبتدي خيلي برام مفيد بود.
مرسي

دیدگاه‌تان را بنویسید: