Valhalla Legends Forums Archive | .NET Platform | [c#] using static member functions

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

[code]
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());
}
[/code]

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

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

so how do i fix this?
April 14, 2005, 3:55 PM
Myndfyr
Make the function:
[code]
private static void ODR_CallBack(string data, int len, int id)
[/code]
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).
April 14, 2005, 11:40 PM
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.
April 15, 2005, 1:07 PM
Myndfyr
[quote author=mentalCo. link=topic=11277.msg108660#msg108660 date=1113570469]
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.
[/quote]

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.
April 15, 2005, 3:54 PM

Search