Member Avatar for sanjeewa.abeywardana

this might be a silly question :-) but just for reference asking that
System.Drawing.Graphics is a reference type ? or value type ?
I mean when creating objects like this

  Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim i As Integer = 10
        Dim g As Graphics = Graphics.FromHwnd(Me.Handle)
        g.DrawString(i, New Font("Arial", 20.0F), Brushes.Blue, New PointF(0, 0))

    End Sub

is g a reference type varable ?
I HOPE IT IS YES ?

Recommended Answers

All 5 Replies

Simple types such as Integer, Boolean, etc (i.e. types that do not require instantiation with "New") are value types. Anything that is used to access an object is a reference type. In your example, Graphics refers to an object (as indicated by the fact that you can access methods/properties via dot notation).

Short answer - YES.

Member Avatar for sanjeewa.abeywardana

Thanks dude, the ansewer is short but very clear ,it's just I had a small doubt about this one ....

As a point of interest, the designers of VB could have made ALL variables objects (reference types) but there is a performance price to pay and it was decided to leave the simple type as simple types to avoid this penalty.

As a point of interest, the designers of VB could have made ALL variables objects (reference types) but there is a performance price to pay and it was decided to leave the simple type as simple types to avoid this penalty.

???
From the documentation for the ValueType Class:

ValueType overrides the virtual methods from Object with more appropriate implementations for value types. See also Enum, which inherits from ValueType.

Data types are separated into value types and reference types. Value types are either stack-allocated or allocated inline in a structure. Reference types are heap-allocated. Both reference and value types are derived from the ultimate base class Object. In cases where it is necessary for a value type to behave like an object, a wrapper that makes the value type look like a reference object is allocated on the heap, and the value type's value is copied into it. The wrapper is marked so the system knows that it contains a value type. This process is known as boxing, and the reverse process is known as unboxing. Boxing and unboxing allow any type to be treated as an object.

Although ValueType is the implicit base class for value types, you cannot create a class that inherits from ValueType directly. Instead, individual compilers provide a language keyword or construct (such as struct in C# and Structure…End Structure in Visual Basic) to support the creation of value types.

You can pass a value such as variable,char,boolean in the function,it is referred as value type and its return a value.If you can pass a reference such as array,structure,string,pointer in the function argument,it is referred as reference type and it return a reference.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.