1

I created a class with properties like this

public class Dependencies
{
    public string issueID { get; set; }
    public string status { get; set; }
    public int dependencyCheck { get; set; }
}

Now I created a method where i can use these properties.

private static void prepareIssuesList(string IssueKey, string JIRAtoken)
{
    Dependencies dpObj = new Dependencies();
    List<Dependencies> listObj = new List<Dependencies>();
    dpObj.issueID = IssueKey;
    dpObj.status = "open";
    dpObj.dependencyCheck = 0;
    listObj.Add(dpObj);

}

Now my question is, how to change the dependencyCheck property value. The prepareIssuesList() can called for multiple times. So i am adding multiple objects to Dependencies class. At certain point of time i want to change the dependencyCheck property value. How to do this. I think need to use the Linq to C#. ICan any one have any solution for this one?

Jon Senchyna
  • 7,867
  • 2
  • 26
  • 46
Searcher
  • 1,845
  • 9
  • 32
  • 45

1 Answers1

4

I would do something along these lines:

public class Dependency
{
     public string IssueId { get; set; }
     public string Status { get; set; }
     public int DependencyCheck { get; set; }
}

public class DependencyManager
{
     public DependencyManager()
     {
          this.Dependencies = new List<Dependency>();
     }         

     public List<Dependency> Dependencies { get; private set; }

     public void AddDependency(string issueId)
     {
         var dep = new Dependency();
         dep.IssueId = issueId;
         dep.Status = "open";
         dep.DependencyCheck = 0;

         this.Dependencies.Add(dep);
     }

     public void SetDependencyCheck(string issueId, int value)
     {
         var dep = this.FindDependencyByIssueId(issueId);
         dep.DependencyCheck = value;
     }

     public Dependency FindDependencyByIssueId(string issueId)
     {
         var dep = this.Dependencies.FirstOrDefault(d => d.IssueId.Equals(issueId));
         if(dep == null) throw new ArgumentException("Dependency not found", "issueId");
         return dep;
     }
}

Then somewhere in your code you could do:

var mgr = new DependencyManager();
mgr.AddDependency("ABC123");
mgr.AddDependency("ABC456");

//... some other stuff that makes sense

mgr.SetDependecyCheck("ABC123", 42);
Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
  • +1 Although it would be worthy explaining why OP's code didn't work (because a new `List` instance was being created on each call). – vgru Jun 13 '12 at 12:30
  • @Klaus.. Thanks. Really I am feeling so much shy that i dont know that how to code like this.. really awesome. – Searcher Jun 13 '12 at 12:41
  • @YSSS You are welcome. Just keep practicing and asking questions here and your skills will definitely improve with time. – Klaus Byskov Pedersen Jun 13 '12 at 12:44
  • @KlausByskovHoffmann the code what you present here also not storing values. – Searcher Jun 13 '12 at 13:16
  • @YSSS hehe ok. I think you are not aware of when values are stored and when not. As long as the `mgr` instance is in scope, it holds all the values you put into it. When the `mgr` runs out of scope it gets "lost". Since I don't know the structure of your code, I cannot recommend how to best store the instance. – Klaus Byskov Pedersen Jun 13 '12 at 13:20
  • @YSSS DependencyManager keeps a Property called `Dependencies` that is used to store the `Dependency` objects. – Jon Senchyna Jun 13 '12 at 13:21
  • What I done is, I kept in another function your second block of code. Means var mgr = new DepedencyManager().. etc Kept in prepareIssueList function. I am calling twice in my program. Old values are not retaining. – Searcher Jun 13 '12 at 13:28
  • @YSSS that is not the right thing to do. You need to have **only one** instance of the `DependencyManager`. You can't just create a new one and expect it to keep the values (how would that work, anyway?). In the class that has your `prepareIssueList` function make a property/field that holds the instance of the `DependencyManager` and use that instance in the function. The easiest way would be: `private DepencyManager myManager = new DependecyManager();`. Now in `prepareIssueList` you use `myManager`. – Klaus Byskov Pedersen Jun 13 '12 at 13:32
  • Where to wirte the private DependancyManager myManger = new DependencyManager(); ? In DependancyManager class? – Searcher Jun 13 '12 at 13:37
  • If possible can you make changes to the your suggested code related to your suggestion!! – Searcher Jun 13 '12 at 13:38
  • @YSSS **In the class that has your prepareIssueList function** – Klaus Byskov Pedersen Jun 13 '12 at 13:43