Writing to text files

I've tried looking up what the static type means but it just isn't clear to me. Does someone have a good explanation of what that type indicates?
10 Replies
Merineth πŸ‡ΈπŸ‡ͺ
I did read it had something to do with instanciating objects, but it's not sticking
Buddy
Buddyβ€’3mo ago
$static
MODiX
MODiXβ€’3mo ago
In C#, static allows you to have members (classes, methods, etc) that are not tied to any particular instance and are therefore always, globally, accessible. When applying static members, take the following considerations: β€’ If there are to be multiple instances of a class, do not use static β€’ If you need to track state, do not use static β€’ If you are going to have multi threaded workflows, do not use static unless you're aware of the caveats static is best used for stateless methods, extension methods/classes and in areas where you understand the pros/cons. Read more here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static
Angius
Angiusβ€’3mo ago
static members are not attached to any specific instance. They're basically C#'s way of having free functions like what Python, JS, TS, and other languages allow, while still keeping the paradigm of "methods go into classes" Non-static members refer to each specific instance of a class
Merineth πŸ‡ΈπŸ‡ͺ
aaaah that makes it much clearer! Static = 🐐 So if i had a class that was not static. I'd have to create an instance of that class (create an object) in order to utilize it's methods?
Angius
Angiusβ€’3mo ago
You can't use non-static members of a class without having an instance of that class A static class cannot have non-static members in the first place
Pobiega
Pobiegaβ€’3mo ago
O_O
Merineth πŸ‡ΈπŸ‡ͺ
namespace Workshop3.Menus
{
public class WriteTextFileMenu : Menu
{
public WriteTextFileMenu(string name)
: base(name)
{
Add(new MenuItem("Write Text File", new Command<WriteTextFileMenu>(this, r => r.WriteTextFile())));
}

public void WriteTextFile()
{
Console.Clear();
Console.WriteLine("File Location? : ");
string location = Console.ReadLine();
Console.Clear();
Console.WriteLine("Type here followed by Enter : ");
string input = Console.ReadLine();

TextFileWriter.WritingTextToFile(input, location);

}
}
}

namespace Workshop3.IO
{
public class TextFileWriter
{
public static void WritingTextToFile(string input, string location)
{
string text_input = input;
string text_location = location;

File.WriteAllText(text_location, text_input);
}

}
}
namespace Workshop3.Menus
{
public class WriteTextFileMenu : Menu
{
public WriteTextFileMenu(string name)
: base(name)
{
Add(new MenuItem("Write Text File", new Command<WriteTextFileMenu>(this, r => r.WriteTextFile())));
}

public void WriteTextFile()
{
Console.Clear();
Console.WriteLine("File Location? : ");
string location = Console.ReadLine();
Console.Clear();
Console.WriteLine("Type here followed by Enter : ");
string input = Console.ReadLine();

TextFileWriter.WritingTextToFile(input, location);

}
}
}

namespace Workshop3.IO
{
public class TextFileWriter
{
public static void WritingTextToFile(string input, string location)
{
string text_input = input;
string text_location = location;

File.WriteAllText(text_location, text_input);
}

}
}
Does anyone by any chance know why my textfile.txt isn't being modified? It's compiling and running without any problems. Altough no text is being changed
Pobiega
Pobiegaβ€’3mo ago
Have you used the debugger? also, your WritingTextToFile method has a curious issue
string text_input = input;
string text_location = location;
string text_input = input;
string text_location = location;
these are not doing anything of value, just confusing
Merineth πŸ‡ΈπŸ‡ͺ
Yeah haha I kind of realized it’s redundant to use it I’ll try the debugger today (;
Want results from more Discord servers?
Add your server