views:

30

answers:

1

Hi. My question is basically this:

If I set dynamic data from one class, can I access that same data from another class? Below is the rough pseudocode of what I am trying to do:

Public Class Person
{
  public string ID { get; set; }
  public string Name { get; set; }

}

And, I do the below:

Public Class SomeClass
{

   private void SomeMethod()
   {

        List<Person> p = new List<Person>();

         loop {
           p.Add(id[i], name[i]);
           Timer t = new Timer(); 
          t.Interval = 1000;
         }
}

Can I access the values set in SomeClass from SomeOtherClass such that:

Public SomeOtherClass
{

  private List<Person> SomeOtherMethod(string id)
  {
      // HERE, THE RESPONSE VALUES MAY CHANGE BASED ON
      // WHERE IN THE LOOP SomeClass.SomeMethod HAS SET
      // THE VALUES IN Person.

      var query = from p in Person
                  where p.ID == id
                  select p;

      return query.ToList();

  }

}

Thanks for your thoughts...

A: 

You can access the members (ID, Name) for those properties, since you declared them as public.

When you say "Person", you're referring to a Type - the Person type.

In order to access a member of a specific Person, you need to use an instance of Person.

Person thePersonInstance = new Person(5, "Joe");
thePersonInstance.ID = 3; // This is fine, since it's public

However, in your example, you would need to provide a specific collection of "Person" instances to your method in SomeOtherClass. Something like:

Public SomeOtherClass
{

    // You need to provide some collection of Person instances!
    public List<Person> SomeOtherMethod(string id, IEnumerable<Person> people)
    {
        var query = from p in people
              where p.ID == id
              select p;

        return query.ToList();
    }
}

You could then use this somewhere else, like:

void SomeMethod()
{
      List<Person> people = new List<Person>();
      people.Add(new Person(1, "Name1");
      people.Add(new Person(2, "Name2");

      SomeOtherClass other = new SomeOtherClass();
      List<Person> filteredPeople = other.SomeOtherMethod(1);
 }
Reed Copsey
Your post was very helpful, thanks. However, I am still hung up on a particular part - is it possible to access data set in class 1 from the context of class 2? I have an page.aspx.cs codebehind that is setting the data in a loop: List<Person> person = new List<Person>();for (i = 0; i <= Request.Name.Count - 1; i++){ person.Add(new Person(Request.ID[i], Request.Name[i]));}Then, whenever my Web Service is called, I want to be able to return the list of people data at whatever point in the loop the data is called:List<Person> IPeopleService.GetPeople(){ //I am still lost here}
Code Sherpa
Is it possible to do this? Set the data from my code behind and then access the data "as it is being set" from another class to get a progress update of sorts? Or, is there a better way to tackle this? Your help is much appreciated.
Code Sherpa
Well, if you're trying to access it "as its being set", ie: at exactly the same time from a different thread, it is possible, but you'll need to look into using a locking mechanism to protect your data. You'll need to have a shared service, though, so you can get access to the list - but as long as you do, it should work fine.
Reed Copsey
OK, thanks again Reed. I'll read up on locking and shared services. In the mean time, I am also going to try a repost of this question as think I could have asked it better. Thanks again for your help!
Code Sherpa