|
-
Jun 28th, 2009, 09:06 PM
#1
Thread Starter
Hyperactive Member
[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:
Public Class Form1
Friend WithEvents Button1 As myButton_Class
Friend WithEvents Button2 As myButton_Class
Class myButton_Class
Inherits Button
Public myColor As New Color_Class
Class Color_Class
Private _color As Color
Sub change()
BackColor = _color
End Sub
Property color() As Color
Get
color = _color
End Get
Set(ByVal value As Color)
_color = value
End Set
End Property
End Class
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.myColor.color = Color.Aqua
Button1.myColor.change()
Button2.myColor.color = Color.Blue
Button2.myColor.change()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1 = New myButton_Class
Button1.Location = New System.Drawing.Point(100, 75)
Button1.Size = New System.Drawing.Size(75, 25)
Button1.Name = "Button1"
Button1.Text = "Button1"
Controls.Add(Button1)
Button2 = New myButton_Class
Button2.Location = New System.Drawing.Point(100, 125)
Button2.Size = New System.Drawing.Size(75, 25)
Button2.Name = "Button2"
Button2.Text = "Button2"
Controls.Add(Button2)
End Sub
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.
-
Jun 28th, 2009, 09:22 PM
#2
Re: Reference to a non-shared member requires an object reference
Which line is giving the error?
My usual boring signature: Nothing
 
-
Jun 28th, 2009, 09:28 PM
#3
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:
Dim obj As New myButton_Class.Color_Class 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?
-
Jun 28th, 2009, 09:32 PM
#4
Re: Reference to a non-shared member requires an object reference
I think you want something like this:
vb.net Code:
Class myButton_Class Inherits Button Public myColor As New Color_Class(Me) Class Color_Class Private _button As myButton_Class Private _color As Color Sub change() Me._button.BackColor = _color End Sub Property color() As Color Get color = _color End Get Set(ByVal value As Color) _color = value End Set End Property Public Sub New(ByVal button As myButton_Class) Me._button = button End Sub End Class 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.
-
Jun 28th, 2009, 09:39 PM
#5
Re: Reference to a non-shared member requires an object reference
My usual boring signature: Nothing
 
-
Jun 28th, 2009, 09:52 PM
#6
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.
-
Jun 29th, 2009, 02:15 AM
#7
Thread Starter
Hyperactive Member
Re: Reference to a non-shared member requires an object reference
 Originally Posted by jmcilhinney
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:
Class Machine
Public Name As String
Class Modes
Private _currentMode as Byte
Public Const OFF as Byte = 0
Public Const ON as Byte = 1
Public Const Standby as Byte = 2
Property Mode() as byte
Get
Mode = _currentMode
End Get
Set(ByVal value as byte)
If value >=0 and value <=2 Then
_currentMode = value
End If
End Property
Sub ChangeMode()
Machine.Operate(_currentMode)
End Sub
End Class
End Class
.
.
.
Public myMachine as new Machine
myMachine.Name = "Herbert"
myMachine.Mode = myMachine.Modes.ON
myMachine.Mode.ChangeMode
I guess you're suggesting that I don't bother?
-
Jun 29th, 2009, 02:25 AM
#8
Re: Reference to a non-shared member requires an object reference
 Originally Posted by Meestor_X
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.
 Originally Posted by Meestor_X
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.
-
Jun 29th, 2009, 02:39 AM
#9
Thread Starter
Hyperactive Member
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.
-
Jun 29th, 2009, 02:54 AM
#10
Re: Reference to a non-shared member requires an object reference
 Originally Posted by Meestor_X
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:
Public Class Outer
Public OuterData As String
Public Class Inner
Public InnerData As String
End Class
End Class
Now look at this code:
vb.net Code:
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?
-
Jun 29th, 2009, 12:21 PM
#11
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|