473,387 Members | 1,664 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,387 software developers and data experts.

How to access form controls from "static" methods?

I'm a little confused with "static" methods and how to access other unstatic
methods. I'm a little new to C#.

I'm testing a callback routine within a DLL and the callback function
returns a string as one of the arguments. As shown below, I have no
problems showing the string in the Report() method in a messagebox or
console window, but how does one access a memo or label on a form from a
static method? I want to throw the strings from the callback function into
a memo control on the form.

namespace WindowsApplication1
{
public partial class Form1 : Form
{
private delegate void CallBack(int testInt, string testStr);

[DllImport("N:\\Temp\\Project1.dll")]
private static extern bool TestCallBack(CallBack x);

private void UpdateStatus(string status)
{
label1.Text = status;
}

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
CallBack myCallBack = new CallBack(Form1.Report);
TestCallBack(myCallBack);
}

private static void Report(int testInt, string testStr)
{
//Form1.UpdateStatus(testStr);
MessageBox.Show(string.Concat(testInt.ToString(), " - ",
testStr));
}
}
}

--
Thanks,
Jon E. Scott
Blue Orb Software
http://www.blueorbsoft.com

Jan 10 '07 #1
5 8865
Jon,

Make the callback method an instance member instead of a static member.
When creating the callback delegate use the syntax this.Report instead
of Form1.Report.

Brian

Jon E. Scott wrote:
I'm a little confused with "static" methods and how to access other unstatic
methods. I'm a little new to C#.

I'm testing a callback routine within a DLL and the callback function
returns a string as one of the arguments. As shown below, I have no
problems showing the string in the Report() method in a messagebox or
console window, but how does one access a memo or label on a form from a
static method? I want to throw the strings from the callback function into
a memo control on the form.

namespace WindowsApplication1
{
public partial class Form1 : Form
{
private delegate void CallBack(int testInt, string testStr);

[DllImport("N:\\Temp\\Project1.dll")]
private static extern bool TestCallBack(CallBack x);

private void UpdateStatus(string status)
{
label1.Text = status;
}

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
CallBack myCallBack = new CallBack(Form1.Report);
TestCallBack(myCallBack);
}

private static void Report(int testInt, string testStr)
{
//Form1.UpdateStatus(testStr);
MessageBox.Show(string.Concat(testInt.ToString(), " - ",
testStr));
}
}
}

--
Thanks,
Jon E. Scott
Blue Orb Software
http://www.blueorbsoft.com
Jan 10 '07 #2
One option would be to have a static member variable which holds a
reference to the form, then you would just need to beginInvoke on the
control on the form.

Jon E. Scott wrote:
I'm a little confused with "static" methods and how to access other unstatic
methods. I'm a little new to C#.

I'm testing a callback routine within a DLL and the callback function
returns a string as one of the arguments. As shown below, I have no
problems showing the string in the Report() method in a messagebox or
console window, but how does one access a memo or label on a form from a
static method? I want to throw the strings from the callback function into
a memo control on the form.

namespace WindowsApplication1
{
public partial class Form1 : Form
{
private delegate void CallBack(int testInt, string testStr);

[DllImport("N:\\Temp\\Project1.dll")]
private static extern bool TestCallBack(CallBack x);

private void UpdateStatus(string status)
{
label1.Text = status;
}

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
CallBack myCallBack = new CallBack(Form1.Report);
TestCallBack(myCallBack);
}

private static void Report(int testInt, string testStr)
{
//Form1.UpdateStatus(testStr);
MessageBox.Show(string.Concat(testInt.ToString(), " - ",
testStr));
}
}
}

--
Thanks,
Jon E. Scott
Blue Orb Software
http://www.blueorbsoft.com
Jan 10 '07 #3

DeveloperX wrote:
One option would be to have a static member variable which holds a
reference to the form, then you would just need to beginInvoke on the
control on the form.
If there is more than one of the same Form running then the callback
method would see the value of that variable as it was set by the last
created instance.

Also, it's not completely clear that BeginInvoke is needed. If
TestCallBack executes synchronously then the callback will be executing
on the UI thread as well. If TestCallBack executes asynchronously then
BeginInvoke would be needed.

Jan 10 '07 #4
Very true, I could of fleshed that out an mentioned InvokeRequired, etc
but it looked asynchronous (don't make me justify that ;)). I did
consider mentioning schemes for handling multiple instances of the
form, but assumed it wasn't a requirement based on the snippet :)

Brian Gideon wrote:
DeveloperX wrote:
One option would be to have a static member variable which holds a
reference to the form, then you would just need to beginInvoke on the
control on the form.

If there is more than one of the same Form running then the callback
method would see the value of that variable as it was set by the last
created instance.

Also, it's not completely clear that BeginInvoke is needed. If
TestCallBack executes synchronously then the callback will be executing
on the UI thread as well. If TestCallBack executes asynchronously then
BeginInvoke would be needed.
Jan 10 '07 #5
"Brian Gideon" wrote...
>
Make the callback method an instance member instead of a static member.
When creating the callback delegate use the syntax this.Report instead
of Form1.Report.
Knew it was something simple like that. Got it working. Thanks!

--
Thanks,
Jon E. Scott
Blue Orb Software
http://www.blueorbsoft.com
Jan 10 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: lawrence | last post by:
To call I would do something like: $headline = McSelectJustOneField::callDatastore("cbHeadline"); Is this the correct use of the static keyword, to implement a Singleton design?
3
by: Steven D'Aprano | last post by:
I've been doing a lot of reading about static methods in Python, and I'm not exactly sure what they are useful for or why they were introduced. Here is a typical description of them, this one...
9
by: Simon Harvey | last post by:
Hi all, In my project I have made a number of helper methods static. As I understand it, this will create the problem that multiple threads could access the static method at the same time and...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
9
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the...
15
by: dn | last post by:
I'm starting an n-tier application with an ASP.NET 2.0 presentation layer, a business layer, a data access layer, and a SQL Server 2005 database, and I have a question. In the business and data...
5
by: none | last post by:
I'd like to create a new static property in a class "hiding" the property present in a base class. Since this needs to happen at runtime I tried doing this via DynamicMethod. But obviously the...
12
by: chandu | last post by:
hello, i want to know usage of static methods in a class. is it advantageous or disadvantage to use more static methods in a class. thank u
3
by: mopont | last post by:
Hello I have a dll file built in C#, in this dll I have some static methods. Everything goes fine when I access those methods using C#, but when I try to access them from a classic asp file i'm...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.