You'll probebly see me making a million threads like this, lol. I love talking theory. I dont have a lot of expierence with seeing other peoples code so I don't know how things are done by others. Anyways, I was wondering if this theory is acceptable to most programmers. Is it good/bad practice to create all of your objects and then allow them to be used by every other object? I know that sounds confusing. Look below for an example... As you can see I define my objects in advanced and then inherit all of my classes on the main one. That way I can call all of my objects from inside the objects themselves. I actually found this to be pretty easy in some cases. Although I question whether it is acceptable among other programmers. Although this is good in theory, it may prove a little difficult to manipulate it if you had more than instance of a class because of the issues involving which object to manipulate, but if you are careful, I am sure you could work around it. Although if you are only using one instance of a class, it certainly saves you the time of having to pass the objects as parameters and then return them.
// MAIN
public class MainClass
{
public SubClass obj1;
public AnotherClass obj2;
public static void Main()
{
obj1 = new Subclass();
obj2 = new AnotherClass();
obj1.Display(); // <-- basically the same as obj2.Output();
}
}
// SubClass + AnotherClass
public class SubClass : MainClass
{
public void Display()
{
obj2.Output();
}
}
public class AnotherClass : MainClass
{
public void Output()
{
Console.WriteLine("I'm AnotherClass being called from SubClass!!!");
}
}