Understanding shared methods

  1. 5 years ago

    Beatrix W

    31 May 2015 Pre-Release Testers, Third Party Store Europe (Germany)

    Doing a bit of refactoring. I've got a class ("MailFields") that holds some data. So far I've always passed the class around, made a copy and got the class back. Since I'm usually dealing with GBs of data I want to eliminate the copying of the data. But I can't seem to understand shared methods.

    The class is simple. The normal properties were converted to shared ones:

    Shared ContentDescription As String
    Shared ContentDisposition As String
    Shared ContentDispositionCharset As string

    and so on. Between parsing runs I need to reset the data with

    Sub Reset()
      
      ContentDescription = ""
      ContentDisposition = ""
      ContentDispositionCharset = ""
     'and so on
      
    End Sub

    But now Xojo barfs at

    MailFields.Reset

    with the error message "Static reference to instance method". At the moment I want to keep this as class because I'm not sure how I want to extend this class. Most likely a module would work at the moment, too.

    How do I need to use the shared method here?

    Xojo 2015r22, Mac OS 10.10.2.

    Beatrix, it seems the root problem is the understanding of Shared vs. Instance. Here is a blog post that I found. It is for VB.NET , but it really doesn't matter, it could have been for any number of languages, the Shared vs. Instance is what is needed here.

    http://blog.giffordconsulting.com/2006/08/object-oriented-overview-and-shared-vs.html

    Once you understand the difference of Shared and Instance methods, your error will make sense and I'm sure you will come to an easy solution based on your needs.

  2. Eli O

    is not verified 31 May 2015 Europe (Berlin, Germany)

    What you are trying to do is calling an instance method like a class method (=shared method). Reset is an instance method, so it must be called on an instance of MailFields:

    aMailFieldsInstance.Reset()
  3. Will S

    31 May 2015 Santa Cruz 

    Make Reset a Shared method. ?

  4. Beatrix W

    31 May 2015 Pre-Release Testers, Third Party Store Europe (Germany)

    Reset already is a shared method. That's what makes this so confusing.

  5. Eli O

    is not verified 31 May 2015 Europe (Berlin, Germany)

    The error message is telling you what I described: Static reference to instance method. You are accessing an instance method in a shared method without having prepended the method with the instance in your code.

    By the way, your code above doesn't say Shared Sub Reset, it says Sub Reset which makes it an instance method.

  6. Will S

    31 May 2015 Santa Cruz 

    So this isn't true?

    @Beatrix W Reset already is a shared method.

  7. Jeremy C

    31 May 2015 Pre-Release Testers, Xojo Pro Answer Ohio, USA
    Edited 5 years ago

    Beatrix, it seems the root problem is the understanding of Shared vs. Instance. Here is a blog post that I found. It is for VB.NET , but it really doesn't matter, it could have been for any number of languages, the Shared vs. Instance is what is needed here.

    http://blog.giffordconsulting.com/2006/08/object-oriented-overview-and-shared-vs.html

    Once you understand the difference of Shared and Instance methods, your error will make sense and I'm sure you will come to an easy solution based on your needs.

  8. Beatrix W

    31 May 2015 Pre-Release Testers, Third Party Store Europe (Germany)

    @Will and Eli: I copied the method from the old version of my code. But the error message occurred for Reset being Static.

    @Jeremy: thanks, that helped. "Shared" is a not very clear name and I will simply use a module for now.

or Sign Up to reply!