0

I require a function/method that retrieves some information, and then use the retrieved information in another function.

Below is the principle of my information retriever function:

public class StuffIneed 
{
    public ArrayList MyPointList { get; set; }
    public string SomeName { get; set; }
    public TSG3D.Point SomeOtherPoint { get; set; }

    public void DataFetcher()
    {
        //Bunch of loops to check that we are looking at the correct object
        //After the loops I am able to state the value of SomeName and SomeOtherPoint, 
        //but I am unable to add the value of object.current value to the MyPointList as such:
        SomeName = TheObject.Current.Name;
        SomeOtherPoint = TheObject.Current.EndPoint;
        //above works, but below one doesn't
        MyPointList.Add(TheObject.Current.EndPoint);
    }
}

I can then use the information in my next function like so:

StuffIneed GatheredInfo = new StuffIneed();
GatheredInfo.StuffIneed();
MessageBox.Show(GatheredInfo.SomeOtherPoint.ToString());

But I am unable to figure out how to add the values into the ArrayList.

Ňuf
  • 6,027
  • 2
  • 23
  • 26
GOD
  • 3
  • 2
  • [Observer Pattern](https://www.tutorialspoint.com/design_pattern/observer_pattern.htm) may help you... – Hary Oct 05 '18 at 09:53
  • 1
    *above works, but below one doesn't* - please be more specific about what *doesn't work* mean - does your program throw `NullReferenceException`? Or program doesn't compile? Or do you receive some other error? – Ňuf Oct 05 '18 at 12:19
  • When the debugger reaches the line MyPointList.Add(TheObject.Current.EndPoint); It will give me an error: "Object reference not set to an instance of an object". My guess was that since I have stated that MyPointList is either get; or set; then I can't add into it – GOD Oct 05 '18 at 13:09
  • ArrayList, avoid it if possible: https://stackoverflow.com/a/5063253/4180382 – Ole EH Dufour Oct 05 '18 at 14:36

2 Answers2

1

To be able to add items to an ArrayList, you first have to create instance of ArrayList using new keyword and assign it to the MyPointList property. Otherwise no ArrayList instance exists, MyPointList property contains default value null ("no value") and hence the "Object reference not set to an instance of an object" exception is thrown. You can do this either using property initializer (property will be initialized during StuffIneed instance creation):

public ArrayList MyPointList { get; set; } = new ArrayList();

or in the DataFetcher method:

MyPointList = new ArrayList();
MyPointList.Add(TheObject.Current.EndPoint);

Note that this has nothing to do with MyPointList being declared as get or set - these just let you read (or write) instance of ArrayList to/from the MyPointList property, but they don't affect the ability to call methods (such as .Add()) on the object instance contained in this property.

See also What is a NullReferenceException, and how do I fix it?

Also note that ArrayList class is now mostly considered obsolete and List or similar generic classes are used instead, e.g.:

public List<TSG3D.Point> MyPointList { get; set; } = new List<TSG3D.Point>();

They offer better type safety and saves you the hassle of type-casting object when reading from it.

Ňuf
  • 6,027
  • 2
  • 23
  • 26
0

Your code does not show where you initialize the ArrayList. If you are encountering a NullReferenceException, then perhaps this is the issue?

If so, you could change:

public ArrayList MyPointList { get; set; }

to:

public ArrayList MyPointList { get; set; } = new ArrayList();
Paul Williams
  • 95
  • 2
  • 11