Click here to Skip to main content
15,894,405 members
Home / Discussions / C#
   

C#

 
GeneralRe: Can I convert EventArgs type to PaintEventArgs type ? Pin
Member 245846717-Oct-19 22:14
Member 245846717-Oct-19 22:14 
GeneralRe: Can I convert EventArgs type to PaintEventArgs type ? Pin
OriginalGriff17-Oct-19 22:36
mveOriginalGriff17-Oct-19 22:36 
GeneralRe: Can I convert EventArgs type to PaintEventArgs type ? Pin
Member 245846721-Oct-19 17:03
Member 245846721-Oct-19 17:03 
QuestionUpdating a List record within a record with c# and Linq, looking for something cleaner than add and remove Pin
jkirkerx17-Oct-19 12:50
professionaljkirkerx17-Oct-19 12: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 16:09
sitebuilderLuc Pattyn17-Oct-19 16: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 6:27
professionaljkirkerx18-Oct-19 6: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 1:44
mveRichard Deeming18-Oct-19 1: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 2:02
mveRichard Deeming18-Oct-19 2:02 
Just for fun, here's a simplistic - and not thoroughly tested! - example using ref returns:
C#
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:
C#
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 6:41
professionaljkirkerx18-Oct-19 6:41 
AnswerI don't get this with IndexOf not finding the record Pin
jkirkerx18-Oct-19 8:24
professionaljkirkerx18-Oct-19 8:24 
SuggestionNeed help wind form code Pin
AlexJo198516-Oct-19 22:11
AlexJo198516-Oct-19 22:11 
GeneralRe: Need help wind form code Pin
dan!sh 16-Oct-19 23:17
professional dan!sh 16-Oct-19 23:17 
GeneralRe: Need help wind form code Pin
Luc Pattyn17-Oct-19 15:41
sitebuilderLuc Pattyn17-Oct-19 15:41 
AnswerRe: Need help wind form code Pin
Richard Deeming18-Oct-19 1:32
mveRichard Deeming18-Oct-19 1:32 
GeneralRe: Need help wind form code Pin
Luc Pattyn18-Oct-19 1:44
sitebuilderLuc Pattyn18-Oct-19 1:44 
GeneralRe: Need help wind form code Pin
Richard Deeming18-Oct-19 3:03
mveRichard Deeming18-Oct-19 3:03 
GeneralRe: Need help wind form code Pin
Luc Pattyn18-Oct-19 3:26
sitebuilderLuc Pattyn18-Oct-19 3:26 
GeneralRe: Need help wind form code Pin
#realJSOP17-Oct-19 2:01
mve#realJSOP17-Oct-19 2:01 
QuestionExistance pointers in c# Pin
Member 774487115-Oct-19 22:38
Member 774487115-Oct-19 22:38 
AnswerRe: Existance pointers in c# Pin
OriginalGriff15-Oct-19 22:40
mveOriginalGriff15-Oct-19 22:40 
AnswerRe: Existance pointers in c# Pin
F-ES Sitecore15-Oct-19 22:47
professionalF-ES Sitecore15-Oct-19 22:47 
AnswerRe: Existance pointers in c# Pin
Richard MacCutchan15-Oct-19 22:58
mveRichard MacCutchan15-Oct-19 22:58 
AnswerRe: Existance pointers in c# Pin
Richard Deeming16-Oct-19 8:02
mveRichard Deeming16-Oct-19 8:02 
AnswerRe: Existance pointers in c# Pin
Anandkumar Prajapati24-Oct-19 22:08
professionalAnandkumar Prajapati24-Oct-19 22:08 
Questionbetter way to serialize a DataView, exclusive of filtered rows ? Pin
BillWoodruff15-Oct-19 21:16
professionalBillWoodruff15-Oct-19 21: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.