locked
how would i know that this class....... RRS feed

  • Question

  • User-685390753 posted
    Hi, Some class's intance need to create with NEW keyword like dim t as new myclass or myclass t = new myclass(); and,some class's intance doesnt need to create with NEW keyword like dim t as myclass or myclass t; now my question is How to know that this class need to instatiate with NEW keyword or not & why ? please give me some explanations Regards, ASIF
    Saturday, September 20, 2008 6:59 AM

Answers

  • User2032526919 posted

    Hi,

    generally if you use structures (value types), you don't need to instantiate them because they're assigned a default value when they are declared. Classes (reference types) you need to instantiate unless, you take a reference to already instantiated class or if they are static classes when they don't need/allow explicit instantiation. Note that you can also declare variable separately from instantiating an object of its type

    Dim t As Integer 'Initializes to 0 by default
    Dim s As Integer = 5 ' we initialize it to 5 explicitly

    Dim dt As New DataTable() ' we instantiate a new DataTable, DataTable is a class
    Dim dt2 As DataTable = dt 'we take a reerence to the other DataTable'

    'we can also

    Dim dt3 As DataTable 'we just declare a variable, it is null reference
    '...Later
    dt3=New DataTable() 'now it points to an object instance

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 20, 2008 7:25 AM