I am trying to update a chart from within a module in vb.net;
Me.Chart1.Series.Add("Profile")
Me.Chart1.Series("Profile").Color = Color.LightGray
......
But I keep getting an error message: 'ME' not valid within a module.
Any workaround would be much appreciated please. Kind regards.
CodePudding user response:
Me
means the current Object or Instance of a Class. Me
works from within a Form because a Form is a Class AND the Chart is presumably contained by the Form so it is then found and acted upon. The Chart is NOT contained by the Module so Me
makes no sense here. Additionally, a Module is a special kind of class where everything (including the Module) is SHARED, which is why you also can't use Me
within it.
The solution is to PASS the Chart into the Module, either storing a reference to it in a Module member, or simply using it via a Parameter in a Method:
' ... in the Module ...
Public Sub UpdateChart(ByVal c As Chart)
c.Series.Add("Profile")
c.Series("Profile").Color = Color.LightGray
' ... other code that use "c" ...
End Sub
In the Form, you'd call it with:
UpdateChart(Me.Chart1)