C
C#2y ago
Birsu

✅ C Sharp help

Hello can someone who knows c# help me with my homework? I share the pdfs for my homework its about methods and i'm struggling with them so if u have more time can u please read pdfs and then help me?
46 Replies
Saber
Saber2y ago
How about you ask a question about what you are stuck on
Birsu
BirsuOP2y ago
I don't know how to create a method and I'm having a hard time typing what my teacher wants into the methods. Also i wrote some cods for my previous homework and i know how to create this program but i don't seperate them into the methods
Saber
Saber2y ago
surely your instructor when over how to use methods in class, or you have materials on how to create a method
Birsu
BirsuOP2y ago
yes i know how to create but my teacher say in this homework methods has a single parameter but i don't create a good one
Birsu
BirsuOP2y ago
Birsu
BirsuOP2y ago
It gives an error but i don't know how to fix it
mtreit
mtreit2y ago
You don't specify the type of the parameter when calling a method
mortious
mortious2y ago
yes, it is expecting a string so give it a string like "this is a string" or something like "My message is this text" etc WriteMessageToFile("Test Message"); Also GetMessage returns a string so it should be like... string message = GetMessage(); WriteMessageToFile(message);
Birsu
BirsuOP2y ago
Birsu
BirsuOP2y ago
I change that this way but if i open the file its empty how can i solve this problem?
mtreit
mtreit2y ago
You seem to be calling ReadLine twice? Are you sure the text you are writing to the file is not actually an empty string?
Birsu
BirsuOP2y ago
I don't write in the file i get the message from the user so the user is writing something and it also write in the file but it didn't
mtreit
mtreit2y ago
What is the console output of your program?
Birsu
BirsuOP2y ago
Birsu
BirsuOP2y ago
and file is empty always
mtreit
mtreit2y ago
"Original message contains characters" <- see anything wrong with that output? It's an empty string Which you are writing to the file
Birsu
BirsuOP2y ago
I write my message is this text but its up to user it changes
mtreit
mtreit2y ago
Your output should have been "Original message contains My message is the text characters" But it's not. Probably because you are calling ReadLine twice and throwing away the input the first time
Birsu
BirsuOP2y ago
where am i calling ReadLine twice?
mtreit
mtreit2y ago
😐
mtreit
mtreit2y ago
Birsu
BirsuOP2y ago
is something wrong with writeData.Dispose or its correct
mtreit
mtreit2y ago
You should really use using instead of calling Dispose directly, but it has nothing to do with the problem.
Birsu
BirsuOP2y ago
here i want to get the message from the user the first ReadLine and i create an a value to return that message
mtreit
mtreit2y ago
Remove the first call to ReadLine See what happens
Birsu
BirsuOP2y ago
ok oh it works but wrong way it directly say what i write not the contain characters
mtreit
mtreit2y ago
The code is doing exactly what you tell it to
Birsu
BirsuOP2y ago
but i want it to write how many characters the message is how can i do that?
mtreit
mtreit2y ago
Use the Length property of the string
Birsu
BirsuOP2y ago
ok i did and it works tysm
Birsu
BirsuOP2y ago
Here the program will read the text in the file
Birsu
BirsuOP2y ago
Am i correctly writing this?
Birsu
BirsuOP2y ago
And i have a problem with this again
mtreit
mtreit2y ago
Do you think trying to read a file after you close it is going to work? Read the error message
Birsu
BirsuOP2y ago
if i am write at the end its give a warning so i replace it
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.
Birsu
BirsuOP2y ago
What should i write inside the ReadTextFromFile method?
arion
arion2y ago
The answer is a simple one, though I won't try to spoonfeed u the answer Instead I will just ask some questions regarding your method that may help you. Your code:
public static string ReadTextFromFile(StreamReader sr)
{
StreamReader MyTextFile = new StreamReader("MyTextFile.txt");
Console.Write(MyTyextFile.ReadLine());
return sr.ReadLine();
sr.Close();
}
public static string ReadTextFromFile(StreamReader sr)
{
StreamReader MyTextFile = new StreamReader("MyTextFile.txt");
Console.Write(MyTyextFile.ReadLine());
return sr.ReadLine();
sr.Close();
}
Question 1: The Parameter sr, why do you have it when you dont really use it? (You make your own StreamReader, so why do you ask for one in your method?) A method like this is basically just "Ask for input, do something, give output" public static string ReadTextFromFile(StreamReader sr) The output for that this is a string, which is basically the same as what you have at "MyTextFile.txt" So you are getting back (as output) a sentence You ask for input: public static string ReadTextFromFile(StreamReader sr) Imagine StreamReader is a hammer. You give your method a sentence when it wants a hammer, it will be angry at you (Red lines) Question 2: Why do you use Console.Write in 1 place and Console.WriteLine in another place? Wouldn't it be better to just choose 1 to use Question 3: After you "Give output" ( return sr.ReadLine();) any code outside of that is not ran, so sr.Close(); won't ever be done, what do you plan to do about that?
Birsu
BirsuOP2y ago
For Question 1 my instructor limited me to use it because i do homework i have to use it I still don't understand what to write for input i have to use StreamReader sr so i don't change it and I don't really understand what to write inside the ReadTextFromFile method for calling the method in the main method because StreamReader is involved I'm confused and I can't for question 2 i change that to Console.WriteLine and for question 3 ok so if i'm write sr.Close(); first and then return sr.ReadLine(); does return part ran even if the file is closed?
arion
arion2y ago
I'm pretty sure you can do this while keeping StreamReader in the body of the method
public static string ReadTextFromFile(string path)
{
}
public static string ReadTextFromFile(string path)
{
}
Next, were you taught using statements inside a method yet? (Not the using System.IO;) by the way, since you have the path now from string path you can pass that parameter into the stream reader via new StreamReader(path);
Birsu
BirsuOP2y ago
you mean in the method i use static string ReadTextFromFile(string filePath) {
string text; string filePath = "C:\MyFiles\MyTextFile.txt"; using (StreamReader reader = new StreamReader(filePath)) { text = reader.ReadToEnd(); } return text; } like this?
FusedQyou
FusedQyou2y ago
Why are you even using a StreamReader It's hard as fuck to use streams as a beginner lol c# has a File class that you can use to read/write files
FusedQyou
FusedQyou2y ago
File.ReadAllText Method (System.IO)
Opens a text file, reads all the text in the file into a string, and then closes the file.
File.WriteAllText Method (System.IO)
Creates a new file, write the contents to the file, and then closes the file. If the target file already exists, it is overwritten.
FusedQyou
FusedQyou2y ago
So much easier than messing with streams You can do using var reader = new StreamReader(filePath); without the nesting Alternatively, you can do return reader.ReadToEnd(); Are you forced to use StreamReader? If so, tell your professor that limiting students in what is allowed only works if it would completely remove any needed complexity Streams is unneeded complexity
arion
arion2y ago
They are forced apparently
For Question 1 my instructor limited me to use it because i do homework i have to use it
with File, the method will be a 1-liner
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