Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C# Expert!

Join 244,276 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,158 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
6 Aug, 2007 - 06:20 AM
Post #1

D.I.C Head
**

Joined: 12 Feb, 2007
Posts: 150



Thanked: 14 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
+Quote Post


PsychoCoder
RE: Accessing Methods In Another Class
6 Aug, 2007 - 06:37 AM
Post #2

loves.Coding(this);
Group Icon

Joined: 26 Jul, 2007
Posts: 12,283



Thanked: 372 times
Dream Kudos: 10775
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

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
+Quote Post

killnine
RE: Accessing Methods In Another Class
6 Aug, 2007 - 06:46 AM
Post #3

D.I.C Head
**

Joined: 12 Feb, 2007
Posts: 150



Thanked: 14 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
+Quote Post

graeme bradbury
RE: Accessing Methods In Another Class
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
+Quote Post

killnine
RE: Accessing Methods In Another Class
6 Aug, 2007 - 09:56 AM
Post #5

D.I.C Head
**

Joined: 12 Feb, 2007
Posts: 150



Thanked: 14 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
+Quote Post

killnine
RE: Accessing Methods In Another Class
7 Aug, 2007 - 05:56 AM
Post #6

D.I.C Head
**

Joined: 12 Feb, 2007
Posts: 150



Thanked: 14 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
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 01:47PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month