Connecting Tech Pros Worldwide Forums | Help | Site Map

Usage of the "New" KeyWord

Sam
Guest
 
Posts: n/a
#1: Nov 20 '05
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.AquaMarine)
Dim bgColor As Color = Color.LightYellow

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
Guest
 
Posts: n/a
#2: Nov 20 '05

re: Usage of the "New" 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
http://www.knowdotnet.com/articles/referencetypes2.html

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!
https://www.windowsembeddedeval.com/...ity/newsgroups
"Sam" <qdo@datawave.ca> wrote in message
news:Oztn9IndEHA.2812@tk2msftngp13.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.AquaMarine)
> Dim bgColor As Color = Color.LightYellow
>
> 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]


Jerry
Guest
 
Posts: n/a
#3: Nov 20 '05

re: Usage of the "New" KeyWord


William,

When you say "Pass in a Reference Type ByVal to a method and any changes
made to it are made to the object you
passed in." I am afraid you are incorrect.

A string type in VB.NET is a reference type. If you send it "ByVal" to a
Subroutine or function and change it inside the subroutine or function the
original will remain intact.

Regards

"William Ryan eMVP" <dotnetguru@comcast.nospam.net> wrote in message
news:OpzjcPndEHA.2352@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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[/color]
an[color=blue]
> object , Dim i as Integer is ok. This may help a little if you aren't
> familiar with the distinction
> http://www.knowdotnet.com/articles/referencetypes2.html
>
> 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[/color]
the[color=blue]
> 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!
> https://www.windowsembeddedeval.com/...ity/newsgroups
> "Sam" <qdo@datawave.ca> wrote in message
> news:Oztn9IndEHA.2812@tk2msftngp13.phx.gbl...[color=green]
> > Hi everyone
> >
> > Could anyone help me understand the usage of the "New" keyword I'm new[/color]
> to[color=green]
> > VB.Net.
> >
> > 1. Why do we use the "New" keyword on some object type variables such as[/color]
> the[color=green]
> > myPen of the example below and not with the bgColor. Both the Pen and[/color]
> Color[color=green]
> > are objects
> >
> > Dim myPen As Pen = New Pen(Color.AquaMarine)
> > Dim bgColor As Color = Color.LightYellow
> >
> > 2. Why do properties such as Size, Location, Font... of controls require[/color][/color]
a[color=blue][color=green]
> > new instance of their classes when we want to change their properites at[/color]
> run[color=green]
> > time
> >
> >
> > Thanks
> >
> > Sam
> >
> >[/color]
>
>[/color]


Guest
 
Posts: n/a
#4: Nov 20 '05

re: Usage of the "New" KeyWord


"Jerry" <JerryOfBorg@yahoo.com> wrote in message
news:OoWy7m2dEHA.3988@tk2msftngp13.phx.gbl...[color=blue]
> William,
>
> When you say "Pass in a Reference Type ByVal to a method and any changes
> made to it are made to the object you
> passed in." I am afraid you are incorrect.
>
> A string type in VB.NET is a reference type. If you send it "ByVal" to a
> Subroutine or function and change it inside the subroutine or function the
> original will remain intact.
>
> Regards
>[/color]


But if you look closely at the String definition :

A String is called immutable because its value cannot be modified once it
has been created. Methods that appear to modify a String actually return a
new String containing the modification. If it is necessary to modify the
actual contents of a string-like object, use the System.Text.StringBuilder
class

Anytime you 'change' the string vaiable you are changing the reference to
point at another possibly new string object, not changing the data within
the original.

--
Jonathan Bailey.


Jonathan Allen
Guest
 
Posts: n/a
#5: Nov 20 '05

re: Usage of the "New" KeyWord


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



When you write this...
myForm.Size.Width = 10

This is what happens behind the scenes...
Dim X as Size 'Size is a value type
X = myForm.Size Get a copy of the form's size value
x.Width = 10 'Change the copy

Which is why you need to do this...
myForm.Size = X 'Replace the form's size with a copy of X


*******************

A Font is an immutable class, that is a class that cannot be altered. The reason is buried in the deep inner-workings of Windows.

One thing is for sure, it reduces the chance for errors. If you could change a font object instead of replacing it, you might accidentally change the font on a bunch of unrelated controls. Why? Because they probably share the same font object.

--
Jonathan Allen


"Sam" wrote:
[color=blue]
> 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.AquaMarine)
> Dim bgColor As Color = Color.LightYellow
>
> 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
>
>
>[/color]
Jonathan Allen
Guest
 
Posts: n/a
#6: Nov 20 '05

re: Usage of the "New" KeyWord


> 1. Why do we use the "New" keyword on some object type variables such as the[color=blue]
> myPen of the example below and not with the bgColor. Both the Pen and Color
> are objects
>
> Dim myPen As Pen = New Pen(Color.AquaMarine)
> Dim bgColor As Color = Color.LightYellow[/color]

A Color is a simple class, so it doesn't matter if you used the names constants or the New operator.

A Pen uses Windows resources, and has to be treated with respect.

If you create a Pen using New, you need to call Dispose on it when your done.

There is also Pens class. Example...

Dim myPen As Pen = Pens.AquaMarine

If you get an existing Pen from the Pens class, then you may not dispose it. If you try, you will get this message.... "Additional information: You may not change this Brush because it does not belong to you."

--
Jonathan Allen


"Sam" wrote:
[color=blue]
> 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.AquaMarine)
> Dim bgColor As Color = Color.LightYellow
>
> 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
>
>
>[/color]
Closed Thread