Click here to Skip to main content
14,356,272 members
   

C#

 
GeneralRe: Can I convert EventArgs type to PaintEventArgs type ? Pin
Member 245846717-Oct-19 23:14
memberMember 245846717-Oct-19 23:14 
GeneralRe: Can I convert EventArgs type to PaintEventArgs type ? Pin
OriginalGriff17-Oct-19 23:36
protectorOriginalGriff17-Oct-19 23:36 
GeneralRe: Can I convert EventArgs type to PaintEventArgs type ? Pin
Member 245846721-Oct-19 18:03
memberMember 245846721-Oct-19 18:03 
QuestionUpdating a List record within a record with c# and Linq, looking for something cleaner than add and remove Pin
jkirkerx17-Oct-19 13:50
professionaljkirkerx17-Oct-19 13:50 
AnswerRe: Updating a List record within a record with c# and Linq, looking for something cleaner than add and remove Pin
Luc Pattyn17-Oct-19 17:09
professionalLuc Pattyn17-Oct-19 17:09 
GeneralRe: Updating a List record within a record with c# and Linq, looking for something cleaner than add and remove Pin
jkirkerx18-Oct-19 7:27
professionaljkirkerx18-Oct-19 7:27 
AnswerRe: Updating a List record within a record with c# and Linq, looking for something cleaner than add and remove Pin
Richard Deeming18-Oct-19 2:44
mveRichard Deeming18-Oct-19 2:44 
AnswerRe: Updating a List record within a record with c# and Linq, looking for something cleaner than add and remove Pin
Richard Deeming18-Oct-19 3:02
mveRichard Deeming18-Oct-19 3:02 
Just for fun, here's a simplistic - and not thoroughly tested! - example using ref returns:
public readonly struct SubCategory : IEquatable<SubCategory>
{
    public SubCategory(int id, string name) => (Id, Name) = (id, name);
    public int Id { get; }
    public string Name { get; }
    public override int GetHashCode() => Id;
    public override bool Equals(object obj) => obj is SubCategory other && Equals(other);
    public bool Equals(SubCategory other) => Id == other.Id;
}

public class SubCategoryList : IEnumerable<SubCategory>
{
    private SubCategory[] _categories = new SubCategory[4];
    private int _length;
    
    public int Count => _length;
    public int Capacity => _categories.Length;
    public ref SubCategory this[int index] => ref _categories[index];
    
    public ref SubCategory FindOrAdd(int id)
    {
        for (int index = 0; index < _length; index++)
        {
            if (_categories[index].Id == id)
            {
                return ref _categories[index];
            }
        }
        
        Add(new SubCategory(id, null));
        return ref _categories[_length - 1];
    }
    
    public void Add(SubCategory value)
    {
        if (_length == _categories.Length)
        {
            int newSize = _length * 2;
            Array.Resize(ref _categories, newSize);
        }
        
        _categories[_length] = value;
        _length++;
    }
    
    public IEnumerator<SubCategory> GetEnumerator()
    {
        SubCategory[] categories = _categories;
        int length = _length;
        
        for (int index = 0; index < length; index++)
        {
            yield return categories[index];
        }
    }
    
    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

public class Category
{
    public SubCategoryList SubCategories { get; } = new SubCategoryList();
}
Usage:
var cR = new Category
{
    SubCategories = 
    {
        new SubCategory(1, "C1"),
        new SubCategory(2, "C2"),
        new SubCategory(3, "C3"),
    }
};

cR.SubCategories.FindOrAdd(2) = new SubCategory(2, "X2");
cR.SubCategories.FindOrAdd(42) = new SubCategory(42, "Z42");
NB: I wouldn't recommend using this approach. Smile | :)



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: Updating a List record within a record with c# and Linq, looking for something cleaner than add and remove Pin
jkirkerx18-Oct-19 7:41
professionaljkirkerx18-Oct-19 7:41 
AnswerI don't get this with IndexOf not finding the record Pin
jkirkerx18-Oct-19 9:24
professionaljkirkerx18-Oct-19 9:24 
SuggestionNeed help wind form code Pin
AlexJo198516-Oct-19 23:11
memberAlexJo198516-Oct-19 23:11 
GeneralRe: Need help wind form code Pin
lw@zi 17-Oct-19 0:17
professional lw@zi 17-Oct-19 0:17 
GeneralRe: Need help wind form code Pin
Luc Pattyn17-Oct-19 16:41
professionalLuc Pattyn17-Oct-19 16:41 
AnswerRe: Need help wind form code Pin
Richard Deeming18-Oct-19 2:32
mveRichard Deeming18-Oct-19 2:32 
GeneralRe: Need help wind form code Pin
Luc Pattyn18-Oct-19 2:44
professionalLuc Pattyn18-Oct-19 2:44 
GeneralRe: Need help wind form code Pin
Richard Deeming18-Oct-19 4:03
mveRichard Deeming18-Oct-19 4:03 
GeneralRe: Need help wind form code Pin
Luc Pattyn18-Oct-19 4:26
professionalLuc Pattyn18-Oct-19 4:26 
GeneralRe: Need help wind form code Pin
#realJSOP17-Oct-19 3:01
mve#realJSOP17-Oct-19 3:01 
QuestionExistance pointers in c# Pin
Member 774487115-Oct-19 23:38
memberMember 774487115-Oct-19 23:38 
AnswerRe: Existance pointers in c# Pin
OriginalGriff15-Oct-19 23:40
protectorOriginalGriff15-Oct-19 23:40 
AnswerRe: Existance pointers in c# Pin
F-ES Sitecore15-Oct-19 23:47
mveF-ES Sitecore15-Oct-19 23:47 
AnswerRe: Existance pointers in c# Pin
Richard MacCutchan15-Oct-19 23:58
protectorRichard MacCutchan15-Oct-19 23:58 
AnswerRe: Existance pointers in c# Pin
Richard Deeming16-Oct-19 9:02
mveRichard Deeming16-Oct-19 9:02 
AnswerRe: Existance pointers in c# Pin
Anandkumar Prajapati24-Oct-19 23:08
professionalAnandkumar Prajapati24-Oct-19 23:08 
Questionbetter way to serialize a DataView, exclusive of filtered rows ? Pin
BillWoodruff15-Oct-19 22:16
mveBillWoodruff15-Oct-19 22:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.