Reputation: 23169
Considering the class below
- can I do anything to implement a case-insensitive string?
public class Attibute
{
// The Name should be case-insensitive
public string Name
{
get;
set;
}
public Attibute()
{
}
}
public class ClassWithAttributes
{
private List<Attributes> _attributes;
public ClassWithAttributes(){}
public AddAttribute(Attribute attribute)
{
// Whats the best way to implement the check?
_attributes.add(attribute);
}
}
Structure of an HTML 4 Document
I have edited the class to be a bit more objective and specific
Upvotes: 0
Views: 4384
Reputation: 888177
In answer to the restructured question, you could do it like this:
public class Attribute { public string Name { get; set; } }
public class AttributeCollection : KeyedCollection<string, Attribute> {
public AttributeCollection() : base(StringComparer.OrdinalIgnoreCase) { }
protected override string GetKeyForItem(Attribute item) { return item.Name; }
}
public class ClassWithAttributes {
private AttributeCollection _attributes;
public void AddAttribute(Attribute attribute) {
_attributes.Add(attribute);
//KeyedCollection will throw an exception
//if there is already an attribute with
//the same (case insensitive) name.
}
}
If you use this, you should either make Attribute.Name
read-only or call ChangeKeyForItem whenever it's changed.
Upvotes: 2
Reputation: 888177
Alternatively, you might want to make the property always uppercase, like this.
public class XHtmlOneDTDElementAttibute : ElementRegion {
string name;
// The Name should be case-insensitive
public string Name {
get { return name; }
set { name = value.ToUpperInvariant(); }
}
// The Value should be case-sensitive
public string Value { get; set; }
}
Upvotes: 1
Reputation: 60972
Well, my take on this, after glancing at the spec, is that there's nothing you need to do to make the string properties case-insensitive. The concept doesn't really make sense, anyway: strings aren't case-sensitive or -insensitive; operations on them (like search and sort) are.
(I know the W3C's HTML recommendations say essentially that. It's badly-phrased.)
Upvotes: 1
Reputation: 888177
It depends what you're trying to do with the strings.
If you want to compare strings regardless of case, call String.Equals
with StringComparison.OrdinalIgnoreCase
.
If you want to put them in a dictionary, make the dictionary's comparer StringComparer.OrdinalIgnoreCase
.
Therefore, you could make a function as follows:
public class XHtmlOneDTDElementAttibute : ElementRegion {
public bool IsTag(string tag) {
return Name.Equals(tag, StringComparison.OrdinalIgnoreCase);
}
// The Name should be case-insensitive
public string Name { get; set; }
// The Value should be case-sensitive
public string Value { get; set; }
}
If you want a more specific solution, please tell me what you're doing with the Name
property
Upvotes: 1
Reputation: 9708
You can't have case-insensitive properties—you can only have case-insensitive operations, like a comparison. If someone accesses XHtmlOneDTDElementAttibute.Name, they will get back a string with whatever case it was created with.
Whenever you use .Name, you can implement that method in a way that ignores the case of the string.
Upvotes: 2