473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Usage of the "New" KeyWord

Sam
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
Nov 20 '05 #1
1 5471
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" <qd*@datawave.ca> wrote in message
news:Oz**************@tk2msftngp13.phx.gbl...
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

Nov 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul Auleciems | last post by:
Hi: I'm having trouble using an Object which is created based on the following: Public CarDetail () as Car Where the CLASS "Car" is defined as: Public Class Car
1
by: alanrn | last post by:
I've implemented a number of strongly-typed collections that inherit from CollectionBase and recently noticed something that I don't fully understand. CollectionBase defines method RemoveAt(). ...
12
by: Ola Johansson | last post by:
Can anyone explain to me a meningfull use of the "new" keyword (Shadows in VB). I think i understand how it works fully but i cant figure out why you would use it. Why would you want to declare...
8
by: Dot net work | last post by:
I need VB.NET's "shadows" functionality inside a C# project. I tried the "new" keyword, but it didn't seem to work, because my particular function does in fact differ in signature to the function...
0
by: Michael Steidl | last post by:
I use XMLspy for handling XML files and started testing its feature to generate C# code from XML Schemas. Now I got stuck in a strange situation: (I have to add being not a C# geek, only currently...
5
by: Sam | last post by:
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...
4
by: Ben R. | last post by:
I'm curious about the differeng behavior of the "new" keyword when dealing with value versus object types. If I'm correct, when I do: dim x as integer There's no need for "new" to be called...
3
by: padhuwork | last post by:
Hi, I m actually making a Windows DLL using VB 6. I have modified the Link.exe, so that, it compiles a Windows DLL. I m now struck with this. I add reference "Microsoft Scripting Runtime"...
6
by: vinnie | last post by:
i'm at the very beginning, and while trying to use the following small code, i got an error msg thta said use the "new" keyword to create an object instance. Actually, since i'm at the very...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.