can't access class library functions?

dkim18
dkim18 used Ask the Experts™
on
Hi,

I have this vb.net application which I turned it into a class library.
I am trying to access this from the c# page.
I added the reference to it.
From my class, I added using my classlib name.
Then I tried to create an object but can't seem to create one.
Using Myclasslib;

Myclasslib.Mysubclass test = new Myclasslib.Mysubclass();

when I tried to use the method by test.Mymethod(), nothing shows up after test., any idea?
Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Is MyMethod defined as Public?
Try the code below, use Msgbox in Mymethod() to see whether it's being called successfully.

using Myclasslib;
public class Form1
{
      public Myclasslib.Mysubclass test = new Myclasslib.Mysubclass();
      private void Form1_Load(object sender, System.EventArgs e)
      {
             test.Mymethod();
      }
      public Form1()
      {
            Load += Form1_Load;
      }
}

Author

Commented:
OK.

When  I create a function and instantiated it inside the function, it showed.
Can you guess why it would do that?

using Myclasslib;
public class Form1
{
     
      private void Form1_Load(object sender, System.EventArgs e)
      {
            public Myclasslib.Mysubclass test = new Myclasslib.Mysubclass();
            test.Mymethod();
      }

}
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
When  I create a function and instantiated it inside the function, it showed.
Yes, you can only call methods from within other methods. You cannot call methods from outside of a method. For example, this is invalid:

using Myclasslib;

public class Form1
{
    public Myclasslib.Mysubclass test = new Myclasslib.Mysubclass();
    test.Mymethod();  // Invalid
     
    private void Form1_Load(object sender, System.EventArgs e)
    {

    }
}

Open in new window


It may be a bit confusing for you considering the initialization of the Mysubclass can occur outside of a method. This is normal though. Just know that any method calls you perform must occur in some other method.
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
For completeness, I suppose I should back pedal on what I mentioned previously. The statement, "just know that any method calls you perform must occur in some other method," is not entirely accurate. If you have a method that is marked as static, then you could call that method outside of a function. For example, the following method call is valid:

public class Form1
{
    public static string test = GetString();

    public static string GetString()
    {
        return "Hello World!";
    }
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial