Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,679 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,186 people online right now. Registration is fast and FREE... Join Now!




Accessing methods in another class

 
Reply to this topicStart new topic

Accessing methods in another class, Two custom classes, I can't call a public method from another.

killnine
post 6 Aug, 2007 - 06:20 AM
Post #1


D.I.C Head

**
Joined: 12 Feb, 2007
Posts: 107



Thanked 3 times
My Contributions


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?
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 6 Aug, 2007 - 06:37 AM
Post #2


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,933



Thanked 118 times

Dream Kudos: 8525

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


QUOTE(killnine @ 6 Aug, 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?


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

Go to the top of the page

killnine
post 6 Aug, 2007 - 06:46 AM
Post #3


D.I.C Head

**
Joined: 12 Feb, 2007
Posts: 107



Thanked 3 times
My Contributions


QUOTE(killnine @ 6 Aug, 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?



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:

CODE

                        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?


QUOTE(PsychoCoder @ 6 Aug, 2007 - 07:37 AM) *

QUOTE(killnine @ 6 Aug, 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?


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

CODE

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


Then, I have another method to STOP the timer

CODE

        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").


User is offlineProfile CardPM

Go to the top of the page

graeme bradbury
post 6 Aug, 2007 - 07:56 AM
Post #4


New D.I.C Head

*
Joined: 28 Jun, 2007
Posts: 10


My Contributions


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
CODE

graph1 t = new graph1();

...

t.graph1_timer_Stop();


User is offlineProfile CardPM

Go to the top of the page

killnine
post 6 Aug, 2007 - 09:56 AM
Post #5


D.I.C Head

**
Joined: 12 Feb, 2007
Posts: 107



Thanked 3 times
My Contributions


QUOTE(graeme bradbury @ 6 Aug, 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
CODE

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:



CODE

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...
User is offlineProfile CardPM

Go to the top of the page

killnine
post 7 Aug, 2007 - 05:56 AM
Post #6


D.I.C Head

**
Joined: 12 Feb, 2007
Posts: 107



Thanked 3 times
My Contributions


QUOTE(killnine @ 6 Aug, 2007 - 10:56 AM) *

QUOTE(graeme bradbury @ 6 Aug, 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
CODE

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:



CODE

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:

CODE

Form someForm;
someForm = new Graph1();



What I needed to do was this:

CODE

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.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 06:35AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month