Link to home
Start Free TrialLog in
Avatar of u2envy1
u2envy1

asked on

Generic List<> as a property

I have a Type List<string>. How would I call the clear method on the List if it was a property.
This is how my property looks
List<string> _RecieveData = new List<string>();//Data Returned from the clock
        public List<string> RecieveData
        {
            get
            {
                return _RecieveData;
            }
            set
            {
                _RecieveData.Clear();
            }
        }

Open in new window

SOLUTION
Avatar of OBonio
OBonio
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you want to use the setter to clear, you could use RecieveData = null; Your set method would then ignore the nulll (because you haven't used value). But I don't know why you would do it like that, that's just confusing.

RecieveData.clear() is more clear.
Avatar of u2envy1
u2envy1

ASKER

If I assign another List<string> MyList = RecieveDate;
Then run  RecieveData.clear()
The MyList is also Cleared. Due to Reference types.
How do I not clear MyList when clearing RecieveDate ?
They both refer to the same object. If you clear one, you clear both.
You'd have to create another list. If you want to copy the elements from recievedata to that list at the same time, use:
List<String> myList = new List<String>(RecieveData);

Open in new window

SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of u2envy1

ASKER

Thanks this works......
get
{
    return new List<string>(_RecieveData);
}

Open in new window