Make a method both static and instance [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 10 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have a class called Foo and a static method - Bar (Foo instance) - inside it. But instead of only being able to call it like Foo.Bar(new Foo()), I want to also be able to say new Foo().Bar();
TL;DR I have a static method in class Foo which has a parameter of type Foo and I want to also be able to call it as if it was a non-static method.
This is all for testing purposes. I know this'd be terrible code design.

Create both methods, and let one redirect to other, where main logic is:
public class Foo
{
// Called by Foo.Bar(new Foo());
public static void Bar(Foo instance)
{
// logic here
}
// Called by new Foo().Bar();
public void Bar()
{
Foo.Bar(this);
}
}

Related

is it bad practice that a child class does not use all parent's elements? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 days ago.
This post was edited and submitted for review 8 days ago.
Improve this question
My question is related to inheritance it.
Let say we have a class called Animal. All animals share some behaviour like walking, pooping, hunting, etc. After few days I decided to add a Fish class that extend Animal class. But here fish swims but does not hunt or walk.
Is it OK to use only some parts of the base class? Because Fish class can still calls walk and hunt but does not execute the behaviour, I guess.
I could go around the problem where i make a function called behaviour in base class and let the child class override it.
Like this:
public class Animal
{
public virtual void behaviour() {
Console.WriteLine("//do something");
}
}
public class GoldFish : Animal
{
public override void behaviour() {
Console.WriteLine("goldfish swims");
}
}
Any suggestion on how should go about this with best practices of programming?

What is the difference between PrivateObject vs PrivateType in C#? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I was looking for approaches that could be used to access a static class which contains private static methods to unit test.
I came across PrivateObject and PrivateType, I do not understand what the difference between them is, and how you would decided to use either one.
BusinessLogic
public static Class BarLogic(){
private static void FooBar(){
//do something here
}
}
API
public class FooService(){
BarLogic.FooBar();
}
The difference is whether you're accessing otherwise inaccessible instance members or static members.
In the example below, you would use PrivateObject to access _someInstanceField, and PrivateType to access _someStaticField.
public class SomeObjectWithInAccessibleMembers
{
private int _someInstanceField;
private static int _someStaticField;
}
Given that your test was in a separate assembly from this class (and therefore unable to to see its internal members) you could do this:
var privateObject = new PrivateObject(new SomeObjectWithInAccessibleMembers());
privateObject.SetField("_someInstanceField", 1);
Assert.AreEqual(1, privateObject.GetField("_someInstanceField"));
var privateType = new PrivateType(typeof(SomeObjectWithInAccessibleMembers));
privateType.SetStaticField("_someStaticField", 1);
Assert.AreEqual(1, privateType.GetStaticField("_someStaticField"));
I've never used this. I didn't even know about it. My first reaction is that it couples the test to the inner implementation of the class instead of testing its public methods. Then it couples it some more by using strings for member names, so the tests could break just by changing a member name.
On the other hand, I've worked in projects where a) I want to add unit tests, but b) I can't refactor to make it more testable. It's not my first choice but I might have a use for this. Thanks!

Polymorphism in c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a problem with a class that is to spawn instances containing one out of about 30 objects of different types (I call it out_out_of_many_types_of_subcontract in the code snippet below).
class supercontract
{
void supercontract (float date, one_out_of_many_types_of_subcontract subcontract)
{
stuff....
}
}
Is there any way of declaring a semi-generic variable or must I (1) resort to polymorphism between constructors or (2) casting an object as a certain type with a block of (else)if clauses?
Cheers!
I would make them all implement a single interface.
interface IContract{}
class AContract: IContract {...}
Even if the interface is empty you can limit what types could be passed to your method.

Unexpected character error in C# dot net application [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a solution created in C#. i have a piece of code in it base.\u002Ector();. When i try to build the application from Visual Studio I am getting the Following error.
Unexpected character '\u002E'
Remove \u002E and you're done!
002E is a full stop or just a dot ('.').
Your code must look like:
base.ctor();
It could happen because of transmitting a program source through different websites/programs with unsupported or broken encoding.
Moreover, ctor is a shortcut for constructor. Make sure you call constructor of base class correctly.
public class Animal
{
public Animal()
{
}
}
public class Cat : Animal
{
// next line will be converted to
// base.ctor() by compiler
public class Cat() : base()
{
}
}
Try to remove base.ctor(); in order to make your class work. I think it should work because your base class has no parameters in constructor.

Main function usage [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to understand usage of Main function in C#.
I am trying to write separate method (ex. Method1) to do the action such as:
Asking question to user (what is your name).. return some response ("Glad to meet you, XXX").
I want to put all the functionality such as Asking question and returning response on a separate method (Method1) instead of using Main.
Then, how can I pass step from Main (starting position) to go to that Method1?
I guess my question is how do I call Method1 from Main?
What kind of information do I have in Main method then?
Do I just put like this way?
static void Main(string[] args)
{
Method1();
Console.ReadKey();
}
The Main function in C# is what's known as the entry point of your program. If your program was a book and the computer wanted to start reading it, it has to begin from somewhere - that's where Main comes in. It's the method that gets called to get your program going.
As you might have noticed, main is a static method:
public static void Main(){
// Your code here
}
Without going into too much detail, a static method can only call other static methods, or create an instance of something. So if you wanted Main to call something else, the two options are like this:
public static void Main(){
Method1();
}
// Method1 is also static:
public static void Method1(){
Console.WriteLine("Hello!");
}
Or alternatively by creating an instance:
public class MyProgram{
public static void Main(){
// Create an instance of this class:
MyProgram program=new MyProgram();
// And call Method1 on the instance:
program.Method1();
}
// Notice how method1 is not static this time:
public void Method1(){
Console.WriteLine("Hello!");
}
}

Categories