How to create exclusive classes?

  • Thread starter Thread starter DabblerNL
  • Start date Start date
D

DabblerNL

I have the following Methods:

public SecondResult DoSomething()
{
FirstResult firstResult=GetFirstResult();
SecondResult secondResult=GetSecondResult(firstResult);
return secondResult;
}

private FirstResult GetFirstResult()
{...}

private SecondResult(FirstResult fr)
{...}

The Method GetSecondResult should only and only then be called after
GetFirstResult has been called. To achieve that I want that the only way to
obtain a SecondResult object is through calling the FirstResult method.

This compares to the IOrderedEnumerable<T> type that is returned from the
OrderBy and OrderByDescending(IEnumerable<T>) methods, so that it is ensured
that the ThenBy and ThenByDescending(IOrderedEnumerable<T>) methods can only
be used after application of a OrderBy method.

How do I achieve this?
 
Hello Pete,

Thanks for you answer. I am not having a problem really. My code works fine.
I am just exploring different programming techniques.
What I want is to create code that ensures that some methods can only be
called after some other methods have been called first, because these
previous methods assing some values to the properties of the class that the
later methods work on.

The .Net Methods OrderBy and ThenBy do exactly that.: ThenBy works on a
IOrderedEnumerable<T>, which is the return value of the OrderBy Method. As
far as I know there is no other way to create a IOrderedEnumerable<T> than
vby using OrderBy. I like that approach but are wondering how it is done.

I will try your suggestion of moving the GetSecondResult Method to the
FirstResult class. It will not make the code necessarily more readible though.

My real code is:

public List<Seating> SeatCellList(List<Cell> cells)
{
cells.Seat();//will set some properties in the cells in the List
return AdaptToSeatingList(cells);//reads the properties that are change
and makes a List<Seating>
}

private List<Seating> AdaptToSeatingList(List<Cell> cells)
{
var result=new List<Seating>
// do some work on the cells
return result;
}

As you see the AdaptToSeatingList Method accepts a List<Cell> but it
requiers that some properties of the elements of this list are assigned to.
This can be solved by doing some error checking inside the AdaptToSeatingList
method, but this is ugly. I would rather have the Seat() (extension) method
of the List<Cell> return a List<SeatedCells> and have the AdaptToSeatingList
method accept this type as an argument. This is not difficult to achieve, but
I would like to ensure that the only way to get a List<SeatedCells> is
through the List<Cell>.Seat() method.
How??


Peter Duniho said:
I have the following Methods:

public SecondResult DoSomething()
{
FirstResult firstResult=GetFirstResult();
SecondResult secondResult=GetSecondResult(firstResult);
return secondResult;
}

private FirstResult GetFirstResult()
{...}

private SecondResult(FirstResult fr)
{...}

The Method GetSecondResult should only and only then be called after
GetFirstResult has been called. To achieve that I want that the only way
to
obtain a SecondResult object is through calling the FirstResult method.

[...]
How do I achieve this?

What problem are you having? The above seems reasonably straightforward.
What specific question do you have?

By the way, rather than having two methods in the same class, one which
operates on the instantiated class (i.e. "FirstResult"), it might make
sense for the second method to actually be an instance method of the
instantiated class. Functionally it would be basically the same,
depending on how you construct the class "FirstResult", but it may provide
better encapsulation of your intermediate results (I can't say for sure,
because there are so few actual details in your proposal...but I think it
probably would).

Pete
 
Back
Top