Results 1 to 10 of 10

Thread: Non-Shared Member reason

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Non-Shared Member reason

    Hi,

    In a recent thread something similar to the following was posted as giving the error message "Reference to a non-shared member requires an object reference"

    formMain is the startup object

    VB Code:
    1. formMain.Text="Testing"

    As Me.Text="Testing" is OK and if formMain had been instantiated in a module, with the module Sub Main being the starrtup object, formMain.Text=Testing would have been OK, can anyone please explain why the error message arises? I have never attempted to refer to a form by it's own name from within itself, so I have never noticed this before.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Because as far as it is concerned there is no instantied form object named formMain.

    Its just not the way you do it. It a rule of OOP. Dont really know a better way to explain that.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    VB6 creates an instance of the form with the same name (essentially). However in .net it works different.

    When you are creating an instance of your form in your sub main, you might be doing this:


    Dim frmForm as new formMain

    then if you try this

    formMain.Text="lkj" you are trying to set the value of a class definition (formMain is the definition) not the actual instance.

    The instance is actually frmForm so you would call this instead:

    frmForm.Text="ljhkjh"

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    in VB6 it is called "Default instance" each form you make in VB has a default instance so you can just reference it by the name you called the form.. you can also dim a variable of that form as well

    in .NET the second option in the only option, as they have done away with default instances... it may seem odd at first, i know it did to me, but as soon as you work with it once, it becomes clear and you realize that there is no reason to have default instances

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Thanks guys, I follow that but what puzzles me is that when you use formMain as the startup object, arn't you getting an automatic instance of it? If not just what have you got?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    within the form itself you should always use the Me keyword.. that is why it is there.

  7. #7
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    Yes you do get an instance, the only difference is you dont have a reference to it in a global way, you would have to create it yourself. I have not seen a way of getting a global handle to it anyway.

    In the case described above you could on the statup of the form, set a reference in a module to be self (newly created form).

    I personally would use a sub main, as you can then add other functions, such as a global error handler. You can still call a form just like you would if it was the startup object, and you have a global reference to it. I can dig up and example if you like.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Many thanks. That has cleared the matter up. I will stick to using a Module Sub Main as the startup object, it's much cleaner.

    With regard to "Me" I can see you have to use it to reference form properties in the quoted situation, but I have always omitted it when referencing contained objects.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  9. #9
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    yeah it is purley optional... but it comes in handy when your form has like 100 controls and subs and you cant remember them all by name

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by kleinma
    yeah it is purley optional... but it comes in handy when your form has like 100 controls and subs and you cant remember them all by name
    AHHH! I guess you are referring to mass access to objects for clearing etc. OK.

    Where does your reference to Subs come in ?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width