Waffen
Waffen
CC#
Created by Waffen on 5/22/2023 in #help
❔ Deep clone variable
How to create a complete, isolated copy of a variable to prevent the copy from changing when changed parent variable. I have tried some variants: 1. The most easiest variant
var parent = new Class();
var clonedParent = parent;
// do some changes with parent
if (parent == clonedParent) <-- True
var parent = new Class();
var clonedParent = parent;
// do some changes with parent
if (parent == clonedParent) <-- True
2. Variant with ICloneable *logic still not changed
class Person : ICloneable
{
public string Name { get; set; }
public int Age { get; set; }

public object Clone()
{
return new Person { Name = this.Name, Age = this.Age };
}
}
class Person : ICloneable
{
public string Name { get; set; }
public int Age { get; set; }

public object Clone()
{
return new Person { Name = this.Name, Age = this.Age };
}
}
if (parent == clonedParent) <-- True Still True 3. A little bit harder variant with ICloneable
class myClass : ICloneable
{
public string Name { get; set; }
public int Age { get; set; }

public object Clone()
{
return this.MemberwiseClone();
}
}
class myClass : ICloneable
{
public string Name { get; set; }
public int Age { get; set; }

public object Clone()
{
return this.MemberwiseClone();
}
}
if (parent == clonedParent) <-- True Still True Do you know any other variants to clone and isolate harold
86 replies
CC#
Created by Waffen on 3/28/2023 in #help
❔ Problem with Dictionary<ulong, Image> (Strange work of memory access)
Concept of my system: A system that draws a picture with stickers, each of the stickers can be moved, there can be an unlimited number of stickers. Code with explanations in comments:
private static Dictionary<ulong, Image> UserStickerProfiles = new();

public static async Task MoveSticker(Sticker sticker)
{
var stickerImage = await ItemManager.GetProfileItemImageAsync(itemId); // take image of sticker from file

UserStickerProfiles.TryGetValue(args.User.Id, out var image);
if (image == null)
{
image = await ProfileMethods.DrawProfileExceptStickerAsync(args.User, itemId); // draw picture without this sticker
UserStickerProfiles.Add(args.User.Id, image);
}

image.Mutate(x => x.DrawImage(stickerImage, new Point(sticker.PositionX, sticker.PositionY), 1f)); // draw this sticker with new coordinations

// saving and sending
var memoryStream = new MemoryStream();
await image.SaveAsPngAsync(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);

// send image to user with memortStream
}
private static Dictionary<ulong, Image> UserStickerProfiles = new();

public static async Task MoveSticker(Sticker sticker)
{
var stickerImage = await ItemManager.GetProfileItemImageAsync(itemId); // take image of sticker from file

UserStickerProfiles.TryGetValue(args.User.Id, out var image);
if (image == null)
{
image = await ProfileMethods.DrawProfileExceptStickerAsync(args.User, itemId); // draw picture without this sticker
UserStickerProfiles.Add(args.User.Id, image);
}

image.Mutate(x => x.DrawImage(stickerImage, new Point(sticker.PositionX, sticker.PositionY), 1f)); // draw this sticker with new coordinations

// saving and sending
var memoryStream = new MemoryStream();
await image.SaveAsPngAsync(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);

// send image to user with memortStream
}
Problem: Each time you use this function, a new picture should appear with ALL stickers and the sticker that we moved to a new location. However, it happens that the sticker is duplicated and there are several of them. The suggestion line image.Mutate(x => x.DrawImage(stickerImage, new Point(sticker.PositionX, sticker.PositionY), 1f)); updates the Dictionary directly, though it shouldn't be, presumably because C# has a memory bug here. I am using .NET 7.0
13 replies
CC#
Created by Waffen on 9/2/2022 in #help
NullReferenceException on FirstorDefault()
85 replies