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
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();
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 ?
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:
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);
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks this works......
get
{
return new List<string>(_RecieveData);
}
RecieveData.clear() is more clear.