Why is “New” constructor used sometimes, but not o

2019-07-28 14:28发布

问题:

I have a basic oop question:

I use (for example in the sharepoint development) at the beginning of an operation a New constructor, but later not. For example:

' CREATES A INSTANCE OF SPSITE FROM GIVEN URL '
Dim site As spsite = New spsite("http://myhost")

' HERE I DO NOT NEED TO USE NEW(...) AND IT RETURNS AN SPSITE OBJECT, '
' A NEW SPSITE OBJECT '
Dim web As spweb = site.openweb()

My question: Why do i need at the first object of site the use of new, and in the second line not!?

回答1:

I don't really understand your problem, but if both lines are in your code (and they should be) then it works like this: This first line creates a new spsite object, which you can access with the variable named: site

dim site as spsite = new spsite("http://myhost")

Here you create a new spweb variable, by invoking the openweb() method of your previously created site variable. This is a function call. this function returns an spweb instance so you don't need a new one.

dim web as spweb = site.openweb()

openweb() either has a new spweb() inside it, or the new spsite("myhost") created a new openweb for you (this you don't have to know, you just know, you'll get an spweb object)



回答2:

This is because the 'openweb' method on the 'site' object instance is allocating the object for you (with new). Whereas, on the first line, you are creating the object yourself.

In this case, it is a function of the openweb method to allocate an spweb instance.

When we write code, we are responsible for allocating new instances of objects, but it is typical that these objects will also instantiate instances of other objects.



回答3:

new is an operator to allocate memory for an object on the heap. It is then usually properly constructed by a constructor. These are basically two actions. openweb appears to be a method you invoke on an existing instance. As it is still there, you do not need to allocate more memory on the heap.

openweb may create a new object which it returns though, but then it is not you who performs a new. openweb does, or it returns an existing object.



回答4:

The new operator simply calls a constructor. In your example, you are directing the creation of an SPSite object with New spsite("http://myhost"), but you are asking SPSite to create an SPWeb object for you with site.openweb().

Also, SharePoint is a little tricky on memory management, and there are times you actually have to worry about cleaning up after yourself. This article provides a pretty good overview of when to dispose of objects (it's a three part series written in C#, but the principles are the same). If you are developing a SharePoint application, please do yourself a favor and familiarize yourself with when to dispose, and use SPDisposeCheck to assist during development.



回答5:

I think your confusion may be that you are equating variables with objects. Variables are not actually objects, they are just a pointer to an object (a reference). The New operator does not create a new variable, it creates a new object. You can declare as many variables as you want without ever calling New because the New operator has nothing to do with variables. A single object can be referenced by any number of variables, for instance:

Dim variable1 As StringBuilder  ' Declare a new variable
Dim variable2 As StringBuilder  ' Declare a new variable
' At this point, both variables are null (Nothing) because 
' they do not reference/point to any object yet
variable1 = New StringBuilder()  ' Create a new object and set variable1 to reference it
variable2 = variable1  ' Set variable2 to reference the same object as variable1
variable2.AppendLine("Hello world")
Console.WriteLine(variable1.ToString())  ' Writes "Hello World"

As you can see with the above code, even though I appended the text to variable2, it affected variable1 as well. That is because the append was performed against/by the object that is pointed to by the variable, not by the variable itself. So, since both variables pointed to the same object, they both contain the same text and will see the same changes made to that object.

So, you don't need to call New every time you declare a new variable. Many times, you just need a variable to reference an object that already exists. The only time you need to use the New operator is when you need to create a new object. So, in your code, the first line needs to create a new sprite object and then set a variable to reference it. But, the second line doesn't need to create a new object, it just needs a variable to reference the object that is returned by the openweb method. Presumably, somewhere inside that openweb method, it is internally using the New operator to create a new object and then returns it.

However, I should mention that everything I've said only applies to "reference types" (classes). It is not true of "value types" (structures), such as Integer, Boolean, Date, etc. I recommend doing some research on the difference between reference types and value types.



回答6:

Thanks! Ok, i understand. I have class a and class b. I create an instance of class a and here i have a method which has for example

return new classB

Therefore its not needed to use new, cause its provided by the method of a class.

thanks you all!



标签: .net vb.net oop