473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.
Dec 14 '10 #1
7 2400
GaryTexmo
1,501 Expert 1GB
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.
Dec 14 '10 #2
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.
Dec 14 '10 #3
GaryTexmo
1,501 Expert 1GB
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.Designer.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.
Dec 14 '10 #4
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:

Expand|Select|Wrap|Line Numbers
  1. private void InitializeComponent()
  2. {
  3.             this.splitContainer1 = new System.Windows.Forms.SplitContainer();
  4.             this.chartSelector1 = new GraphX.chartSelector();
  5.             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:
Expand|Select|Wrap|Line Numbers
  1. private void radioButton1_CheckedChanged(object sender, EventArgs e)
  2.         {
  3.  
  4.         }
3. In class chartSpace(which 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:
Expand|Select|Wrap|Line Numbers
  1. public void modifySeriesPrice(bool b)
  2.         {
  3.             this.series1.Enabled=b;
  4.         }
So my question is, how do I access the method modifySeriesPrice() which has been defined in the chartSpace class, through an event generated in the chartSelector class?
Dec 15 '10 #5
Christian Binder
218 Expert 100+
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. ChartTypeChanged.
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.
Dec 15 '10 #6
Alright Chris. I am new to this, could you help with how the event based approach can be implemented?
Dec 15 '10 #7
Christian Binder
218 Expert 100+
First take an enum for the possible selections
Expand|Select|Wrap|Line Numbers
  1. public enum ChartTypes {
  2.   CandleStick,
  3.   Bar,
  4.   Line
  5. }
  6.  
Then we create the EventArgs-class for the new event
This class only contains the selected chart-type
Expand|Select|Wrap|Line Numbers
  1. public class ChartTypeChangedEventArgs : EventArgs {
  2.   public ChartTypes ChartType { get; private set; }
  3.  
  4.   public ChartTypeChangedEventArgs(ChartTypes newType) {
  5.     ChartType = newType;
  6.   }
  7. }
  8.  
//in the chartSelector class we add a new event based on the ChartTypeChangedEventArgs-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)

Expand|Select|Wrap|Line Numbers
  1. public class chartSelector {
  2.   //the event
  3.   public event EventHandler<ChartTypeChangedEventArgs> ChartTypeChanged;
  4.  
  5.   private void radioButton1_CheckedChanged(object sender, EventArgs e) {
  6.     ChartTypes chartType = ChartTypes.CandleStick; //default
  7.     if(sender == radioButton2)
  8.       chartType = ChartTypes.Bar;
  9.     else if(sender == radioButton3)
  10.       chartType = ChartTypes.Line;
  11.  
  12.     //firing the event
  13.     if(ChartTypeChanged != null)
  14.       ChartTypeChanged(this, new ChartTypeChangedEventArgs(chartType));
  15.   }
  16. }
  17.  
in MainForm-class we attach to the event in constructor
Expand|Select|Wrap|Line Numbers
  1. public class MainForm {
  2.   public MainForm() {
  3.     InitializeComponent();
  4.  
  5.     chartSelector1.ChartTypeChanged += new ChartTypeChangedEventHandler(chartSelector1_ChartTypeCHanged);
  6.   }
  7.  
  8.   private void chartSelecto1_ChartTypeChanged(object sender, ChartTypeChangedEventArgs ea) {
  9.     chartSpace1.ChangeChartType(ea.ChartType);
  10.   }
  11. }
  12.  
at least there must be a ChangeChartType-method in chartSpace, which does the work when another ChartType should be displayed.
Expand|Select|Wrap|Line Numbers
  1. public class chartSpace {
  2.   public void ChangeChartType(ChartTypes newChartType) {
  3.     //here goes the code for displaying another chart-type
  4.   }
  5. }
  6.  
I hope this helps
Dec 15 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: damian birchler | last post by:
Hello there! I'm wondering if it is possible to automatically dynamically add methods to a class instance. For example, if there is a class Foo with one method named add_function and an...
3
by: Matik | last post by:
Hi, I alredy tried to search this problem in last posts, but I couldn't find the answer. I try to access via Extended SP the method in a dll. I registered the dll as a ExSP, with a name of...
25
by: cppaddict | last post by:
I'd like to know what goes on under the hood when methods return objects. Eg, I have a simple Point class with two members _x and _y. It's constructor, copy constructor, assignment operator and...
6
by: Andreas Schmitt | last post by:
Ok. I got the following problem. I created the following array and executed a function : CField* Spielfeld; CreatePlayField( Spielfeld ); *************************************************...
11
by: Jon Slaughter | last post by:
Are methods of a class created only once for all objects or for each object as its own copy of its methods? It seems that I can only access an object method's address using :: so if I have a...
1
by: Jon | last post by:
I have a class for complex numbers and an exponential function. This event handler does not reach Expz in debug mode: z = New Complex(CDbl(Real.Text), CDbl(Imag.Text)) Real.Text =...
6
by: Pohihihi | last post by:
When a thread calls a method (and/or that method calls another method), are they thread safe if the first method is controlled on access?
1
by: weston | last post by:
So, the following apparently works as a method of grafting a method onto an object (using the squarefree JS shell): obj = { x: 1, inc: null } result obj.inc = function () { this.x++ } result...
1
by: tuka | last post by:
Hi, I have 2 methods defined within a javascript objects that I would like to call in a third function within the same object. In the example below, func3 works well in FF but in IE7 it breaks....
1
by: Felix T. | last post by:
I have a class called Interval(type.ObjectType) that is supposed to mimic closed mathematical intervals. Right now, it has a lot of methods like this: def __add__(self,other): if type(other) in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.