Polymorphism in c# [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 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.

Related

Object oriented 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 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

How to make extension for all at once ? [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 6 years ago.
Improve this question
I have to make extensions for all types arrays of numbers : ints, longs, floats, doubles, etc. that can do some things.
Of course, I could do it for every type of integers, but that would look ugly.
double GetSomeValue (int[] array)
{
// some code
}
double GetSomeValue (double[] array)
{
// some code
}
etc, etc...
Is there any nice way to make in a short manner ?
Short answer is no. Depending on what you are actually trying to do you can use generic method declaration but it will accept broader set of types, cause there is no way to restrict generic method to accept only numeric types in C#.
Also an option would be using T4 Templates like here

Interface in C# what does it mean in this context [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
Was going through this code here
The code here confused me
private void sendMessage()
{
IConnectionFactory wlsConnectionFactory;
IQueue ordersQueue;
IDictionary<string, Object> environment;
IContext jndiContext;
In dot net and C# a I prefix means a Interface no ?
If the above statement is true what does this line mean for example
IQueue ordersQueue;
Would this program have worked if we had something like
Queue ordersQueue instead of IQueue ordersQueue ?
First of all, an I prefix implies an interface, but that's just a convention (albeit, a extreme widely-used convention)
IQueue ordersQueue;
means that ordersQueue can be assign any type which implements the IQueue interface.

Casting object to an interface return null [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 have an object from a class which returns an interface. but when I cast the object to that interface it returns null. what is the problem?
public partial class CompanyConfigurations : SgConfigurationControl, `IWizardControl`
CompanyConfigurations c
c as IWizardControl
The as operator returns null if the instance you want to cast, does not implement the specified interface, or does not inherit from the specified base class.
It's all clearly stated in MSDN: http://msdn.microsoft.com/en-us/library/cscsdfbt.aspx
Offcourse, if the instance that you want to cast is not assigned to an instance of a class (in other words, if the variable is null, then casting the variable using the as operator will return null as well offcourse.

How to get type of an object and then create array of objects with this type [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
class baker: unit {}
class killer: unit{}
void Round(unit first, unit second, byte fA, byte sA)
{
how to create array of "unit first" type objects?
}
I pass baker or killer as a parameter and I want to clone this particular class in method, but I don't understand how
For the simple approach :
if(first is baker)
{
// Create array of bakers
}
Or you can use Generics like so :
void Round<T>(T first, T second,...)
{
// Create the list of the type
new List<T>();
Or you can use :
Array.CreateInstance(first.GetType(), length);
In the worse case, you can use first.GetType() and use reflection to create an array.
But it's a bit more complicated

Categories