DataTable and DataStorage  
Author Message
thukralz





PostPosted: Visual C# General, DataTable and DataStorage Top

I have two Datatables say Table1 and Table2...

and both contain some data....I passed this data to the below class....

private DataTable errorStore = null;

private DataTable infoStore = null;

public DataTable TheErrors

{

get { return this.errorStore; }

set { this.errorStore = value; }

}

public DataTable TheInfo

{

get { return this.infoStore; }

set { this.infoStore = value; }

}

public DataStorage(DataTable errData, DataTable perInfo)

{

this.errorStore = errData;

this.infoStore = perInfo;

}

public DataStorage()

{

//

// TODO: Add constructor logic here

//

}

//Instantiate the class and pass the data

store.TheErrors = errorStore;

store.TheInfo = perInfo;

// Now If some other of my class want to use these datatables which got stored there How they can use it..



Visual C#14  
 
 
Geert Verhoeven





PostPosted: Visual C# General, DataTable and DataStorage Top

Hi,

What is the context of the class you are creating If you want to have only one instantiation of your tables, it is best to use a singleton pattern. The singleton pattern, makes sure that only one instance of your object is created.

Here is an example of the singleton pattern:
http://msdn2.microsoft.com/en-us/library/ms998558.aspx

Greetz,

Geert

Geert Verhoeven
Ausy Belgium

My Personal Blog



 
 
Harsimrat





PostPosted: Visual C# General, DataTable and DataStorage Top

Yes, I guess I only need to instantiate class only once and if I do it again and again, it will make new instances. I went throught that example before couldn't get my work accordingly.

If possible can you please provide me an example using my code

Really appreciate.

Harsimrat


 
 
Harsimrat





PostPosted: Visual C# General, DataTable and DataStorage Top

Got it...my bad..

Thanks,

Harsimrat


 
 
Geert Verhoeven





PostPosted: Visual C# General, DataTable and DataStorage Top

This is the code:

public sealed class DataStorage
{
private static DataStorage _instance = null
;
private DataTable _errorStore = null
;
private DataTable _infoStore = null
;

public DataTable
ErrorStore
{
get { return
_errorStore; }
}

public DataTable InfoStore
{
get { return
_infoStore; }
}

private
DataStorage()
{
// Initialize your datatables here:
_errorStore = new DataTable
();
_infoStore = new DataTable
();
}

public static DataStorage Instance
{
get
{
if (_instance == null
)
{
_instance = new DataStorage
();
}

return
_instance;
}
}
}

Greetz,

Geert

Geert Verhoeven
Ausy Belgium

My Personal Blog



 
 
Gabriel Lozano-Moran





PostPosted: Visual C# General, DataTable and DataStorage Top

Please not that the above code is not thread-safe. I recommend reading Jon Skeet's famous article on implementing the singleton pattern in C#:



 
 
Harsimrat





PostPosted: Visual C# General, DataTable and DataStorage Top

That article was really good..