arch_il
arch_il
CC#
Created by arch_il on 1/25/2024 in #help
✅ How to force C# to copy data?
I am trying to debug code and I have a list of rooms and another list of backup_rooms. They both get initialized at the same time, but when I need use backup room I find that is has changed (It is never used in code). My guess is, that a reference is created to an object rather than a copy of it. So how to force C# to copy?
10 replies
CC#
Created by arch_il on 12/10/2023 in #help
✅ Confused about publish options
Can I not create an executable for windows that doesn't need any sort of .NET installation to run it? Even python has an option to create an executable that contains all the dependencies and python itself within it. How do I do that for C#?
6 replies
CC#
Created by arch_il on 11/23/2023 in #help
2d collision, Chaotic Ball
No description
26 replies
CC#
Created by arch_il on 8/22/2023 in #help
❔ How does class casting work?
public class Foo
{
public virtual void Func()
{
Console.WriteLine("Foo");
}
}

public class Boo : Foo
{
public override void Func()
{
Console.WriteLine("Foo");
}
}


public class Program
{
public static void Main()
{
var myObjects = new List<Foo>();

myObjects.Add(new Foo()); // works
myObjects.Add(new Boo()); // works
myObjects.Add((Boo)(new Foo())); // how to make this work?

foreach (var myObject in myObjects)
myObject.Func();
}
}
public class Foo
{
public virtual void Func()
{
Console.WriteLine("Foo");
}
}

public class Boo : Foo
{
public override void Func()
{
Console.WriteLine("Foo");
}
}


public class Program
{
public static void Main()
{
var myObjects = new List<Foo>();

myObjects.Add(new Foo()); // works
myObjects.Add(new Boo()); // works
myObjects.Add((Boo)(new Foo())); // how to make this work?

foreach (var myObject in myObjects)
myObject.Func();
}
}
I think code explains itself, but still. How do i cast parent class to child class?
11 replies
CC#
Created by arch_il on 8/10/2023 in #help
❔ I just can't even start to think what has gone wrong.
23 replies
CC#
Created by arch_il on 7/7/2023 in #help
❔ Part of me feels like I need to learn System.Linq; one day
float[] overlaps = new float[]
{
second.Left - first.Right, // left
second.Right - first.Left, // right
second.Top - first.Bottom, // top
second.Bottom - first.Top // bottom
};

if (overlaps[0] <= 0 && overlaps[1] >= 0 && overlaps[2] <= 0 && overlaps[3] >= 0)
{
float min = overlaps.Min(x => Math.Abs(x));

if (min == Math.Abs(overlaps[0]))
return new Vector2(overlaps[0], 0);
else if (min == Math.Abs(overlaps[1]))
return new Vector2(overlaps[1], 0);
else if (min == Math.Abs(overlaps[2]))
return new Vector2(0, overlaps[2]);
else if (min == Math.Abs(overlaps[3]))
return new Vector2(0, overlaps[3]);
}

return Vector2.Zero;
float[] overlaps = new float[]
{
second.Left - first.Right, // left
second.Right - first.Left, // right
second.Top - first.Bottom, // top
second.Bottom - first.Top // bottom
};

if (overlaps[0] <= 0 && overlaps[1] >= 0 && overlaps[2] <= 0 && overlaps[3] >= 0)
{
float min = overlaps.Min(x => Math.Abs(x));

if (min == Math.Abs(overlaps[0]))
return new Vector2(overlaps[0], 0);
else if (min == Math.Abs(overlaps[1]))
return new Vector2(overlaps[1], 0);
else if (min == Math.Abs(overlaps[2]))
return new Vector2(0, overlaps[2]);
else if (min == Math.Abs(overlaps[3]))
return new Vector2(0, overlaps[3]);
}

return Vector2.Zero;
This is simple collision I wrote in few minutes for my game. I feel like I don't need to use Math.Abs() 2 times, first to find and then to compare to number. Can I find index of minimum value using Linq in some simple fancy one liner way?
5 replies
CC#
Created by arch_il on 7/6/2023 in #help
✅ Convert from string to Type?
public class Foo
{
public string MyTypeInString = "Foo";
}

public static class SomeFunctions
{
public static MyFunction<T>(T input)
{
//
}
}

public class Program
{
public static void Main()
{
Foo f = new();
SomeFunctions.MyFunction<f.MyTypeInString.ConvertToType()>(f);
}
}
public class Foo
{
public string MyTypeInString = "Foo";
}

public static class SomeFunctions
{
public static MyFunction<T>(T input)
{
//
}
}

public class Program
{
public static void Main()
{
Foo f = new();
SomeFunctions.MyFunction<f.MyTypeInString.ConvertToType()>(f);
}
}
How do I get that ConvertToType(this string) function? I believe it should be possible.
6 replies
CC#
Created by arch_il on 7/5/2023 in #help
✅ Why can't I deserialize list of tuples?
I Got List<bool, string> inside my code. This is how I wrote it down in Json.
"texts": [
{
"Item1": true,
"Item2": "hello"
},
{
"Item1": false,
"Item2": "hi"
}
]
"texts": [
{
"Item1": true,
"Item2": "hello"
},
{
"Item1": false,
"Item2": "hi"
}
]
But bool is always false and string is always equal to null. Is it wrong format or something?
18 replies
CC#
Created by arch_il on 6/29/2023 in #help
✅ List of inherited objects
I know that C# is strongly typed programming language and there are some limitations when it comes to what you can put in lists, but can you create a list of objects where each object is inherited from single class and have same constructor and function?
26 replies
CC#
Created by arch_il on 6/16/2023 in #help
✅ Get this error while trying to Deserialize object
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Text.Json.dll: 'Each parameter in the deserialization constructor on type 'AnimationTesting.Models.Room' must bind to an object property or field on deserialization. Each parameter name must match with a property or field on the object. The match can be case-insensitive.'
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Text.Json.dll: 'Each parameter in the deserialization constructor on type 'AnimationTesting.Models.Room' must bind to an object property or field on deserialization. Each parameter name must match with a property or field on the object. The match can be case-insensitive.'
This is an error I get. I am not even sure what part of code to send, which would cause an issue.
8 replies
CC#
Created by arch_il on 5/14/2023 in #help
✅ advanced math in c#
is there any math module in C# that would evaluate (1/sqrt(2))^2 as 1/2 ? Something like Wolfram Mathematica but much simpler.
3 replies
CC#
Created by arch_il on 5/8/2023 in #help
✅ cannot be accessed with an instance reference
public static class Details
{
// class for drawing event
public class EventDrawModel
{
public static string title { get; set; }
}

// model for drawing event
private static EventDrawModel eventDrawModel = new EventDrawModel();

// this is specific update func only called when selected event is changed
public static void Update()
{
Details.eventDrawModel.title = "";
}
}
public static class Details
{
// class for drawing event
public class EventDrawModel
{
public static string title { get; set; }
}

// model for drawing event
private static EventDrawModel eventDrawModel = new EventDrawModel();

// this is specific update func only called when selected event is changed
public static void Update()
{
Details.eventDrawModel.title = "";
}
}
Member 'Details.EventDrawModel.title' cannot be accessed with an instance reference; qualify it with a type name instead
Member 'Details.EventDrawModel.title' cannot be accessed with an instance reference; qualify it with a type name instead
How would one fix this issue?
5 replies
CC#
Created by arch_il on 1/12/2023 in #help
❔ A problem with database in asp dotnet
I have class Market that has Owner as one object. With migration everything worked just fine. In database Market table has value OwnerId and writing to that table from program works just fine, but when i try to get those values it shows me null in place of Owner. What do I do?
52 replies