Usage of the "New" KeyWord

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sam

    Usage of the "New" KeyWord

    Hi everyone

    Could anyone help me understand the usage of the "New" keyword I'm new to
    VB.Net.

    1. Why do we use the "New" keyword on some object type variables such as the
    myPen of the example below and not with the bgColor. Both the Pen and Color
    are objects

    Dim myPen As Pen = New Pen(Color.AquaM arine)
    Dim bgColor As Color = Color.LightYell ow

    2. Why do properties such as Size, Location, Font... of controls require a
    new instance of their classes when we want to change their properites at run
    time


    Thanks

    Sam


  • William Ryan  eMVP

    #2
    Re: Usage of the "New&qu ot; KeyWord

    Color is a Structure, hence a ValueType whereas Pen is a Reference type.
    Everythign in .NET is an 'object' btw. That's why even though integer is an
    object , Dim i as Integer is ok. This may help a little if you aren't
    familiar with the distinction


    The difference in behavior between valuetypes and referencetypes is NOT
    trivial by any means, but understanding the differences is pretty
    straightforward . For instance, pass in a Value Type by value to function
    and modify it, the original is still in tact. Pass in a Reference Type
    ByVal to a method and any changes made to it are made to the object you
    passed in.

    I'm not sure I totally understand your second question but I'll take a
    guess. Those properties are instance properties, meaning they belong to the
    instance. Other properties are static (shared in VB) which means they
    belong to the class and not a specific instance.

    HTh,

    Bill

    --
    W.G. Ryan MVP Windows - Embedded

    Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
    Let Microsoft know!

    "Sam" <qdo@datawave.c a> wrote in message
    news:Oztn9IndEH A.2812@tk2msftn gp13.phx.gbl...[color=blue]
    > Hi everyone
    >
    > Could anyone help me understand the usage of the "New" keyword I'm new[/color]
    to[color=blue]
    > VB.Net.
    >
    > 1. Why do we use the "New" keyword on some object type variables such as[/color]
    the[color=blue]
    > myPen of the example below and not with the bgColor. Both the Pen and[/color]
    Color[color=blue]
    > are objects
    >
    > Dim myPen As Pen = New Pen(Color.AquaM arine)
    > Dim bgColor As Color = Color.LightYell ow
    >
    > 2. Why do properties such as Size, Location, Font... of controls require a
    > new instance of their classes when we want to change their properites at[/color]
    run[color=blue]
    > time
    >
    >
    > Thanks
    >
    > Sam
    >
    >[/color]


    Comment

    Working...