0

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
2
  • Have you tried prefixing the class name with the name of the Namespace something like NameSpace.ClassName tc = new NameSpace.ClassName Commented Aug 2, 2013 at 7:32
  • this is class MyFirstTestCase ? Commented Aug 2, 2013 at 7:33

8 Answers 8

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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();

Comments

0

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.

Comments

0

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

Comments

0

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();

Comments

0

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.... 
;

Comments

-1

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.

9 Comments

This does not work like supposed. I still have to write testCase.DoThis();
Don't think the OP is asking for this, he's asking for c# WITH equivalent. Your DoThis() and DoThat() are not methods of MyFirstTestCase, if they were they would need an instance or class in the call.
That's not how the using statement works. The using statement does not create a different scope, the only thing it does, is call the dispose-method on the object you specified after running the code-block. Disposing also has nothing to do with the OP's question. You're referring to the with-statement in VB
@Adrian as I mentioned in my answer, what the OP wants to do is not possible. I saw his edit after I had posted my original answer, and then edited mine to reflect that.
@Kenneth please read the question before the OP edited it. I think it was fair to assume that he was looking for the IDisposable pattern. After his edit though, I would agree that he meant something completely different
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.