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 years ago.
Improve this question
If a partial class has the same method name but different types I will get an error right?
public partial class Employee
{
public int sum()
{
}
}
public partial class Employee
{
public string sum()
{
}
}
Yes, it does:
'Employee' already defines a member called 'sum' with the same parameter types
Partial classes are just the same as regular class, except their definition is split in two sources. They have to comply to every rule regular classes have. That includes rules concerning the uniqueness of method signatures.
Related
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?
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 2 years ago.
Improve this question
I have different Models in my Asp.Net Core application. Is it recommended to do SQL queries inside IActionResult Action methods? or is a better to have a seperate class do all work in terms of fetching data for each of the models and return the result to IActionResult method where we can display Return View()?
I have a static Utility class. If I were to have a class for each model to fetch data I would need access to IHttpContextAccessor but i cant assign that in a static class. What type of class would be the way to go?
You can go for the repository pattern (https://martinfowler.com/eaaCatalog/repository.html), adding a generic repository (one single class) or a concrete repository (one per model).
Another solution would be using partial classes, to add data access logic in your model classes.
In example:
public partial class MyModel
{
public string Name { get; set; }
}
public partial class MyModel
{
//add data access here
}
Also to identify them, you could add an extension to model class files, like ".cs.da" (then you would have "MyModel.cs" and "MyModel.cs.da" files) and nest them using File nesting extension: https://marketplace.visualstudio.com/items?itemName=MadsKristensen.FileNesting
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 6 years ago.
Improve this question
I have a class called Promotion (model class). I want a promotion to have a type called Category where Category is its own model class I'm assuming where I would define all the types of categories such as fast food foot wear jewelry and so on. I'm not quite sure of how to go about this though so for example, my class called category is already a set thing but my class promotion is something where before I create it I need to set it with a category with the viable category options. thank you!
why don't you create a property called Categories of type Category in the Promotion?
enum Algorithms
{
FCFS,
SJF,
PRIORITY,
RR
}
class Process
{
public Algorithm algorithms{get;}
}
For the detailed difference please read the answer from this SO answer
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.
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.