Accessing methods in another class

Two custom classes, I can't call a public method from another.

Page 1 of 1

5 Replies - 76539 Views - Last Post: 07 August 2007 - 06:56 AM Rate Topic: -----

#1 killnine  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 161
  • Joined: 12-February 07

Accessing methods in another class

Post icon  Posted 06 August 2007 - 07:20 AM

Howdy guys,
Silly question but here goes.

I have two classes. One is called "signal_chart" and the other is called "graph1".

I have a timer in "graph1" and wish to start and stop the timer from within "signal_chart". I have created methods (public) to start and stop the timer and tried to call said methods within signal_chart, but to no avail (e.g. - graph1.start_Timer(), from within "signal_chart")

I have done this before between two custom methods (actually, I have done it in the same class).

What am I doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Accessing methods in another class

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Accessing methods in another class

Posted 06 August 2007 - 07:37 AM

View Postkillnine, on 6 Aug, 2007 - 07:20 AM, said:

Howdy guys,
Silly question but here goes.

I have two classes. One is called "signal_chart" and the other is called "graph1".

I have a timer in "graph1" and wish to start and stop the timer from within "signal_chart". I have created methods (public) to start and stop the timer and tried to call said methods within signal_chart, but to no avail (e.g. - graph1.start_Timer(), from within "signal_chart")

I have done this before between two custom methods (actually, I have done it in the same class).

What am I doing wrong?


Can you post the code for the start_Timer method, and how you're calling it in signal_chart please
Was This Post Helpful? 0
  • +
  • -

#3 killnine  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 161
  • Joined: 12-February 07

Re: Accessing methods in another class

Posted 06 August 2007 - 07:46 AM

View Postkillnine, on 6 Aug, 2007 - 07:20 AM, said:

Howdy guys,
Silly question but here goes.

I have two classes. One is called "signal_chart" and the other is called "graph1".

I have a timer in "graph1" and wish to start and stop the timer from within "signal_chart". I have created methods (public) to start and stop the timer and tried to call said methods within signal_chart, but to no avail (e.g. - graph1.start_Timer(), from within "signal_chart")

I have done this before between two custom methods (actually, I have done it in the same class).

What am I doing wrong?



My quick solution was to call the "namespace.class.method", and that would work.

However, I am creating multiple instances of the "graph1" form. Therefore, I need to modify specific timers of a particular instance.

for example:

						graph1_form = new graph1();
						graph1_form.Show(); 

//now I want to call a method within graph1_form, which is an 
//instance of the graph1 form

						graph1_form.graph1_Timer.stop();

//however, the above code does not work. Why?



View PostPsychoCoder, on 6 Aug, 2007 - 07:37 AM, said:

View Postkillnine, on 6 Aug, 2007 - 07:20 AM, said:

Howdy guys,
Silly question but here goes.

I have two classes. One is called "signal_chart" and the other is called "graph1".

I have a timer in "graph1" and wish to start and stop the timer from within "signal_chart". I have created methods (public) to start and stop the timer and tried to call said methods within signal_chart, but to no avail (e.g. - graph1.start_Timer(), from within "signal_chart")

I have done this before between two custom methods (actually, I have done it in the same class).

What am I doing wrong?


Can you post the code for the start_Timer method, and how you're calling it in signal_chart please



Sorry for doublepost.

When I load the "graph1" form, it starts the timer

				graph1_timer.Interval = 100;
				graph1_timer.Enabled = true;
				graph1_timer.Start();



Then, I have another method to STOP the timer

		public void graph1_timer_Stop(object sender, EventArgs e)
		{
			graph1_timer.Stop();
		}




What I want to do is create instances of this form ("graph1") and be able to access their timers from another class ("signal_chart").
Was This Post Helpful? 0
  • +
  • -

#4 graeme bradbury  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-June 07

Re: Accessing methods in another class

Posted 06 August 2007 - 08:56 AM

You've answered your own question.

Create the instance of graph1, keeping a reference.

Then to stop the timer call the method against the instance.

ie
graph1 t = new graph1();

...

t.graph1_timer_Stop();


Was This Post Helpful? 0
  • +
  • -

#5 killnine  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 161
  • Joined: 12-February 07

Re: Accessing methods in another class

Posted 06 August 2007 - 10:56 AM

View Postgraeme bradbury, on 6 Aug, 2007 - 08:56 AM, said:

You've answered your own question.

Create the instance of graph1, keeping a reference.

Then to stop the timer call the method against the instance.

ie
graph1 t = new graph1();

...

t.graph1_timer_Stop();




Well intellisense does not recognize graph1_timer_stop() as an acceptable method. I have found out why.

My FORM and my CLASS have the same name. Therefore, when I thought I was calling the class, I was calling the form. Here is an example:



namespace someNamespace
{
	public partial class graph1 : Form
	{
		private bool someVar1;
		private int	someVar2;		
 
		public graph1(int graphID)
		{
			InitializeComponent();		 
			graph1_Load(this, null);

			//init signal list
			for (int i = 0; i < graph1_signal_name_ids.Length / 2; i++)
			{
				if(null != graph1_signal_name_ids[i, 0])
				{  
					graph1_sig_text.Items.Add(graph1_signal_name_ids[i, 0]);
					graph1_sig_value.Items.Add(" ");
				}
			}
		}
.
.
.





So what was happening was I was calling an instance of graph1, but it was actually calling the METHOD graph1(), and not creating an instance of the class.

I am still working on a solution...
Was This Post Helpful? 0
  • +
  • -

#6 killnine  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 161
  • Joined: 12-February 07

Re: Accessing methods in another class

Posted 07 August 2007 - 06:56 AM

View Postkillnine, on 6 Aug, 2007 - 10:56 AM, said:

View Postgraeme bradbury, on 6 Aug, 2007 - 08:56 AM, said:

You've answered your own question.

Create the instance of graph1, keeping a reference.

Then to stop the timer call the method against the instance.

ie
graph1 t = new graph1();

...

t.graph1_timer_Stop();




Well intellisense does not recognize graph1_timer_stop() as an acceptable method. I have found out why.

My FORM and my CLASS have the same name. Therefore, when I thought I was calling the class, I was calling the form. Here is an example:



namespace someNamespace
{
	public partial class graph1 : Form
	{
		private bool someVar1;
		private int	someVar2;		
 
		public graph1(int graphID)
		{
			InitializeComponent();		 
			graph1_Load(this, null);

			//init signal list
			for (int i = 0; i < graph1_signal_name_ids.Length / 2; i++)
			{
				if(null != graph1_signal_name_ids[i, 0])
				{  
					graph1_sig_text.Items.Add(graph1_signal_name_ids[i, 0]);
					graph1_sig_value.Items.Add(" ");
				}
			}
		}
.
.
.





So what was happening was I was calling an instance of graph1, but it was actually calling the METHOD graph1(), and not creating an instance of the class.

I am still working on a solution...




OKAY, I GOT IT.

The issue was, I had created instances of a FORM, not of the GRAPH1 class. so instead of doing something like this:

Form someForm;
someForm = new Graph1();




What I needed to do was this:

Graph1 someForm;
someForm = new Graph1();



Or something to that effect. Basically, none of my methods were showing up and it was because intellisense was looking to the methods in FORM class not in GRAPH1 class. Yikes. Stupid me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1