How to address a function?

Usual apologies: I am a newbie, etc. Making my first steps in C#.

There is a form class: Form1:

namespace tapTCPports_1

{

      Public class Form1 : System.Windows.Forms.Form
      {
         .........
            private static void ConvIPtoLong (string strIP)

            {
               ...... // some code that compiles
            }
            public void button1_Clicked (Object sender, EventArgs e)
            {
               // al I want to do here is to use the function
               // (method) that I just defined: ConvIPtoLong
               // and I get an error no matter how I reference it
               this.ConvIPtoLong ("198.28.87.221")
               // the compiler cannot find it in the class Form1
            }
      }
}

I also tried to place a statement at the beginning of the class Form1:

private System.Windows.Forms.AccessibleObject convIPtoLong;

Then I get an error message pointing to the convIPtoLong function definition that this object has already been defined previously.

You see, I do not understand something very basic in C# and cannot go any further until it is resolved. Please, help.

Thanks.




Answer this question

How to address a function?

  • Robert Schlabbach

    The reason you can't do this is that you have your ConvIPToLong method declared as "static".  This makes it a class method and not an instance method.  The keyword "this" can only be used with instance constructors, instance methods, or instance accessors.  "this" can't see static members.

    You should be able to just call it from a method inside the class like this:
       ConvIPToLong("192.168.0.0")


  • le_beluet

    Hi,

    try:

    Form1.ConvIPtoLong ("198.28.87.221");

  • Dale17677

    Thanks, it worked. Big Smile



  • How to address a function?