wewa
wewa

Reputation: 1678

create namespace for an object in C#

How can I set the namespace for an object?

Now I have to use an object in the following way. MyFirstTestCase tc = new MyFirstTestCase();

MyFirstTestCase tc = new MyFirstTestCase();
tc.dothis();
tc.dothat();
// ...

I want to use the object in this way. MyFirstTestCase tc = new MyFirstTestCase();

MyFirstTestCase tc = new MyFirstTestCase();
using tc;
dothis();
dothat();
// ...

But this does not work. How can I do this?


To clarify what I mean.

// MyFirstTestCase is the class
// tc is the object of MyFirstTestCase
// and instead of:
tc.dothis();
// ... I want to use:
dothis();
// dothis is a method of tc

Upvotes: 0

Views: 1336

Answers (8)

Sameer Singh
Sameer Singh

Reputation: 1366

EDIT after question update:

So, you actually want to be able to call a method from your MyFirstTestCase class, but without qualifying it with an instance of your class?

Well, you can't do that.

Either:

var testCase = new MyTestCase(); // Create an instance of the MyTestCase class
testCase.DoThis(); // DoThis is a method defined in the MyTestCase class

or:

MyTestCase.DoThis(); // DoThis is a static method in the MyTestCase class

Information about the static keyword and how to define static members in a class.

Upvotes: -1

thmshd
thmshd

Reputation: 5847

What is the reason to do it like this? Are you tired of prefixing tc. from time to time? :) If you keep calling C# methods on a class that way more often, it might be a sign that your classes are just not well structured.

You might combine several public methods into one which then calls private methods within the class, or introduce something like "chaining", where usually void methods return their class instance with this instead:

Change this:

public class MyFirstTestCase {
    public void MyMethod1() {
       // Does some stuff
    }
    public void MyMethod2() {
       // Does some stuff
    }
}

Into:

public class MyFirstTestCase {
    public MyFirstTestCase MyMethod1() {
       // Does some stuff
        return this;
    }
    public MyFirstTestCase MyMethod2() {
       // Does some stuff
        return this;
    }
}

What you can do now is:

MyFirstTestCase tc = new MyFirstTestCase();
tc
    .MyMethod1()
    .MyMethod2()
    // etc.... 
;

Upvotes: 0

Mesh
Mesh

Reputation: 6442

WITH blocks are not part of C#, you can sort of get similar functionality, by Chaining the methods. Basically each method returns this. So you can write code like:

tc.DoThis().DoThat();

which can also be written

tc
.Dothis()
.DoThat();

Upvotes: 0

Ehsan
Ehsan

Reputation: 32681

instance methods need to be accessed via instance. So you cannot do that.

Upvotes: 0

Vadim
Vadim

Reputation: 2865

This is VB.Net feature, C# does not allow this. But take a look at this - http://www.codeproject.com/Tips/197548/C-equivalent-of-VB-s-With-keyword. The article proposes a simple way to get almost what you want.

Upvotes: 0

hallie
hallie

Reputation: 2845

Not possible. If you are working on the same class you can directly call the methods just like the way you want it to be. But on an instantiated object you must use the variable you created. In VB, it has a WITH keyword that is used to scope a part of a code, but C# does not have this.

WITH object
   .methodA()
   .methodB()
END WITH

Upvotes: 1

Ondrej Svejdar
Ondrej Svejdar

Reputation: 22054

You can't do this in C# - it is not VB.

Upvotes: 3

Lloyd
Lloyd

Reputation: 29668

Your class is usually already in a namespace. If not you can add one manually by just wrapping the whole thing in a namespace block:

namespace Something.Here
{

    public class MyClass
    {

    }

}

So you can then do:

Something.Here.MyClass my_class = new Something.Here.MyClass();

Upvotes: 0

Related Questions