I come from a procedural world so bear with me on this. I am not sure of the “proper” terminology. I get functions – classes not so much (I gather they are blue prints but I can’t convince myself to use them yet, probably because I do not understand how helpful they can be)
I am attempting to create a C# console application (in VS2010). It acts in a procedural nature (starts from top, may reference a function every once in a while or try catch and then exits). My issue is that there will be say 4 pieces to the application – the portion that defines what the sql strings are, the portion that connects to the database, the portion that executes sql statements and the portion that does extra calculations based on the results.
I’m trying to determine a way of splitting up these chunks of code into different files that I can call from – essentially keeping the default “program.cs” from being too bulky. My code up to date works its just a beating to scroll through to pick out items.
Suggestions?
(I understand this is very subjective)
[addition]
The structure of my code right now looks similar to the following – note, [[[[[ ]]]]] are what I refer to as chunks:
[[[[[
string sqlQueryFetchCalls = "sql lives here";
... and more strings for sql... and more... and more
]]]]]
SqlConnection mssqlConnection = new SqlConnection(params);
try{
mssqlConnection.Open();
System.Console.WriteLine("connection opened");}
catch (Exception errorVar){
output.Text = errorVar.ToString();}
[[[[[
SqlDataReader results = null;
SqlCommand exec_sqlQueryFetchCalls = new SqlCommand(sqlQueryFetchCalls, mssqlConnection);
results = exec_sqlQueryFetchCalls.ExecuteReader();
while (results.Read())
{
// do something
}results.Close();
... again... again ... again... again, though the resulting actions are quite different each time ]]]]]
mssqlConnection.Close();
While this isn’t the best way to arrange your code, it can be done.
If you are using
staticmethods just make sure that all the methods are in the same namespace. Then they will all be able to see each other.You can use a single class an use the
partialkeyword to split the definition across several files.However, I would suggest that you read up more on classes and how they are organised as you will find it a lot easier to manage your code if you use them.