✅ 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
How about you ask a question about what you are stuck on
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
surely your instructor when over how to use methods in class, or you have materials on how to create a method
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
It gives an error but i don't know how to fix it
You don't specify the type of the parameter when calling a method
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);
I change that this way but if i open the file its empty how can i solve this problem?
You seem to be calling ReadLine twice?
Are you sure the text you are writing to the file is not actually an empty string?
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
What is the console output of your program?
and file is empty always
"Original message contains characters" <- see anything wrong with that output?
It's an empty string
Which you are writing to the file
I write my message is this text but its up to user
it changes
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
where am i calling ReadLine twice?
😐
is something wrong with writeData.Dispose
or its correct
You should really use
using
instead of calling Dispose
directly, but it has nothing to do with the problem.here i want to get the message from the user the first ReadLine and i create an a value to return that message
Remove the first call to ReadLine
See what happens
ok
oh it works but wrong way
it directly say what i write
not the contain characters
The code is doing exactly what you tell it to
but i want it to write how many characters the message is
how can i do that?
Use the
Length
property of the stringok i did and it works tysm
Here the program will read the text in the file
Am i correctly writing this?
And i have a problem with this again
Do you think trying to read a file after you close it is going to work?
Read the error message
if i am write at the end its give a warning
so i replace it
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.What should i write inside the ReadTextFromFile method?
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:
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 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?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?
I'm pretty sure you can do this while keeping StreamReader in the body of the method
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);
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?
string text; string filePath = "C:\MyFiles\MyTextFile.txt"; using (StreamReader reader = new StreamReader(filePath)) { text = reader.ReadToEnd(); } return text; } like this?
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 filesYou will then have these: https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readalltext?view=net-7.0 and https://learn.microsoft.com/en-us/dotnet/api/system.io.file.writealltext?view=net-7.0
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.
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 complexityThey are forced apparently
For Question 1 my instructor limited me to use it because i do homework i have to use itwith File, the method will be a 1-liner
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.