|
-
Apr 3rd, 2003, 05:50 PM
#1
Thread Starter
PowerPoster
Creating new objects
I am kinda confused by the way i am seeing objects created in vb.net.
In VB there was late and early binding
VB Code:
Dim MyObj as Object
Set MyObj = New Object
'would be late binding
Dim MyObj as New Object
'would be early binding
But in .Net i've notice some odd ways of creating an object.....examples
VB Code:
Dim TheFont As Font = New Font("Times New Roman", 24, System.Drawing.GraphicsUnit.Point) ' i dont get it. Dim the font as font = New Font....
Dim TheGraphic As Graphics = Graphics.FromImage(TheImage)' same thing here except no NEW keyword....huh?
TheGraphic.DrawString(TheText, TheFont, New SolidBrush(Color.FromArgb(Red, Green, Blue)), 0, 0) ' Creating a new object as a paramter of a function? never seen this before either.
Can anyone enlighten me a little.
Thanks!
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Apr 3rd, 2003, 06:15 PM
#2
Actually declaring anything as the generic Object type and then casting it to something would still be late binding in VB6 regardless of which declaration you used.
Anyways those funky ways of creating objects have been pretty standard in other languages and are new to VB. Since in VB.NET you can initialize a variable at the same time that you declare it:
VB Code:
Dim counter as Integer=1
'Same as
Dim counter as Integer
counter=1
Then you can do the samething with classes via the 'Constructor', which in VB.NET is a method named 'New' that is called when an instance of a class is created. Do a search on the web for Constructor or check it out in any good book for more info. They are very handy but basically they just help to initialize the class at the same time that you declare it:
VB Code:
Dim MyObject as New MyClass("default something",1)
'Same as
Dim MyObject as New MyClass()
MyObject.MyString="default something"
MyObject.MyInteger=1
-
Apr 4th, 2003, 12:13 AM
#3
Thread Starter
PowerPoster
Originally posted by Edneeis
Actually declaring anything as the generic Object type and then casting it to something would still be late binding in VB6 regardless of which declaration you used.
Yah i know i ment it as a generic place holder.. i should have put "AnObject".
Anyways those funky ways of creating objects have been pretty standard in other languages and are new to VB. Since in VB.NET you can initialize a variable at the same time that you declare it:
VB Code:
Dim counter as Integer=1
'Same as
Dim counter as Integer
counter=1
OK that makes since then thanks 
Then you can do the samething with classes via the 'Constructor', which in VB.NET is a method named 'New' that is called when an instance of a class is created. Do a search on the web for Constructor or check it out in any good book for more info. They are very handy but basically they just help to initialize the class at the same time that you declare it:
VB Code:
Dim MyObject as New MyClass("default something",1)
'Same as
Dim MyObject as New MyClass()
MyObject.MyString="default something"
MyObject.MyInteger=1
Yah i'm familiar with the NEW keyword i just had not seen it used in the ways it's being used in .net such as
VB Code:
Dim TheFont As Font = New Font("Times New Roman", 24, System.Drawing.GraphicsUnit.Point)
And i'm still not sure i totaly understand this statement. Why not just say
VB Code:
Dim TheFont As New Font("Times New Roman", 24, System.Drawing.GraphicsUnit.Point)
hmm why the = New FOnt().. makes no sense to me.
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Apr 4th, 2003, 03:04 AM
#4
Sleep mode
Don't let that confuses you , you can use it the same as the above declaration . I mean this way :
VB Code:
Dim TheFont As New Font("Times New Roman", 24, System.Drawing.GraphicsUnit.Point)
Button1.Font = TheFont
-
Apr 4th, 2003, 03:34 AM
#5
Well there is the New keyword that lets us declare a New instance of an object and now there is also the New keyword as a method in a class that defines the constructor (at least in VB otherwise the method must be the same name as the class). So:
VB Code:
Dim TheFont As New Font("Times New Roman", 24, System.Drawing.GraphicsUnit.Point)
'Same As
Dim TheFont as New Font()
TheFont.FontFamily="Times New Roman"
TheFont.Size=24
TheFont.Unit=Point
'Would like like this in the Class
Class Font
Public Sub New(fontfamily as string,size as double,unit as Point)
Me.FontFamily=fontfamily
Me.Size=Size
Me.Unit=unit
End Sub
End Class
Also some objects require something to be intialized, these usually don't have empty constructors, or can't be declared with:
Dim obj As New Something()
This usually means that it needs to be started with something that can't be dynamically change, somewhat like the old designtime only properties in VB6. Like the Font object, in .NET you can't really change a font object you have to replace it with a new one.
Last edited by Edneeis; Apr 4th, 2003 at 03:40 AM.
-
Apr 4th, 2003, 01:13 PM
#6
Thread Starter
PowerPoster
Ok thanks guys, i think i get it
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Apr 4th, 2003, 01:40 PM
#7
Fanatic Member
Is there a relationship between the New keyword and objects of Reference Type? In other words, do you only use the New keyword when instantiating Reference Types, and don't use New when instantiating Value Types.
For example:
Dim intCounter As Integer = 0
Dim myObj As New myClass()
-
Apr 4th, 2003, 01:47 PM
#8
Yes that is correct I believe only reference type variables use the New keyword. That is why the normal datatypes and structures don't require it.
-
Apr 4th, 2003, 01:52 PM
#9
In C# terms, if it is static you dont use New. Usually means they do not have a constructor.
-
Apr 4th, 2003, 02:35 PM
#10
PowerPoster
VB Code:
Dim TheFont As Font = New Font("Times New Roman", 24, System.Drawing.GraphicsUnit.Point)
' i dont get it. Dim the font as font = New Font....
This first creates the variable TheFont, then assigns it to a new object instance.
VB Code:
Dim TheGraphic As Graphics = Graphics.FromImage(TheImage)
' same thing here except no NEW keyword....huh?
This first creates the variable TheGaphic, then it assigns it a reference to an existing object so you can use the variable to affect that object.
VB Code:
TheGraphic.DrawString(TheText, TheFont, New SolidBrush(Color.FromArgb(Red, Green, Blue)), 0, 0)
' Creating a new object as a paramter of a function? never seen this before either.
Inserting the New SolidBrish(Color.FromArgb(Red, Green, Blue)) just makes it easier when you are required to supply an object as an argument to a method. That method requires a SolidBrush object, so instead of creating a seperate variable to insert into it, just pass it a new object with some simple settings. Example:
VB Code:
TheGraphic.DrawString(TheText, TheFont, New SolidBrush(Color.FromArgb(Red, Green, Blue)), 0, 0)
'or if you want to do some advanced property changes to the brush, you could do it this way.
Dim myBrush As SolidBrush = New SolidBrush(Color.FromArgb(Red, Green, Blue))
'Do all your property changes to your myBrush object here.
TheGraphic.DrawString(TheText, TheFont, myBrush, 0, 0)
So basically, if you have to pass an object into a method, but you could care less about changing that object before hand, you can just do an inline new statement like the first example. If you want to change some of the properties, and further customize the object before passing it into the method, then you should create a variable to hold the object that way you can modify it more before passing it to the method.
Last edited by hellswraith; Apr 4th, 2003 at 02:38 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|