Solved

Generic List<> as a property

Posted on 2009-05-08
7
2,059 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Coding C# in Linux 8 67
open System.IO.Compression.ZipArchiveEntry as ZipFile without storing to file 2 60
SqlDataBase 7 48
Graphics 2 28
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

773 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