Nothing new, is it possible?

Nothing

after a New

, is it possible?

Dim myObj As MyClass = Nothing
myObj = New MyClass(params)
If myObj Is Nothig Then
  ' is it possible?
End If

      

Is there a theoretical possibility that the constructor returns a null (Nothing) object?

Tell me to set Me = Nothing

in constructor? Or, if an exception is thrown in the constructor, what happens in the catch with the object? Or, on the last line of the constructor, I pass the "Me" reference to the method, and that method sets the reference to Nothing?

+3


source to share


2 answers


No, New

-operator is
used to create a new instance of an object. Even if all the fields of that object remain Nothing

, the instance itself is not Nothing

.

Visual Basic Language Specification:



11.10 New expressions The new operator is used to create new instances of types ...

11.10.1 Expressions for creating objects An expression for creating objects is used to create a new instance of a class type or structure type. the type of the object creation expression must be a class type, a struct type, or a type parameter with a new constraint and cannot be a MustInherit class. Given an object creation expression of the form New T (A), where T is a class type or structure type and A is an optional argument list, overload resolution determines the correct T constructor to call. The type parameter with the new constraint is considered the only constructor without parameters. If no constructor is callable, a compile-time error occurs; otherwise, the expression results in a new instance of T being created using the chosen constructor.If there are no arguments, the parentheses can be omitted. Where the allocation of an instance depends on whether instance is a class type or a value type. New instances of class types are created on the system heap, whereas new instances of value types are created directly on the stack. An object-object expression can optionally provide a list of element initializers after the constructor arguments. These element initializers are prefixed with the C keyword, and the initializer list is interpreted as if it were in the context of the With statement.

+2


source


Unless you are using On Error Resume Next

and MyClass

there is an exception in the constructor , or you are creating a proxy that returns Nothing

on creation
.

While confirming that the VB.NET version of the proxy is "working", I noticed that myObj Is Nothing

there is False

immediately after creation (like you asked in the OP) and yet when you try to do anything else with it it will definitely looks like Nothing

. And usually it becomes Nothing

after you try to do much more with it than test it for value. (At this point it is the same with C #. And at this point I should really start a new question ...)

But I found that having "empty" is Try Catch

enough for VB.NET to crystallize Nothing

! (Starting with Roslyn's LinqPad C # 6 (Beta), C # does the same thing.)



Sub Main()
    Dim myObj = New MyFunnyType()
    If myObj Is Nothing Then
      Call "It IS Nothing".Dump
    Else
      ' Comment out this Try and myObj will not be Nothing below.
      Try
        'Call myObj.ToString.Dump
      Catch nr As NullReferenceException
        Call "Maybe it was nothing?".Dump
      Catch ex As Exception
        Call ex.Message.Dump
      End Try
      Call myObj.Dump("Nil?")
      If myObj Is Nothing Then
        Call "Now it IS Nothing".Dump
      Else
        Call "It still is NOT Nothing!".Dump
      End If
    End If
End Sub

' Define other methods and classes here
Class MyFunnyProxyAttribute
  Inherits ProxyAttribute
    Public Overrides Function CreateInstance(ByVal ServerType As Type) As MarshalByRefObject
        Return Nothing
    End Function
End Class

<MyFunnyProxy> _
Class MyFunnyType
  Inherits ContextBoundObject
  Public Overrides Function ToString() As String
    If Me IsNot Nothing Then
      Return "Yes, I'm here!"
    Else
      Return "No, I'm really Nothing!"
    End If
  End Function
End Class

      

Note that the call ToString

is for commenting: when it is not, it is Nothing

crystallized as "expected".

(Before LinqPad based on Roslyn C # 6, I didn't see a similar effect in C #. That is, just annotating the call ToString

within the limits try

was enough myObj

to stay not null

. LinqPad (Beta) C # 6 does the same thing as VB .NET, requiring it to be removed try

in order not to crystallize null

.)

+1


source







All Articles