pls, When do we have to use the New keyword and when we don t have to use it

Sometimes we have to use New when declaring objects (I think like when declaring dataset, sqlDataadapter objects.... )

I think in those cases, if we don t put the New keyword like in:

dim ds as New Dataset

it ll give us an error when we do: da.Fill(ds)

But, on the other hand, when declaring a usercontrol. we don t have to use New keyword like in:

dim uc as UserControl

uc=LoadControl("........ascx")

Anyexplanation for this pls.




Answer this question

pls, When do we have to use the New keyword and when we don t have to use it

  • JohnGray

    Some methods will create the object for you.

    In your first example, you created the object, in your second example your calling something that creates it.

    It no different that doing this

    ds = Test

    Private Function Test as Dataset

    Return New Dataset

    End Function

    Does that make any sense to you


  • myoungbl

    just coz
  • AbeR

    correct me if I m wrong.

    So if we use: da.Fill(ds) then ds should ve been declared with New already

    but if I do ds=ResturnMyDataSet() this within Page_Load for ex where ResturnMyDataSet is a function that instantiates a dataset privateds and returns this latter as a return value, then I don t have to declare the ds used by: ds=ResturnMyDataSet() in Page_Load

    The same thing for sqwldataadapter, because we do: da.fill then da should be declared with New, but if we do da=... then we don t haver to declare da in advance since da=.... will create an instance for it right

    Thanks for clarifying this for me



  • pls, When do we have to use the New keyword and when we don t have to use it