Calling methods on unnamed objects

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rishabh Chandra
    New Member
    • Dec 2010
    • 9

    Calling methods on unnamed objects

    Hello,

    We are building a graphing application in which we define the different components of the form as user controls.

    We have defined a number of series on the graph such as bar, candlesticks, line etc. and at a time, only one series is visible on the graph. The user selects a radio button and based on the selection, one of the series is made visible and the others are disabled.

    The problem we are facing is that we are unable to access the method defined for disabling/enabling a series in the class for creating the chart as there is no way to reference the object created at run-time for the chart.

    We are coding in Visual Studio.

    What can be done to call methods in run-time objects which cannot be referenced?

    Thanks.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Sorry, I'm a bit confused... why can't you access the object you created at run-time? Are you unable to keep a reference to it anywhere?

    I guess I don't understand why you can't reference the object.

    Comment

    • Rishabh Chandra
      New Member
      • Dec 2010
      • 9

      #3
      Thanks for the reply Gary. So the issue is that I've designed the UI in Visual Studio and the components are placed directly in the main form and called internally without me having created objects using hand-coding.

      In other words, the design-process is:
      1. I designed the two components, i.e., a set of checkboxes and the graph in two separate classes.
      2. Added the two components, to the main form.
      3. Run the app.

      In this process, I am unable to find the reference to the objects that have been created by the IDE through its internal code.

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        When you put a component on a form it still gets a named object created, it's just done by the designer. There should be a Name property on the object in designer. Alternatively you can check the form's designer code (MYFORMNAME.Des igner.cs) to see it.

        This should be a class member variable and you should be able to access it from anywhere within the class. Typically, the designer names it <classname><num > where classname is obviously the name of the type, but with a lower case starting letter, and the number is the current number of controls on the form. So if you made two Button objects on your form, by default, they would be called button1 and button2.

        Comment

        • Rishabh Chandra
          New Member
          • Dec 2010
          • 9

          #5
          I have managed to locate the code where the objects are created by the designer. However, I am still unclear as to how I should be referring to the methods which have been defined in some other object.

          Here are some pictures to clarify what I am trying:

          Picture 1: http://i53.tinypic.com/v8n0b7.jpg
          This is an image of the main form with the split container which will hold the two components, i.e. the chart selector and the actual chart area.

          Picture 2: http://i54.tinypic.com/103tu38.jpg
          This is an image of the MainForm with the two components, namely, the chartSelector user control and the chartSpace user control added.

          Here's a listing of the code, used in each of the classes:

          1. In class MainForm:

          Code:
          private void InitializeComponent()
          {
                      this.splitContainer1 = new System.Windows.Forms.SplitContainer();
                      this.chartSelector1 = new GraphX.chartSelector();
                      this.chartSpace1 = new GraphX.chartSpace();
          (GraphX is the solution name)

          2. In class chartSelector, the code that should be responding to the event of one of the radiobuttons changing is:
          Code:
          private void radioButton1_CheckedChanged(object sender, EventArgs e)
                  {
          
                  }
          3. In class chartSpace(whic h contains the actual chart), the code that can be used to activate or deactivate a series has been defined by the use of public functions such as:
          Code:
          public void modifySeriesPrice(bool b)
                  {
                      this.series1.Enabled=b;
                  }
          So my question is, how do I access the method modifySeriesPri ce() which has been defined in the chartSpace class, through an event generated in the chartSelector class?

          Comment

          • Christian Binder
            Recognized Expert New Member
            • Jan 2008
            • 218

            #6
            There are many possibilities how to do that. Some ideas for that:

            Event-based way:
            You create a new event in chartSelector-class, e.g. ChartTypeChange d.
            Then you attach to this event in MainForm and when the event occurs, you call a public method of chartSpace which changes the displayed chart type.

            non-event-based way:
            You create a new public property/variable in chartSelector class of type chartSpace. In constructor of MainForm, you assign the chartSpace to the chartSelector. In chartSelector-class you call a method of chartSpace-property which changes the displayed chart type.

            another way would be changing the control's modifiers to public, so MainForm would be able to access the RadioButtons and it's events.

            Comment

            • Rishabh Chandra
              New Member
              • Dec 2010
              • 9

              #7
              Alright Chris. I am new to this, could you help with how the event based approach can be implemented?

              Comment

              • Christian Binder
                Recognized Expert New Member
                • Jan 2008
                • 218

                #8
                First take an enum for the possible selections
                Code:
                public enum ChartTypes {
                  CandleStick,
                  Bar,
                  Line
                }
                Then we create the EventArgs-class for the new event
                This class only contains the selected chart-type
                Code:
                public class ChartTypeChangedEventArgs : EventArgs {
                  public ChartTypes ChartType { get; private set; }
                
                  public ChartTypeChangedEventArgs(ChartTypes newType) {
                    ChartType = newType;
                  }
                }
                //in the chartSelector class we add a new event based on the ChartTypeChange dEventArgs-class

                //I assume the three readionButtons are numbered from 1 to 3 and are calling the same event-handler (so there's only 1 handler for all three radios)

                Code:
                public class chartSelector {
                  //the event
                  public event EventHandler<ChartTypeChangedEventArgs> ChartTypeChanged;
                
                  private void radioButton1_CheckedChanged(object sender, EventArgs e) {
                    ChartTypes chartType = ChartTypes.CandleStick; //default
                    if(sender == radioButton2)
                      chartType = ChartTypes.Bar;
                    else if(sender == radioButton3)
                      chartType = ChartTypes.Line;
                
                    //firing the event
                    if(ChartTypeChanged != null)
                      ChartTypeChanged(this, new ChartTypeChangedEventArgs(chartType));
                  }
                }
                in MainForm-class we attach to the event in constructor
                Code:
                public class MainForm {
                  public MainForm() {
                    InitializeComponent();
                
                    chartSelector1.ChartTypeChanged += new ChartTypeChangedEventHandler(chartSelector1_ChartTypeCHanged);
                  }
                
                  private void chartSelecto1_ChartTypeChanged(object sender, ChartTypeChangedEventArgs ea) {
                    chartSpace1.ChangeChartType(ea.ChartType);
                  }
                }
                at least there must be a ChangeChartType-method in chartSpace, which does the work when another ChartType should be displayed.
                Code:
                public class chartSpace {
                  public void ChangeChartType(ChartTypes newChartType) {
                    //here goes the code for displaying another chart-type
                  }
                }
                I hope this helps

                Comment

                Working...