Question on classes

  • Thread starter Thread starter Keon
  • Start date Start date
K

Keon

I am trying to write a class, but I got stuck.

When writing a class, how can you call a recursive function that is also in
the class
file, but without having to create a new instance of the class before you
call it.


// This is my class file

using System.Data;
using System.Windows.Forms;

namespace Misc_Utils
{
class InitializeMyDB
{
public void PrepareConnections(........)
{

// prepare the variables here

foreach (......)
{
StoreDB(....);
}
}
}

class StoreIntoToDB
{
public void StoreDB(....)
{

// do my processing here

foreach (......)
{
StoreDB(....);
}
}
}
}

Am I stuck with just having to create a new instance of the
StoreInfoToDB.StoreDB()
everytime I need to make a call to that function?
Or is there a way to encapsulate the two functions and make it one class?


TIA
 
Keon,

Well, in the PrepareConnections method, the call to StoreDB is not
recursive. You will need an instance of the StoreIntoToDB class in order to
make the first call. However, the call that the StoreDB method makes to
itself is recursive.

If you want to avoid creating an instance, you should make the method
static, but you really have to look at what the class does, and determine if
you need the state that an instance provides for you, or just have one
"instance" in it being a static class.
 
I'm not certain as to what you are attempting, but it could be that you
have a misunderstanding as to how to use classes.

I think that what you want is something like

public class DB
{
// define variables etc

public bool Initialize ()
{
// init stuff - setup connection vars etc

return true;
}

public bool Store ()
{
// Store stuff - using your connection vars
return true;
}
}

Then somewhere, for each DB

DB db = new DB ();

db.Initialize ();
db.Store ();
 
Back
Top