General newbie type question
-
Since I lack a mentor I have to ask these basic structure question here. Good guidance is really appreciated. Please be advised that I am thinking of both Python and C# structure; but generalities are good too: Classes: What should go into a class and what should stay out? -I am converting a program to have an OOP structure the program prints out processed results to a file line by line. Should my class for this return the processing results to the main program and then use a function or another class to print the results? -Is it acceptable to have a class return some result but then call on the same class object to process the next logical step of the process? This seems right to me since built in functions seem to do this. eg: Have MyClass read the first line in a file, return it Have the main print the line Have the main call MyClass again but have it read the next line in the object Should I just forget about "functions" and "sub-routines" and throw everything into classes? TIA, The shwa guy
-
Since I lack a mentor I have to ask these basic structure question here. Good guidance is really appreciated. Please be advised that I am thinking of both Python and C# structure; but generalities are good too: Classes: What should go into a class and what should stay out? -I am converting a program to have an OOP structure the program prints out processed results to a file line by line. Should my class for this return the processing results to the main program and then use a function or another class to print the results? -Is it acceptable to have a class return some result but then call on the same class object to process the next logical step of the process? This seems right to me since built in functions seem to do this. eg: Have MyClass read the first line in a file, return it Have the main print the line Have the main call MyClass again but have it read the next line in the object Should I just forget about "functions" and "sub-routines" and throw everything into classes? TIA, The shwa guy
shwaguy wrote:
Classes: What should go into a class and what should stay out?
Everything in C# MUST be in a class.
shwaguy wrote:
Should my class for this return the processing results
and
shwaguy wrote:
Is it acceptable to have a class return some result but then call on the same class
Classes in C# can not return values, methods (functions) and properties (think externally accessible variable) can.
shwaguy wrote:
Should I just forget about "functions" and "sub-routines" and throw everything into classes?
your functions have to be contained in a class For an example all of these sources do the same thing
namespace myApp
{
class mainclass
{
static void Main(string[] args)
{
My_Function_();
}
public static void My_function_()
{
Console.WriteLine("My \"Function\" was called");
}
}
}namespace myApp
{
class mainclass
{
static void Main(string[] args)
{
myclass.My_Function_();
}
}
class myclass
{
public static void My_function_()
{
Console.WriteLine("My \"Function\" was called");
}
}
}namespace myApp
{
class mainclass
{
static void Main(string[] args)
{
anothernamespace.myclass.My_Function_();
}
}
}
namespace anothernamespace
{
class myclass
{
public static void My_function_()
{
Console.WriteLine("My \"Function\" was called");
}
}
}
-Spacix All your skynet questions[^] belong to solved
-
shwaguy wrote:
Classes: What should go into a class and what should stay out?
Everything in C# MUST be in a class.
shwaguy wrote:
Should my class for this return the processing results
and
shwaguy wrote:
Is it acceptable to have a class return some result but then call on the same class
Classes in C# can not return values, methods (functions) and properties (think externally accessible variable) can.
shwaguy wrote:
Should I just forget about "functions" and "sub-routines" and throw everything into classes?
your functions have to be contained in a class For an example all of these sources do the same thing
namespace myApp
{
class mainclass
{
static void Main(string[] args)
{
My_Function_();
}
public static void My_function_()
{
Console.WriteLine("My \"Function\" was called");
}
}
}namespace myApp
{
class mainclass
{
static void Main(string[] args)
{
myclass.My_Function_();
}
}
class myclass
{
public static void My_function_()
{
Console.WriteLine("My \"Function\" was called");
}
}
}namespace myApp
{
class mainclass
{
static void Main(string[] args)
{
anothernamespace.myclass.My_Function_();
}
}
}
namespace anothernamespace
{
class myclass
{
public static void My_function_()
{
Console.WriteLine("My \"Function\" was called");
}
}
}
-Spacix All your skynet questions[^] belong to solved
SpacixOne wrote:
Everything in C# MUST be in a class.
What about Structures?
"It was the day before today.... I remember it like it was yesterday." -Moleman
-
SpacixOne wrote:
Everything in C# MUST be in a class.
What about Structures?
"It was the day before today.... I remember it like it was yesterday." -Moleman
Well, there wasn't a question about a struct... but A structure can't inherit from a class (and vice versa) and a struct is created then dies when reaching the closing brace. It is a different animal but no it doesn't have to be defined within a class, but if you use it (write or read a member) that HAS to be inside a class... More C# syntax can be found here http://msdn2.microsoft.com/en-us/library/618ayhy6(vs.80).aspx[^]
-Spacix All your skynet questions[^] belong to solved
-
Since I lack a mentor I have to ask these basic structure question here. Good guidance is really appreciated. Please be advised that I am thinking of both Python and C# structure; but generalities are good too: Classes: What should go into a class and what should stay out? -I am converting a program to have an OOP structure the program prints out processed results to a file line by line. Should my class for this return the processing results to the main program and then use a function or another class to print the results? -Is it acceptable to have a class return some result but then call on the same class object to process the next logical step of the process? This seems right to me since built in functions seem to do this. eg: Have MyClass read the first line in a file, return it Have the main print the line Have the main call MyClass again but have it read the next line in the object Should I just forget about "functions" and "sub-routines" and throw everything into classes? TIA, The shwa guy
shwaguy wrote:
What should go into a class and what should stay out?
In: Students who have an aptitude for the subject and really want to learn. Out: Students who think the subject is an "easy A" and that "anyone can do it".
-
Since I lack a mentor I have to ask these basic structure question here. Good guidance is really appreciated. Please be advised that I am thinking of both Python and C# structure; but generalities are good too: Classes: What should go into a class and what should stay out? -I am converting a program to have an OOP structure the program prints out processed results to a file line by line. Should my class for this return the processing results to the main program and then use a function or another class to print the results? -Is it acceptable to have a class return some result but then call on the same class object to process the next logical step of the process? This seems right to me since built in functions seem to do this. eg: Have MyClass read the first line in a file, return it Have the main print the line Have the main call MyClass again but have it read the next line in the object Should I just forget about "functions" and "sub-routines" and throw everything into classes? TIA, The shwa guy
OK, So the message I seem to be getting is that, anything can be done from anywhere as long as it works. So if it seems write to have MyClass.Function0 return a value and dump that value to a file it is technically OK. Aside from logical organization is there anything style wise I should know? For instance it would not seem right to have MyClass.Function both return a value and print those same values; I seems right to have a seperate class.function take the return values from MyClass.Function and do the writing. That would allow me to have multiple funcitons access my file writing function and eliminate repeat code. Is that right? The shwa guy