bRaNdOn
bRaNdOn

Reputation: 1082

Accessing Method in different class Same name space without instantiating the class Reference Only

is there a way to access a method in defferent class with same name space

Sample Codes

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApplication1;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public void Test()
        {

        }
    }
}


namespace WindowsFormsApplication1
{
    public class TestClass
    {
        public void Test2()
        {

        }
    }
}

I wabt to access test2() method inside Test() method they are in different class but in same namespace i want to access it without instantiating the TestClass class like this

TestClass test = new TestClass();
test.Test2();

i dont want to do it that way is there a way?

NOTE " I am avoiding to use a static Method" thank you for the answering this

Upvotes: 0

Views: 55

Answers (1)

H.Wolper
H.Wolper

Reputation: 709

AS far as I know, there is no way to do it. But why would you want to?

To access Test() you're instantiating a class (Form1). You want then to use a different method but not instantiate the class. So what would be the meaning of running it? What would be its context?

Possible solutions:

  1. Create an extension method (since you'll be extending Form, I don't recommend it)
  2. Move Test2() into Form1
  3. Move test() into TestClass, and instantiate it only once.
  4. Use a static class after all

Upvotes: 1

Related Questions