✅ Good way to access methods from other classes?
I'm using separate class files to try and organize my code, as my code contains alot of stuff for specific things. Currently I'm accessing these seperate Class files by having all of them in the same namespace, and having a public class that has them all instanced. Problem is, I cannot use this same method for getting these classes in the MainForm, as for some reason it breaks my code. What are some ways you guys would recommend referencing a separate class file?
8 Replies
First, you got to understand that the file structure is irrelevant. Once compiled, you have assemblies, with namespaces. That's it.
After that, you have to think about access modifiers (public, private, protected, internal, etc.). These restrict access to what can access it (properly). The namespace is not relevant at that step though, only the assembly, and if a class is part of another class for example.
This page here will contain more specific information regarding this: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers
Maybe I'm misunderstanding, but to access methods/variables from another file I have to instance it, which is typically done by using something like
classFile specifiedName = new classFile();
right? This has worked for me until I've needed the MainForm to access a specific method. I have a method in my Command class file that runs a command, if said command is found in a list, which commands are added to when the application starts. Problem is, since MainForm creates a new instance of the class file, I believe that Command list is never built, so the command never runs. I've tried running commands from every other class file and it works, but only from the MainForm does running the command never work, I'll show my code abit more in a secIt depends. If it's an instance member, yes. If it's static, or a constant, no.
And also: As I said, the notion of "file" is irrelevant at this point. Remove it from the equation because it's just noise.
But yes, some code would help understanding your issue.
Okay so, every class is under the same namespace, which allows them to use this class that specifies all the instanced classes, so all classes access the same classes without instancing new ones
I currently have the MainForm under a different namespace, as for some reason putting it in the same namespace as my other classes breaks my code, idk why exactly maybe I need to look into it sometime. This class is under the MainForm namespace to give the MainForm access to all the classes, but this instances the other classes again. Now don't get me wrong I am way out of my depth here so idrk what I'm talking about, but I believe this means that the classes that the MainForm and other classes access are basically different classes. When my app loads I typically have the Spark class handle everything, which has worked fine, one thing it did is load a bunch of commands from the Command class into a list. If I have any other class run the RunCommand method from my Command class it works perfectly fine (RunCommand only runs code if the provided command is found in the commandList) but when the RunCommand method is accessed from the MainForm, it doesn't work. I used a foreach loop to print the values of the commandList in the Commands class, and if ran from any other class it prints all the commands I added, but if ran from the MainForm it prints nothing, as if no commands were added
Sorry if that's too much to read or incomprehensible, I do have a Github if you'd prefer to have a look at that
I mentioned earlier that the namespace is not relevant either at this point to determine if something can access or not.
Right now, what you're doing is basically a large class containing a reference to one instance of each of these 6 classes. If you instantiate one of these classes elsewhere, it won't be the same instance as this one.
Oh, my apologies
My guess would be that your
MainForm
probably instantiate it's own instance, so it's not shared with what's in your Classes
class.
Can't know for sure without the actual code, but I don't have time to look into it in details tonight; maybe someone else does, but I can't speak for them.Yeah you're probably right
I mean, I found a work around by having the Spark class send the command to the Command class instead of the MainForm, since the Spark class is the one that instantiates all of the classes anyways. Not exactly how I'd prefer it but it works. Sorry for being hard to work it, I'm abit out of my depth and I should definetely go out of my way to learn abit more. Thanks for all of the information you provided though! I'm sure it will prove rather useful