C
C#9mo ago
dismal

❔ If statement isn't working as expected?

Basically, I'm writing an app with WinForms and I am implementing a version system that fetches the up to date version number from my GitHub and compares it to the local version file (_ver.apim).
using System.Net;
private void Form1_Load(object sender, EventArgs e)
{
WebClient client = new WebClient();
if (client.DownloadString("https://raw.githubusercontent.com/Dismalitie/ProtoSploit/main/ver.apim") != File.ReadAllText("_ver.apim"));
{
MessageBox.Show("A new version of ProtoSploit is available!", "ProtoSploit Update");
}
}
using System.Net;
private void Form1_Load(object sender, EventArgs e)
{
WebClient client = new WebClient();
if (client.DownloadString("https://raw.githubusercontent.com/Dismalitie/ProtoSploit/main/ver.apim") != File.ReadAllText("_ver.apim"));
{
MessageBox.Show("A new version of ProtoSploit is available!", "ProtoSploit Update");
}
}
But for some reason, even when both values are the same, it shows the message even though the if statement is inverted by !=. I'm new to C# and still getting the hang of it, but I just can't seem to figure it out.
No description
25 Replies
canton7
canton79mo ago
The most obvious reason is that both strings aren't the same Maybe one has a trailing newline or something? Prove to us that both are the same 🙂
Angius
Angius9mo ago
Use the debugger to see what the strings actually are Could be the WebClient is just returning null on a bad request or something Wouldn't surprise me, it had to be deprecated for a reason lol
dismal
dismal9mo ago
both of them definitely have just one line do you have any dlls / libraries to replace it?
Angius
Angius9mo ago
HttpClient And, sure, they have just one line... but are all the characters in that line the exact same? cat and cat are not the same, neither are dog and Dog
dismal
dismal9mo ago
1000%
Angius
Angius9mo ago
Aight, show the screenshot of the debugger that shows both variables
dismal
dismal9mo ago
same, no spaces even matches in sit
No description
Angius
Angius9mo ago
I don't see the debugger
dismal
dismal9mo ago
idk how to use dbg im beginner
Angius
Angius9mo ago
No better time to learn than now, then $debug
MODiX
MODiX9mo ago
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
dismal
dismal9mo ago
thanks i think ill just attempt to use restsharp and if i get the same problem then ill find another way
canton7
canton79mo ago
Looks to me like the first one has a newline after?
1
<newline>
1
1
<newline>
1
Try calling .Trim() on both strings
dismal
dismal9mo ago
im dumb thats it
canton7
canton79mo ago
😆
dismal
dismal9mo ago
i tried a combo of setting output lines to // and one of them didnt return a newline
dismal
dismal9mo ago
the file doesnt have new line ill just use trim no more new lines, but my code still runs? hmm
canton7
canton79mo ago
Not sure what you mean by that I'm afraid Did you expect your code to refuse to run?
dismal
dismal9mo ago
private void Form1_Load(object sender, EventArgs e)
{
WebClient client = new WebClient();
if (client.DownloadString("https://raw.githubusercontent.com/Dismalitie/ProtoSploit/main/ver.apim").Trim() != File.ReadAllText("_ver.apim").Trim());
{
MessageBox.Show("A new version of ProtoSploit is available!", "ProtoSploit Update");
}
Console.WriteLine(client.DownloadString("https://raw.githubusercontent.com/Dismalitie/ProtoSploit/main/ver.apim").Trim());
Console.WriteLine(File.ReadAllText("_ver.apim").Trim());
}
private void Form1_Load(object sender, EventArgs e)
{
WebClient client = new WebClient();
if (client.DownloadString("https://raw.githubusercontent.com/Dismalitie/ProtoSploit/main/ver.apim").Trim() != File.ReadAllText("_ver.apim").Trim());
{
MessageBox.Show("A new version of ProtoSploit is available!", "ProtoSploit Update");
}
Console.WriteLine(client.DownloadString("https://raw.githubusercontent.com/Dismalitie/ProtoSploit/main/ver.apim").Trim());
Console.WriteLine(File.ReadAllText("_ver.apim").Trim());
}
the statement should be false because they are the same values yet MessageBox.Show("A new version of ProtoSploit is available!", "ProtoSploit Update"); still runs
canton7
canton79mo ago
Read them into separate variables, then look at them in the debugger
dismal
dismal9mo ago
k
dismal
dismal9mo ago
when reading them into vars net and file, how could file be null even tho its printed to the console?
string net = client.DownloadString("https://raw.githubusercontent.com/Dismalitie/ProtoSploit/main/ver.apim").Trim();
string file = File.ReadAllText("_ver.apim").Trim();

Console.WriteLine("net: "+client.DownloadString("https://raw.githubusercontent.com/Dismalitie/ProtoSploit/main/ver.apim").Trim());
Console.WriteLine("file:"+File.ReadAllText("_ver.apim").Trim());
string net = client.DownloadString("https://raw.githubusercontent.com/Dismalitie/ProtoSploit/main/ver.apim").Trim();
string file = File.ReadAllText("_ver.apim").Trim();

Console.WriteLine("net: "+client.DownloadString("https://raw.githubusercontent.com/Dismalitie/ProtoSploit/main/ver.apim").Trim());
Console.WriteLine("file:"+File.ReadAllText("_ver.apim").Trim());
No description
No description
Accord
Accord9mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.