Bujju
Bujju
CC#
Created by Bujju on 12/17/2022 in #help
✅ Exception message not working
I have this code for an exception:
public class WebhookExecutionFailedException : Exception
{
internal WebhookExecutionFailedException(string message, WebhookError error, HttpResponseMesage response) : base(message)
{
ErrorMessage = error.Message;
ErrorCode = error.Code;
Response = response;
}

public string ErrorMessage { get; private set; }

public int ErrorCode { get; private set; }

public HttpResponseMessage { get; private set; }
}
public class WebhookExecutionFailedException : Exception
{
internal WebhookExecutionFailedException(string message, WebhookError error, HttpResponseMesage response) : base(message)
{
ErrorMessage = error.Message;
ErrorCode = error.Code;
Response = response;
}

public string ErrorMessage { get; private set; }

public int ErrorCode { get; private set; }

public HttpResponseMessage { get; private set; }
}
And this code for throwing the exception:
throw new WebhookExecutionFailedException(error.Message, error, response);
throw new WebhookExecutionFailedException(error.Message, error, response);
But when the exception is thrown, the message is "Exception of type 'DiscordIntegration.Exceptions.WebhookExecutionFailedException' was thrown.'" instead of the value of error.Message.
5 replies
CC#
Created by Bujju on 12/10/2022 in #help
✅ WPF Fonts
How can I use custom fonts in a WPF application?
3 replies
CC#
Created by Bujju on 11/25/2022 in #help
✅ Incorrect math
(3 / 4) * 100 returns 0, when it should return 75. Why is this?
8 replies
CC#
Created by Bujju on 11/16/2022 in #help
Help with refactoring [Answered]
How could I simplify this?
var service = new RbService();

switch (fieldNum)
{
case 0:
service.Data.UserAccounts.Find(x => x.UserId == ctx.User.Id).Field1 = value;
await ctx.CreateResponseAsync("✅ Set the first field.", true);
break;
case 1:
service.Data.UserAccounts.Find(x => x.UserId == ctx.User.Id).Field2 = value;
await ctx.CreateResponseAsync("✅ Set the second field.", true);
break;
case 2:
service.Data.UserAccounts.Find(x => x.UserId == ctx.User.Id).Field3 = value;
await ctx.CreateResponseAsync("✅ Set the third field.", true);
break;
}

service.Data.Update();
var service = new RbService();

switch (fieldNum)
{
case 0:
service.Data.UserAccounts.Find(x => x.UserId == ctx.User.Id).Field1 = value;
await ctx.CreateResponseAsync("✅ Set the first field.", true);
break;
case 1:
service.Data.UserAccounts.Find(x => x.UserId == ctx.User.Id).Field2 = value;
await ctx.CreateResponseAsync("✅ Set the second field.", true);
break;
case 2:
service.Data.UserAccounts.Find(x => x.UserId == ctx.User.Id).Field3 = value;
await ctx.CreateResponseAsync("✅ Set the third field.", true);
break;
}

service.Data.Update();
8 replies
CC#
Created by Bujju on 11/4/2022 in #help
Levenshtein distance method throwing `IndexOutOfRangeException` [Answered]
I have this code to calculate levenshtein distance:
public static int CompareStrings(string string1, string string2)
{
if (string.IsNullOrEmpty(string1) && string.IsNullOrEmpty(string2)) return 0;
if (string.IsNullOrEmpty(string1)) return string2.Length;
if (string.IsNullOrEmpty(string2)) return string1.Length;

if (string1.Length > string2.Length)
{
string temp = string1;

string1 = string2;
string2 = temp;
}

var distances = new int[string1.Length + 1, string2.Length + 1];
for (int i = 0; i <= string1.Length; i++)
{
for (int j = 1; i <= string2.Length; j++)
{
int a = Math.Min(distances[i - 1, j] + 1, distances[i, j - 1] + 1);
int b = string2[j - 1] == string1[i - 1] ? 0 : 1;

distances[i, j] = Math.Min(a, b);
}
}

return distances[string1.Length, string2.Length];
}
public static int CompareStrings(string string1, string string2)
{
if (string.IsNullOrEmpty(string1) && string.IsNullOrEmpty(string2)) return 0;
if (string.IsNullOrEmpty(string1)) return string2.Length;
if (string.IsNullOrEmpty(string2)) return string1.Length;

if (string1.Length > string2.Length)
{
string temp = string1;

string1 = string2;
string2 = temp;
}

var distances = new int[string1.Length + 1, string2.Length + 1];
for (int i = 0; i <= string1.Length; i++)
{
for (int j = 1; i <= string2.Length; j++)
{
int a = Math.Min(distances[i - 1, j] + 1, distances[i, j - 1] + 1);
int b = string2[j - 1] == string1[i - 1] ? 0 : 1;

distances[i, j] = Math.Min(a, b);
}
}

return distances[string1.Length, string2.Length];
}
When string1 is empty and string2 is "Example", it works. When string1 is "Ex" and string2 is "Example", it throws an IndexOutOfRangeException.
25 replies
CC#
Created by Bujju on 10/15/2022 in #help
Sort items in list to most similar to a specific string [Answered]
How can I sort items in a List<string> by similarity to a certain string?
5 replies
CC#
Created by Bujju on 10/14/2022 in #help
Modify property of object in List [Answered]
I have a struct like this:
public struct MyStruct
{
public ulong Id { get; set; }

public string MyProperty { get; set; }
}
public struct MyStruct
{
public ulong Id { get; set; }

public string MyProperty { get; set; }
}
And I have a List<MyStruct>. I want to modify MyProperty for a certain MyStruct in the list, using the Id property. I tried this:
MyList.Where(x => x.Id == id).ToList().ForEach(x => x.MyProperty = myVariable);
MyList.Where(x => x.Id == id).ToList().ForEach(x => x.MyProperty = myVariable);
But it didn't work.
13 replies
CC#
Created by Bujju on 10/9/2022 in #help
ArgumentNullException when argument is not null
I have this code:
public class Config
{
[JsonProperty("token")]
public string Token = "TOKEN HERE";

[JsonProperty("source_url")]
public string SourceUrl = "SOURCE URL HERE";

[JsonProperty("server_url")]
public string ServerUrl = "SERVER URL HERE";

[JsonProperty("invite_url")]
public string InviteUrl = "INVITE URL HERE";

[JsonIgnore]
private string _fileName;

public Config(string fileName)
{
_fileName = fileName;

if (!File.Exists(_fileName)) File.Create(_fileName).Close();

var loaded = JsonConvert.DeserializeObject<Config>(File.ReadAllText(_fileName));
if (loaded is not null)
{
Token = loaded.Token;
SourceUrl = loaded.SourceUrl;
ServerUrl = loaded.ServerUrl;
InviteUrl = loaded.InviteUrl;
}
else
{
File.WriteAllText(_fileName, JsonConvert.SerializeObject(new Config(), Formatting.Indented));
}
}

private Config()
{
_fileName = string.Empty;
}

public void Update()
{
File.WriteAllText(_fileName, JsonConvert.SerializeObject(this, Formatting.Indented));
}
}
public class Config
{
[JsonProperty("token")]
public string Token = "TOKEN HERE";

[JsonProperty("source_url")]
public string SourceUrl = "SOURCE URL HERE";

[JsonProperty("server_url")]
public string ServerUrl = "SERVER URL HERE";

[JsonProperty("invite_url")]
public string InviteUrl = "INVITE URL HERE";

[JsonIgnore]
private string _fileName;

public Config(string fileName)
{
_fileName = fileName;

if (!File.Exists(_fileName)) File.Create(_fileName).Close();

var loaded = JsonConvert.DeserializeObject<Config>(File.ReadAllText(_fileName));
if (loaded is not null)
{
Token = loaded.Token;
SourceUrl = loaded.SourceUrl;
ServerUrl = loaded.ServerUrl;
InviteUrl = loaded.InviteUrl;
}
else
{
File.WriteAllText(_fileName, JsonConvert.SerializeObject(new Config(), Formatting.Indented));
}
}

private Config()
{
_fileName = string.Empty;
}

public void Update()
{
File.WriteAllText(_fileName, JsonConvert.SerializeObject(this, Formatting.Indented));
}
}
And whenever I try to access the following field, I get the following exception:
private static Config Config = new("config.json");
private static Config Config = new("config.json");
System.TypeInitializationException: 'The type initializer for 'class' threw an exception.'

Inner Exception:
ArgumentNullException: Path cannot be null. (Parameter 'path')
System.TypeInitializationException: 'The type initializer for 'class' threw an exception.'

Inner Exception:
ArgumentNullException: Path cannot be null. (Parameter 'path')
52 replies
CC#
Created by Bujju on 9/26/2022 in #help
Where is AssemblyInfo.cs [Answered]
I just learned that I am not supposed to make my projects using the .NET Framework project templates, and I can't find AssemblyInfo.cs to set the version in my new project. I tried to make the file myself, but it said the version attributes were duplicates.
69 replies
CC#
Created by Bujju on 9/26/2022 in #help
.NET 6.0 not showing up in Visual Studio [Answered]
19 replies
CC#
Created by Bujju on 9/10/2022 in #help
Line-seperated list from KeyValuePair
I have a KeyValuePair<string, string>, and I would like to make a line-separated list like the following: [Key]: [Value] [Key]: [Value] [Key]: [Value] How could I do that?
10 replies