Results 1 to 11 of 11

Thread: [RESOLVED] Reference to a non-shared member requires an object reference

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    300

    Resolved [RESOLVED] Reference to a non-shared member requires an object reference

    Just some BS code to illustrate the problem I'm having. The issue is with the change() sub under Color_Class (line 13). It gives the error:
    "Reference to a non-shared member requires an object reference"

    VB Code:
    1. Public Class Form1
    2.     Friend WithEvents Button1 As myButton_Class
    3.     Friend WithEvents Button2 As myButton_Class
    4.  
    5.     Class myButton_Class
    6.         Inherits Button
    7.         Public myColor As New Color_Class
    8.  
    9.         Class Color_Class
    10.             Private _color As Color
    11.  
    12.             Sub change()
    13.                 BackColor = _color
    14.             End Sub
    15.  
    16.             Property color() As Color
    17.                 Get
    18.                     color = _color
    19.                 End Get
    20.                 Set(ByVal value As Color)
    21.                     _color = value
    22.                 End Set
    23.             End Property
    24.         End Class
    25.  
    26.     End Class
    27.  
    28.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    29.         Button1.myColor.color = Color.Aqua
    30.         Button1.myColor.change()
    31.         Button2.myColor.color = Color.Blue
    32.         Button2.myColor.change()
    33.     End Sub
    34.  
    35.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    36.         Button1 = New myButton_Class
    37.         Button1.Location = New System.Drawing.Point(100, 75)
    38.         Button1.Size = New System.Drawing.Size(75, 25)
    39.         Button1.Name = "Button1"
    40.         Button1.Text = "Button1"
    41.         Controls.Add(Button1)
    42.  
    43.         Button2 = New myButton_Class
    44.         Button2.Location = New System.Drawing.Point(100, 125)
    45.         Button2.Size = New System.Drawing.Size(75, 25)
    46.         Button2.Name = "Button2"
    47.         Button2.Text = "Button2"
    48.         Controls.Add(Button2)
    49.     End Sub
    50. End Class

    How can I fix this error? I want to create "smart" properties that perform functions on themselves, but each has to be it's own instance since I can't share the storage... Clearly I'm missing something fundamental in my understanding of shared and instantiated...

    Help?

    -Andy.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Reference to a non-shared member requires an object reference

    Which line is giving the error?
    My usual boring signature: Nothing

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reference to a non-shared member requires an object reference

    The problem is that you are referring to BackColor inside the Color_Class class but BackColor is not a member of that class. It's a member of the myButton_Class class but the fact that Color_Class is declared inside myButton_Class doesn't inherently give it access to an instance of myButton_Class. If you did this:
    vb.net Code:
    1. Dim obj As New myButton_Class.Color_Class
    2.  
    3. obj.change()
    how would you expect that to work? Inside that change method you would be trying to set the BackColor property but the BackColor property of what? That myButton_Class.Color_Class object has no relationship to any myButton_Class object so how can it set its BackColor?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reference to a non-shared member requires an object reference

    I think you want something like this:
    vb.net Code:
    1. Class myButton_Class
    2.     Inherits Button
    3.  
    4.     Public myColor As New Color_Class(Me)
    5.  
    6.     Class Color_Class
    7.  
    8.         Private _button As myButton_Class
    9.         Private _color As Color
    10.  
    11.         Sub change()
    12.             Me._button.BackColor = _color
    13.         End Sub
    14.  
    15.         Property color() As Color
    16.             Get
    17.                 color = _color
    18.             End Get
    19.             Set(ByVal value As Color)
    20.                 _color = value
    21.             End Set
    22.         End Property
    23.  
    24.         Public Sub New(ByVal button As myButton_Class)
    25.             Me._button = button
    26.         End Sub
    27.  
    28.     End Class
    29.  
    30. End Class
    Now the Color_Class object always has a reference to a myButton_Class object so it can set the BackColor property of that object. You now have an instance on which to access the non-Shared member.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Reference to a non-shared member requires an object reference

    D'Oh!!!
    My usual boring signature: Nothing

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reference to a non-shared member requires an object reference

    I have to say though, from the example that you've provided I can't see any advantage to defining a nested class over just putting that behaviour in the control itself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    300

    Re: Reference to a non-shared member requires an object reference

    Quote Originally Posted by jmcilhinney View Post
    I think you want something like this:
    Public myColor As New Color_Class(Me)
    Thanks for the fast answer. What's the significance of (Me) in that declaration?

    I have to say though, from the example that you've provided I can't see any advantage to defining a nested class over just putting that behaviour in the control itself.
    Well, I'm probably mis-guided, but I like the idea of a class that contains many sub-classes.

    Like:

    vb.net Code:
    1. Class Machine
    2.   Public Name As String
    3.  
    4.   Class Modes
    5.      Private _currentMode as Byte
    6.      Public Const OFF as Byte = 0
    7.      Public Const ON as Byte = 1
    8.      Public Const Standby as Byte = 2
    9.  
    10.      Property Mode() as byte
    11.          Get
    12.              Mode = _currentMode
    13.          End Get
    14.          Set(ByVal value as byte)
    15.              If value >=0 and value <=2 Then
    16.                  _currentMode = value
    17.              End If
    18.       End Property
    19.  
    20.       Sub ChangeMode()
    21.            Machine.Operate(_currentMode)
    22.       End Sub
    23.   End Class
    24. End Class
    25. .
    26. .
    27. .
    28. Public myMachine as new Machine
    29. myMachine.Name = "Herbert"
    30. myMachine.Mode = myMachine.Modes.ON
    31. myMachine.Mode.ChangeMode

    I guess you're suggesting that I don't bother?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reference to a non-shared member requires an object reference

    Quote Originally Posted by Meestor_X View Post
    What's the significance of (Me) in that declaration?
    Me is always a reference to the current instance. An object can pass itself as an argument just the same as it can pass any other object.
    Quote Originally Posted by Meestor_X View Post
    I guess you're suggesting that I don't bother?
    That's not a design I'd go for. You might want to look at enumerations for a start, for defining the values.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    300

    Re: Reference to a non-shared member requires an object reference

    Ok. Thanks for your help. I'll try it without the nesting and see if I still like it. If not, you've given me the direction on how to accomplish the nested classes properly.

    I don't really understand why the inner class (within the outer class) cannot see the properties of the outer class, but it seems that you are correct!

    Thanks very much.

    -Andy.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reference to a non-shared member requires an object reference

    Quote Originally Posted by Meestor_X View Post
    Ok. Thanks for your help. I'll try it without the nesting and see if I still like it. If not, you've given me the direction on how to accomplish the nested classes properly.

    I don't really understand why the inner class (within the outer class) cannot see the properties of the outer class, but it seems that you are correct!

    Thanks very much.

    -Andy.
    Your are getting confused between types and objects. The fact that a class is defined inside another class has no impact whatsoever on what the nested class can or can't see. The reason you would declare one public type inside another is to indicate that the nested type shouldn't exist in isolation from the outer type. Look at this code:
    vb.net Code:
    1. Public Class Outer
    2.  
    3.     Public OuterData As String
    4.  
    5.     Public Class Inner
    6.  
    7.         Public InnerData As String
    8.  
    9.     End Class
    10.  
    11. End Class
    Now look at this code:
    vb.net Code:
    1. Dim inner As New Outer.Inner
    That creates an instance of the Inner class but there is no Outer object. Why should that Inner object inherently be able to access members of the Outer class when it can exist without an Outer object existing?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    300

    Re: Reference to a non-shared member requires an object reference

    As always, you "Da Man". Simple concept that I never quite got my head around...

    Thanks again.

    -Andy.

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