The Artima Developer Community
Sponsored Link

C# Answers Forum
Static members on generic types

1 reply on 1 page. Most recent reply: Aug 4, 2011 8:34 PM by Shane Poppleton

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Shane Poppleton

Posts: 2
Nickname: spopple
Registered: Aug, 2011

Static members on generic types Posted: Aug 4, 2011 8:33 PM
Reply to this message Reply
Advertisement
I have a class that i want to use as follows:

PersonCollection pc = PersonCollection.Load("person.xml");
Person p = new Person();
p.Name = "Test";
pc.Add(p);
pc.Save();

my code works and compiles correctly, but CodeAnalysis is complaining that I have a static member on a generic type (i.e. my Load method)
"CA1000: Do not declare static members on generic types"

Here is my code:

public interface ISaveable
{
string FileName { get; set; }
bool Dirty { get; set; }
}
public class MyCollection<TItem> : BindingList<TItem>, ISaveable
{
string _fileName;
bool _dirty;

public string FileName
{
get { return _fileName; }
set { _fileName = value; }
}
public bool Dirty
{
get
{
return _dirty;
}
set
{
_dirty = value;
}
}
}
public class SaveableCollection<TList, TItem> : MyCollection<TItem> where TList : ISaveable, new()
{
public SaveableCollection()
{
this.ListChanged += new ListChangedEventHandler(SaveableCollection_ListChanged);
}
void SaveableCollection_ListChanged(object sender, ListChangedEventArgs e)
{
this.Dirty = true;
}
public static TList Load(string fileName)
{
TList list = new TList();

//... Load code goes here

return list;
}
public void Save()
{
//... Save code goes here
}
}


Shane Poppleton

Posts: 2
Nickname: spopple
Registered: Aug, 2011

Re: Static members on generic types Posted: Aug 4, 2011 8:34 PM
Reply to this message Reply
I forgot to include this:

Person and PersonCollection would be declared like this:

[Serializable]
public class Person : INotifyPropertyChanged
{
string _Name;
string _LastName;

public string Name
{
get { return _Name; }
set { _Name = value; OnPropertyChanged("Name"); }

}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}

[Serializable]
public class PersonCollection : SaveableCollection<PersonCollection, Person>
{
}

Flat View: This topic has 1 reply on 1 page
Topic: rounding float values in C# Previous Topic   Next Topic Topic: norton 360 performance issues

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use