heyred
heyred

Reputation: 2051

Returning a static List<>

Can anyone please help me

I have a static ListHelper class that I want to use to store a number of different types of lists (lists of type float, Vector2 etc). Then I can just call this class' methods when I want access to a particular List<>

For instance, I have a class called CalculationHelper that I will use to do some calculations. Once a particular calculation is done, I add the values to one of my lists in ListHelper.

But how do I return the values from this static ListHelper class to e.g. Game1 class?

Here is my code to show what I am trying to do. First, I call the CalculationHelper method to do some calculations from Game1

CalculationHelper.ForceIntersections(LineList[i], LineList[i + 1]);

Then in CalculationHelper I do the calculations and add the result to my VanishingPoint List in ListHelper class

public static class CalculationHelper
{
    public static void ForceIntersections(Line line1,Line line2,List<Sprite> VanishingPointIntersectionList, String greenCirlce)
    {
        Vector2 intersectionPoint;
        // Do some calculations

        // Add value to ListHelper
        ListHelper.IntersectionPoint = intersectionPoint;
    }       
}

Then in ListHelper add the intersectionPoint to the List

public static class ListHelper
{
    static List<Vector2> VanishingPoint = new List<Vector2>();

    static Vector2 intersectionPoint;
    public static Vector2 IntersectionPoint 
    {
        get
        {
            // How do I return the List?
            foreach (var value in VanishingPoint)
            {
                 return value;          // NOT WORKING
            }        
        }
        set
        {
            intersectionPoint = value;
            VanishingPoint.Add(intersectionPoint);
        }
    }
}

Can anyone please explain how this is done, and maybe even if this is the best way to do it?

Upvotes: 0

Views: 9273

Answers (2)

Chris Gessler
Chris Gessler

Reputation: 23123

You need some sort of key. The key could be embedded in the Vector2 object itself or you could switch to a Dictionary<CalculationTypes, Vector2>. This would allow you to specifiy which item is a VanishingPoint.

To get a specific object from the list, you'll have to use the Find method:

return VanishingPoint.Find(v => v.SpecificType == SomeTypes.IntersectionPoint);

or in your case:

foreach(Vector2 v in VanishingPoint)
{
  if(v.SpecificType == SomeTypes.IntersectionPoint)
  {
    return v;
  }
}
return null;

But if you're wanting to return the entire list, it will require a bit of refactoring:

public static class CalculationHelper   
{   
    public static void ForceIntersections(Line line1,Line line2,List<Sprite> VanishingPointIntersectionList, String greenCirlce)   
    {   
        Vector2 intersectionPoint;   
        // Do some calculations   

        // Add value to ListHelper   
        ListHelper.VanishingPoints.Add(intersectionPoint);   
    }          
}  

public static class ListHelper    
{    
    static List<Vector2> VanishingPoints = new List<Vector2>();    

    public static List<Vector2> VanishingPoints 
    {    
        get    
        {    
            return VanishingPoints;          
        }      
    }    
}  

Upvotes: 1

walther
walther

Reputation: 13600

This:

public static Vector2 IntersectionPoint 

means you're going to return something of type Vector2. If you want to return a list, you need to do it like this:

public static List<Vector2> IntersectionPoint 
{
        get
        {
            List<Vector2> temp;
            foreach (var value in VanishingPoint)
            {
                 temp.Add(value);         
            }     
            return temp;   
        }
        set
        {
            intersectionPoint = value;
            VanishingPoint.Add(intersectionPoint);
        }
    }

HOWEVER, your code is very bad. You're clearly working with some class variables, that are dependent on each other in the same instance. Forget "static" and create a normal class OR, if you want to be sure there will be only one instance, use singleton pattern.

Upvotes: 1

Related Questions