❔ C# Will not write to txt file

Not to sure what I did wrong as this is the first time I'm trying this. The code runs just gives (process 32508) exited with code 0. https://paste.mod.gg/duanwieezrrk/0
BlazeBin - duanwieezrrk
A tool for sharing your source code with the world!
56 Replies
Angius
Angius2y ago
You're not using the streamwriter You're writing to the console
suddenly_toast
suddenly_toastOP2y ago
can you point out where it is writing to console?
Angius
Angius2y ago
suddenly_toast
suddenly_toastOP2y ago
ohhh so i need a new set of write lines for the print func that say streamwriter gotcha
Henkypenky
Henkypenky2y ago
it's never entering this if
if (char.Equals(Option != 'Y', StringComparison.OrdinalIgnoreCase))
{
Manometer.Printvalues(density, interval);
};
if (char.Equals(Option != 'Y', StringComparison.OrdinalIgnoreCase))
{
Manometer.Printvalues(density, interval);
};
Angius
Angius2y ago
That too, yeah It's trying to compare that char with a boolean
suddenly_toast
suddenly_toastOP2y ago
Im not seeing why its trying to compare with a boolean?
Angius
Angius2y ago
Option != 'Y' This results in a boolean
suddenly_toast
suddenly_toastOP2y ago
yes
Henkypenky
Henkypenky2y ago
char.Equals doesn't have an overload for what you want
Angius
Angius2y ago
a.Equals(b) compares char a with char b So 'a'.Equals(false) will try comparing char 'a' with boolean false
mtreit
mtreit2y ago
Isn't that char the type and not an instance?
suddenly_toast
suddenly_toastOP2y ago
honestly im not sure. im still trying to learn this. So to fix would i change char to boolean or the option part to a char?
mtreit
mtreit2y ago
char is a keyword in C#
Henkypenky
Henkypenky2y ago
you can do char.Equsl(obj1, obj2) or Option.Equals(ob1)
mtreit
mtreit2y ago
Yeah, but that's not what the code shown is doing
Henkypenky
Henkypenky2y ago
yeah
mtreit
mtreit2y ago
Does that even compile?
Henkypenky
Henkypenky2y ago
yes cause StringComparison.OrdinalIgnoreCase is 5
suddenly_toast
suddenly_toastOP2y ago
it compiled before i changed the console bits under stream writer to streawriter
mtreit
mtreit2y ago
Oh it's just treating the StringComparison like another object Equals(object a, object b)
Henkypenky
Henkypenky2y ago
yeah
mtreit
mtreit2y ago
So this code is clearly not doing what is intended 🙂
suddenly_toast
suddenly_toastOP2y ago
yes lol i just need it to see if its y or n make it upper case then do what the y or n option is
Henkypenky
Henkypenky2y ago
StringComparison in char Equality is also a red flag there is no overload for char equality
suddenly_toast
suddenly_toastOP2y ago
and this also brings up another question. if the y/n is not working is the m/w not working also?
Henkypenky
Henkypenky2y ago
i tested your code and the txt is empty so yes
suddenly_toast
suddenly_toastOP2y ago
okat testing the code rn for the streamwriter problem. after that im still not sure how to fix the boolean comparisin prob
Henkypenky
Henkypenky2y ago
you can do:
char.Equals(obj1, obj2)
char.Equals(obj1, obj2)
or
obj1.Equals(obj2)
obj1.Equals(obj2)
suddenly_toast
suddenly_toastOP2y ago
Program does not contain a static 'Main' method suitable for an entry point its saying that
Henkypenky
Henkypenky2y ago
what did you change
suddenly_toast
suddenly_toastOP2y ago
BlazeBin - avehyhvvntnd
A tool for sharing your source code with the world!
Henkypenky
Henkypenky2y ago
why are u passing a StreamWriter in the main as a parameter?
Angius
Angius2y ago
Was about to ask
suddenly_toast
suddenly_toastOP2y ago
because it did this - i sec
Henkypenky
Henkypenky2y ago
what you shared before was correct you just need to correct char comparison and streamwriter cause you are calling a method that prints to console not to file
suddenly_toast
suddenly_toastOP2y ago
BlazeBin - yumcdvyzoegl
A tool for sharing your source code with the world!
suddenly_toast
suddenly_toastOP2y ago
si i changed the colsole to streamwriter and it was red squiogillys eveerywhere
Angius
Angius2y ago
Because you're trying to use those methods as static
Henkypenky
Henkypenky2y ago
cause your variable is outputefile
Angius
Angius2y ago
Use the StreamWriter you instantiated
Henkypenky
Henkypenky2y ago
Henkypenky
Henkypenky2y ago
don't use it as Type
suddenly_toast
suddenly_toastOP2y ago
of pute output file there there we go that makes sense okay now for the if statements. when you say obj1 obj 2 is it comparing ob1 to obj 2?
Angius
Angius2y ago
Yes
Henkypenky
Henkypenky2y ago
i understand what you want to do, with OrdinalIgnoreCase, but that only works for strings, there is no overload in char.Equals that takes that argument, you just compare chars. That's it
suddenly_toast
suddenly_toastOP2y ago
so is obj 1 or obj 2 the master that is the one being compared too
Angius
Angius2y ago
They're... just being compared
int a = 5;
int b = 17;

a.Equals(b); // same result
b.Equals(a); // same result
int a = 5;
int b = 17;

a.Equals(b); // same result
b.Equals(a); // same result
suddenly_toast
suddenly_toastOP2y ago
okay let me try that 1 min
{
Console.WriteLine("Create file from data?: (Y/N)");
Option = char.Parse(Console.ReadLine());
Option = char.ToUpper(Option);

if (Option != 'Y' && Option != 'N')
Console.WriteLine("Incorrect Response, please input a correct respose (Y/N)");
} while (Option != 'Y' && Option != 'N');


if (char.Equals(Option, 'Y'))
{
Manometer.Printvalues(density, interval);
};
if (char.Equals(Option, 'N'))
{
Environment.Exit(0);
};
{
Console.WriteLine("Create file from data?: (Y/N)");
Option = char.Parse(Console.ReadLine());
Option = char.ToUpper(Option);

if (Option != 'Y' && Option != 'N')
Console.WriteLine("Incorrect Response, please input a correct respose (Y/N)");
} while (Option != 'Y' && Option != 'N');


if (char.Equals(Option, 'Y'))
{
Manometer.Printvalues(density, interval);
};
if (char.Equals(Option, 'N'))
{
Environment.Exit(0);
};
is that right?
Angius
Angius2y ago
Yep
suddenly_toast
suddenly_toastOP2y ago
thankyou
FusedQyou
FusedQyou2y ago
I'm surprised this is valid code
Henkypenky
Henkypenky2y ago
since char.Equals(obj1, obj2) evaluates object? it's valid, albeit wrong
MODiX
MODiX2y ago
Henkypenky#4865
REPL Result: Success
Console.WriteLine(char.Equals(true, false));
Console.WriteLine(char.Equals(true, false));
Console Output
False
False
Compile: 561.552ms | Execution: 64.937ms | React with ❌ to remove this embed.
MODiX
MODiX2y ago
Henkypenky#4865
REPL Result: Success
Console.WriteLine(char.Equals(true, 5));
Console.WriteLine(char.Equals(true, 5));
Console Output
False
False
Compile: 511.944ms | Execution: 25.421ms | React with ❌ to remove this embed.
Accord
Accord2y 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.
Want results from more Discord servers?
Add your server