0

I'm trying to get the following to work:
A class called Caption, where I populate a List - adding Items to the Class list I then want to reference the list of class values, in a lookup method

public class Caption
{
    readonly CaptionKey _CaptionKey;    //Enum list
    readonly string _Description;

    public Caption(CaptionKey captionKey, string description)
    {
        _CaptionKey = captionKey;
        _Description = description;
    }

    public CaptionKey CaptionKey { get { return _CaptionKey; } }

    public string Description { get { return _Description; } }

}

Here is the class that creates the class list

public class InitCaptions

    public static List<Caption> _Captions = new List<Caption>();

// the class access I need

public static string LookupCaption( CaptionKey )
{
   //?  How to return the description for 

}

The problem is with referencing the list of classes from another class and process.

I can see the values in the debugger are there- System.Collections.Generic.List<MyNamespace.Controllers.Captions> I'm just not sure how to reference it properly.

I should add - this is a MVC solution - so the List is created in the application startup, but the reference call is done from a report - using ReportViewer. the List shows in the code using Intellisense, but when run - the List is not there.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
PBrent
  • 1
  • 2
  • 1
    use a Dictionary. – Daniel A. White Mar 27 '20 at 15:05
  • Does this answer your question? [How can I loop through a List and grab each item?](https://stackoverflow.com/questions/18863187/how-can-i-loop-through-a-listt-and-grab-each-item) – oleksa Mar 27 '20 at 16:39
  • Can't use a Dictionary - the class has methods I left out in my question to simply it. I'm not sure if an interface or delegate can be used – PBrent Mar 27 '20 at 17:22
  • To help with answering your question it would be helpful with more of the relevant code, e.g include the type CaptionKey (even though the _comment_ suggests it's an enum). It could also be useful to state _why_ you want to do, what you want to do. E.g. why do you want to access that static list from _another_ static class? – Torben Koch Pløen Mar 28 '20 at 17:22
  • Yes, I'm sorry I'm new to this. The class for Captions is created at application startup since it's global and required for all reports. The problem is when the ReportViewer RDLC file is called from the application. In the Report XML, there's a custom code that does a lookup on the caption via a method call. Inside this external call , it cannot reference the method using the class (though it can find the enum value) I'm thinking based on what I see the thread is dropping the instance of the List - so I'm not sure how to do this - getting closer though.. Thanks for any help – PBrent Mar 30 '20 at 20:11

1 Answers1

0

Assuming a Simple Dictionary<> won't suit your needs (for reasons you have not specified), then to find and return an item from a List you can use the List.Find method, which takes a delegate and is used like this:

Caption foundItem = _Captions.Find(delegate (Caption obj) { return obj.Description.equals("Find this Description"); });

If there is more than one, then it will return the first it finds.

There is also the FindAll(....) version of this which will return a new list of all matches items.

jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51
  • Yes, a Dictionary wont work because there's more actual complexity in the Caption class. The problem is before it gets to the 'Find'. I can't reference List 'Captions' instantiated in the InitCaptions class. Can you explain where a delegate fits in? – PBrent Mar 27 '20 at 16:51
  • Maybe these will help: https://glucoloen.wordpress.com/2012/05/19/c-tips-using-delegate-in-list-find-predicate/ https://stackoverflow.com/questions/242012/how-do-i-form-a-good-predicate-delegate-to-find-something-in-my-listt – jason.kaisersmith Mar 27 '20 at 17:31
  • If you need to access your List, then you need to keep it in reference somewhere. Maybe as a Static, maybe in a Manager or Coordinator class. – jason.kaisersmith Mar 27 '20 at 17:32
  • Thanks - In Debugger I get this error value in the Watch when it hits the reference to the List _Captions Code : Phrase phrase = null; Caption _caption InitCaptions._Caption.FirstOrDefault(p => p.CaptionKey == key ); Error: 'InitCaptions._Captions' threw an exception of type 'System.TypeInitializationException' System.Collections.Generic.List {System.TypeInitializationException} I checked and the class list is already static. Not sure how to setup a manager or coordinator class – PBrent Mar 27 '20 at 19:19