Solved

Generic List<> as a property

Posted on 2009-05-08
7
2,060 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:u2envy1
7 Comments
 
LVL 8

Assisted Solution

by:OBonio
OBonio earned 250 total points
ID: 24334745

<object>.ReceiveData.Clear()

Open in new window

0
 

Accepted Solution

by:
u2envy1 earned 0 total points
ID: 24334815
Thx.....
Are Generics Ref types or Value types ?
0
 
LVL 1

Expert Comment

by:Acumentality
ID: 24334822
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.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:u2envy1
ID: 24334881
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 ?
0
 
LVL 1

Expert Comment

by:Acumentality
ID: 24334918
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

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 24334933
Have you tried changing your get to:
get
{
    return new List<string>(_RecieveData);
}

Open in new window

0
 

Author Comment

by:u2envy1
ID: 24335021
Thanks this works......
get
{
    return new List<string>(_RecieveData);
}

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question