C# namespace in a class - c#

I have basic knowledge of classes and methods. I can make classes, and define methods for them:
myClass.awesome("test"); (example)
But I saw a class have the following method:
anotherClass.something.methodName(arguments);
How do I create a method that has additional namespace(s). I tried:
public Class test
{
namespace subname
{
public void Test()
{
return;
}
}
//also i tried:
public void lol.Test()
{
return;
}
}
But they both say that its not to be done like that, how do it correctly, so I can order/group my methods better?
Please don't ask why, or give a alternative, I just want to make a class that has this kind of methods(Class.sub.Method() or Class.sub.sub....sub.Method())
Thank you for reading my question, and possibly giving ans answer :)

i saw a class have the following method:
anotherClass.something.methodName(arguments);
The method is not from the class anotherClass instead it is from the class of object something.
The class anotherClass has a field/property something which is of another class type, that class has the method methodName

What you think you have seen is not correct actually. It can be any of the followings:
anotherClass is namespace, something is class, methodName is static method
anotherClass is an object, something is a property of anotherClass, methodName is a method

If you want to group your methods you should consider using static classes like this:
public class Test
{
public static class InnerGroup
{
public static void Method1() { }
}
public static class AnotherInnerGroup
{
public static void Method2() { }
}
}
or class properties like this:
public class Test
{
public class InnerGroup
{
public static void Method1() { }
}
public class AnotherInnerGroup
{
public static void Method2() { }
}
public InnerGroup InnerGroup { get; set; }
public AnotherInnerGroup AnotherInnerGroup { get; set; }
public Test()
{
InnerGroup = new InnerGroup();
AnotherInnerGroup= new AnotherInnerGroup();
}
}
Hope you understood.

Related

Making abstract methods available to sub classes

I want to be able to use the method RaiseMessage that exists on the Abstract Class AgentBase, on other classes through the program.
public class MNyTestAgent: AgentBase
{
RaiseMessage("hey", "hey")
var a = new Foo();
}
public class Foo
{
public Foo()
{
RaiseMessage("","") -<< how do i use it here
}
}
First of all, your code isn't valid C#.
Second of all, if you want to have a method accessible everywhere else, you probably want a public static. To implement a public static method you need to first reconsider your life choices, as doing so in an Agent class looks like bad design and a violation of OOP principles. If you still decide that you need it, something like this should work:
public abstract class AgentBase
{
public static RaiseMessage(string title, string message)
{
// Implementation.
}
}
public class MNyTestAgent: AgentBase
{
public MNyTestAgent()
{
AgentBase.RaiseMessage("hey", "hey");
}
}
public class Foo
{
public Foo()
{
AgentBase.RaiseMessage("hey", "hey");
}
}
Could maybe this help?
public class MNyTestAgent: AgentBase
{
RaiseMessage('hey', 'hey')
var a = new Foo(this);
}
public class Foo
{
public Foo()
{
}
public Foo(AgentBase base)
{
base.RaiseMessage('','') -<< how do i use it here
}
}

How to call a method from same class in another method in C#?

Now I am working with c# code that looks something like this
public class MyClass
{
public static void Method1()
{
//Do something
}
public void Method2()
{
//Do something
Method1();
}
}
Now what if I replace the code as:
public class MyClass
{
public static void Method1()
{
//Do something
}
public void Method2()
{
//Do something
MyClass.Method1();
}
}
Now what is the difference in above 2 representations.
Is it the same or does it show some different working.
Any help is appreciated.
The second is just a longer version of the previous. If you are in the same class as the static method, you do not need to specify the class name, you can, but you don't need to (much like specifying this for instance methods).
Inside the class there is no difference but the difference comes when you try to invoke them from outside the class. For instance method you need a instance of your class whereas for static method that's not required. But inside your class you can just say
public class MyClass
{
public static void Method1()
{
//Do something
}
public void Method2()
{
Method1(); //you don't have to qualify it
}
}

Using expression tree call a method with the same name on 2 unrelated classes

Suppose I have a base class like the following
public abstract class BaseHelloWorld<T> where T : BaseEntity
{
public abstract IEnumerable<T> DoSomething();
}
and another like
public class BaseEntity
{
public abstract void DoSomethingInPayload();
}
Then I have 4 classes like:
public class Payload1 : BaseEntity
{
public override void DoSomethingInPayload()
{
Console.Write("Hello world");
}
}
public class Class1 : BaseHelloWorld<Payload1>
{
public override IEnumerable<Payload1> DoSomething()
{
return new List<Payload1> { };
}
}
public class Payload2 : BaseEntity
{
public override void DoSomethingInPayload()
{
Console.Write("Goodbye world");
}
}
public class Class2 : BaseHelloWorld<Payload2>
{
public override IEnumerable<Payload2> DoSomething()
{
return new List<Payload2>() { };
}
}
Although I have shown code here, suppose these where third party libraries that I don't have code for and I want to extend them. What I want to do is to be able to create a single extension method that will allow me to call the DoSomethingInPayload() method on the payload class similar to
public static void CallDoSomething<T>(this BaseHelloWorld<T> theClass) where T: BaseEntity
{
theClass.DoSomethingInPayload();
}
Obviously this will not work so I started looking at expression trees. My reading suggests this is possible to do with expression trees but I cant figure it out. After hours of trying and getting nowhere I am unsure if my theory is correct. Therefore could you please tell me:
A) Is it possible to do this with expression trees
B) If so how would I do it?
Thanks in advance
Your own example is not working because T is a different type in the input and return parameters.
Indeed, as #AleksAdreev mentioned, you could simply try:
public static IEnumerable<T> CallDoSomething<T>(this BaseHelloWorld<T> theClass)
{
return theClass.DoSomething();
}
Which can then be called as follows:
var someClass = new Class2();
var someResult = someClass.CallDoSomething();

is it possible to use a method from a different class within the same assembly?

I am new to c#. I am curious if I can do something like this:
namespace Test {
public class SomeClass
{
public static double someMethod()
{
//do something
}
}
public class AnotherClass
{
someMethod();
}
}
I tried something similar to this and it gave me a "does not exist in current context" error. I tried referring it as SomeClass.someMethod() but still gives me the same error.
if the method is static you are able to call it the following way SomeClass.someMethod()
public class AnotherClass
{
public void anotherMethod ()
{
SomeClass.someMethod();
}
}
If you want to class contains methods and fields of other class you should use inheritance
public class SomeClass
{
public static double someMethod()
{
//do something
}
}
public class AnotherClass : SomeClass
{
}

Is it possible to "inherit" tests with xUnit.net?

I have a concrete class called EventManager and a subclass called ScheduledEventManager. I would like ScheduledEventManager to have to pass the same tests as EventManager plus a few additional ones. Is this possible with xUnit.net?
EDIT: I just realized that my case is a little more complicated than this. I'm using nested classes to keep my tests more organized. Example:
public class EventManagerTests
{
public class WhenAnEventIsFired
{
[Fact]
void ItNotifiesSubscribers()
{
// Perform the test
}
}
}
public class ScheduledEventManagerTests
{
// How to I inherit the above tests since they are in nested classes?
}
It seems to me that this is not possible, but maybe one of you geniuses knows something I don't.
Yes You can:
public abstract class EventManagerTests
{
protected IEventManager _ev;
protected EventManagerTests(IEventManager ev)
{
_ev = ev;
}
[Fact]
public void SharedTest()
{
// Perform _ev test
}
}
public class ScheduledEventManagerTests : EventManagerTests
{
public ScheduledEventManagerTests():base(new ScheduledEventManager())
{
}
// It will inherit tests from the base abstract class
}
public class UnScheduledEventManagerTests : EventManagerTests
{
public UnScheduledEventManagerTests():base(new UnScheduledEventManager())
{
}
// It will inherit tests from the base abstract class
}
Create a parameterized test that takes an instance of your base class as the SUT, and invoke the test with an instance of the sub class. Here's a (contrived) example using NUnit, which results in one passing and one failing test:
public class Foo
{
public virtual int DoSomething()
{
return 10;
}
}
public class Bar : Foo
{
public override int DoSomething()
{
return 9;
}
}
[TestFixture]
public class Tests
{
private Foo[] _foos = { new Foo(), new Bar() };
[Test]
[TestCaseSource("_foos")]
public void When_DoSomething_Is_Invoked_Then_A_Power_Of_Ten_Is_Returned(Foo sut)
{
Assert.That(sut.DoSomething() % 10, Is.EqualTo(0));
}
}

Categories

Resources