• Welcome to Valhalla Legends Archive.
 

[c#] using static member functions

Started by mentalCo., April 14, 2005, 10:55 AM

Previous topic - Next topic

mentalCo.

im having a problem with accessing functions throughout the rest of my project... heres an example:


public void AddChat(System.Drawing.Color Col, String Data){
        const uint ScrollToPosn = 99999;
        const uint SB_THUMBPOSITION = 0x4;
        const uint WM_VSCROLL = 0x115;
        rtbMain.SelectionStart = rtbMain.Text.Length;
        rtbMain.SelectionLength = 1;
        rtbMain.SelectionColor = Col;
        rtbMain.SelectedText = Data;
        rtbMain.SelectionStart = rtbMain.Text.Length;
}

private static void ODR_CallBack(string data, int len, int id)
{
        AddChat(rtbGreen, data + " with a len of " + len.ToString() + " with an id of " + id.ToString());
}


When i try to compile this part of the code gets the error


An object reference is required for the nonstatic field, method, or property 'UGBot.net__C_.Form1.AddChat(System.Drawing.Color, string)'


so how do i fix this?

MyndFyre

Make the function:

private static void ODR_CallBack(string data, int len, int id)

nonstatic.

You have to ask yourself: why is this function static?  Does it belong to an instance of the class or the class itself?  The answer is that it belongs to an instance, because it modifies instance-based data (in this case, your rich text box).
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

mentalCo.

it has to be static.  its a callback function.  Im passing a delegate into a c++ dll so the dll can execute functions in my c# app.  it works but i just dont know how to go about accessing nonstatic functions FROM a static function.

MyndFyre

Quote from: mentalCo. on April 15, 2005, 08:07 AM
it has to be static.  its a callback function.  Im passing a delegate into a c++ dll so the dll can execute functions in my c# app.  it works but i just dont know how to go about accessing nonstatic functions FROM a static function.

You'll need to hold references to the objects in some type of static member field (such as, perhaps, a hashtable or arraylist) that lets you identify which instance of your form you're accessing.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.