OOP Method Usage [closed] - c#

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I would like to ask if I have a class Person and Food,
and I want to have method of "eat".
Should I put eat method in class Person or method eaten in class Food ?
Thank you.

It depends that you say Person.Eat(food) or Food.EatenBy(person)
If you preffer Person.Eat(food)
public class Person
{
public void Eat(Food food)
{
//...
}
}
If you preffer Food.EatenBy(person)
public class Food
{
public void EatenBy(Person person)
{
//...
}
}

Related

Class per command or not - best practices [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 months ago.
Improve this question
Let's say that I have two commands CreateCarCommand and UpdateCarCommand and they obviously share some properties.
What is the best practice, should I create separate classes per command like
public class CreateCarCommand { ... }
public class UpdateCarCommand { ... }
or should UpdateCarCommand inherit from CreateCarCommand
public class CreateCarCommand { ... }
public class UpdateCarCommand : CarCommand { ... }
I would have two separate command classes - and if you have a lot of common properties, use a CarCommandBase base class and make CreateCarCommand and UpdateCarCommand inherit from that base class:
public class CarCommandBase
{
// define common properties and possibly methods here
}
public class CreateCarCommand : CarCommandBase
{
// custom code
}
public class UpdateCarCommand : CarCommandBase
{
// custom code
}

How can I create a reference that can contain both a stack and a list without needing a cast 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 1 year ago.
Improve this question
I'd like to be able to have a reference to a collection in a superclass that will need to contain either a stack or a list in a subclass, and I am having trouble understanding how to make that work, any ideas?
Something like:
public class Group
{
Collection<Human> group;
}
public class PeopleStack : Group
{
public PeopleStack()
{
this.group = new Stack<Human>();
}
}
public class Crowd : Group
{
public Crowd()
{
this.group = new List<Human>();
}
}
The Stack is defined as
public class Stack<T> :
System.Collections.Generic.IEnumerable<T>,
System.Collections.Generic.IReadOnlyCollection<T>,
System.Collections.ICollection
and the List is
public class List<T> :
System.Collections.Generic.ICollection<T>,
System.Collections.Generic.IEnumerable<T>,
System.Collections.Generic.IList<T>,
System.Collections.Generic.IReadOnlyCollection<T>,
System.Collections.Generic.IReadOnlyList<T>,
System.Collections.IList
So you can use one of the strong (generic) types IEnumerable<Human> or IReadOnlyCollection<Human> for group.
And of course that should be protected etc, but I suppose you know that.

Why would an empty class serve as the base class for several generic classes? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm looking over some scripts from a Unity project called 2D GameKit, & I'm trying to learn from piecing together the purpose of each. In one of them that serves as a mechanism for persistent storage, I noticed that several generic classes were derived from an empty base class. Why?
Here is a sample of the code:
public class Data
{
}
public class Data<T> : Data
{
public T value;
public Data(T value)
{
this.value = value;
}
}
public class Data<T0, T1> : Data
{
public T0 value0;
public T1 value1;
public Data(T0 value0, T1 value1)
{
this.value0 = value0;
this.value1 = value1;
}
}
So that you can check (know / compare) that a certain object is definitely data. If you don't have a base class, when you want to check if an object is certainly a type of data before processing it, you will need to compare with Data<T> AND Data<T0, T1> and also any other data types that is created in the future

C# method running with worksheet [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 3 years ago.
Improve this question
Maybe it is a beginner question but I can not seem to figure it out. How do I run the method as seen in the picture?
[[image]: https://i.stack.imgur.com/1QlnL.png][1]
By the looks of your picture your code for the ExcelRun may not be in the same class or linked to the class where you are trying to run it.
Example to get it to work:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ExcelRun();
Bogo.excelRun2();
Bogo n = new Bogo();
n.excelRun3();
}
private void ExcelRun()
{
MessageBox.Show("Have the Method Available in the same Class or callable from another.");
}
}
public class Bogo
{
public static void excelRun2()
{
MessageBox.Show("Hello there");
}
public void excelRun3()
{
MessageBox.Show("I am here");
}
}
This tutorial might help:
https://www.tutorialspoint.com/csharp/csharp_methods.htm

Alternative way to return concrete objects [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 7 years ago.
Improve this question
I have a function which takes an object parameter say entity and returns concrete objects like below. The concrete objects inherits from IEntity.
public IEntity GetEntity(object entity)
{
if(entity is A) { .... return new Customer(); }
else if(entity is B) {... return new Invoice(); }
.......
}
This obviously works but I was wondering if this is the best approach or is there any other alternative and recommended way from architecture perspective?
Thanks!
You could use generics:
public IEntity GetEntity<T>(T entity)
where T : IEntity, new()
{
return new T();
}

Categories

Resources