If Me Is Nothing



  • Someone please tell me I'm not batshit-flipping-insane to think that "Me" in VB or "this" in C# can never ever possibly be null.

    Thanks.



  • It should never be null, in a properly designed application. I'm pretty sure it can end up being null through various bugs though.

    I don't know how VB compiles OO code, but a C++ function has a "this" parameter that references the object data. A function call such as "object.function(arg1);" would call objectType_function_param1(object, arg1);

    Now, if object is set to null, the "this" argument passed to the function implementation is null. What one *should* be doing is find where a function is called on an object that has already been destroyed, rather than trying to catch those calls and ignore them. This is a band-aid patch for a bug in another place.      



  • I'm fairly sure that in C# this situation is impossible, as far as I know the system should throw a null pointer exception before even executing any member function with an empty reference. C# won't allow you to work with raw pointers to objects, so bypassing the reference system doesn't really seem to be an option either.

    I have seen people checking for this==NULL in C++ before though, some lunatic thought that:

    bool MyObjectTemplate::InheritsFrom(ClassID classID)

    {

        if (this==NULL) return false;

        if (mClassID==classID) return true;

        return ParentClass::InheritsFrom(classID); 

    }

    Would be a perfect way to fix some of the crashes he'd been having. He also had to remove the virtual declaration to make it work. Sigh.



  • My original post was written hastily. I was leaving work.

    So, yeah, I understand how it can be possible in many languages that manage OOP by implicitly taking this as an argument (C++, Lua, Python sort of). I just can't imagine how, in a .NET language, this could ever be null when NullReferenceExceptions are thrown before an instance method is called on a null object.

    I'm 99.99% sure that it's impossible, but how can I positively prove it?



  • @djork said:

    I'm 99.99% sure that it's impossible, but how can I positively prove it?
     

    You can't prove that an MS product doesn't contain stupid bugs. 



  • @djork said:

    I'm 99.99% sure that it's impossible, but how can I positively prove it?

    @The Microsoft® Visual Basic® Language Specification, Version 8.0, 11.4.3 said:

    • The keyword <font face="Andale Mono, Courier New, Luxi Mono" color="blue">Me</font> represents the instance of the type containing the method or property accessor being executed.

    @Same, 2.4.7 said:

    <font face="Andale Mono, Courier New, Luxi Mono" color="blue">Nothing</font> is a special literal; it does not have a type and is convertible to all types in the type system, including type parameters. When converted to a particular type, it is the equivalent of the default value of that type.

    @Same, 7.1 said:

    A [i]null reference[/i] is a reference that refers to nothing; it is not possible to do anything with a null reference except assign it. <...> For a variable of a reference type, the default value is a null reference.

    QED?



  • @Spectre said:

    @djork said:

    I'm 99.99% sure that it's impossible, but how can I positively prove it?

    @The Microsoft® Visual Basic® Language Specification, Version 8.0, 11.4.3 said:

    • The keyword <font face="Andale Mono, Courier New, Luxi Mono" color="blue">Me</font> represents the instance of the type containing the method or property accessor being executed.

    So, with that in mind, in VB .NET I can safely assume that all of the "If Me Is Nothing" checks can be nuked. If the app crashes willy-nilly then I'll let Microsoft know.



  •  according to the language it will never be null.  So short of a compiler error or something you will be safe.



  •  <font color="#a9a9a9">579</font>:         <font color="#8b0000">public</font> Object CreateInstance(String typeName,
    <font color="#a9a9a9">580</font>:                                     bool ignoreCase,
    <font color="#a9a9a9">581</font>:                                     BindingFlags bindingAttr,
    <font color="#a9a9a9">582</font>:                                     Binder binder,
    <font color="#a9a9a9">583</font>:                                     Object[] args,
    <font color="#a9a9a9">584</font>:                                     CultureInfo culture,
    <font color="#a9a9a9">585</font>:                                     Object[] activationAttributes)
    <font color="#a9a9a9">586</font>:         {
    <font color="#a9a9a9">587</font>:             <font color="#008000">// jit does not check for that, so we should do it ...</font>
    <font color="#a9a9a9">588</font>:             <font color="#8b0000">if</font> (this == <font color="#8b0000">null</font>)
    <font color="#a9a9a9">589</font>:                 throw <font color="#8b0000">new</font> NullReferenceException();

    http://www.dotnet247.com/247reference/System/Reflection/Assembly/__rotor

    Checked with the released sources and reflector...


Log in to reply