Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Conversion from C# to VB.NET
Message
De
17/04/2013 14:38:37
 
 
À
17/04/2013 13:50:02
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01571173
Message ID:
01571257
Vues:
48
>>Have you tried using a lock ?
>>
>>In the code below, anyone accessing Tables will block if Tables is being reinitialized
>>
>>
>>	public class AClass
>>	{
>>		private Dictionary<string, string> Tables_;
>>		private object TableLocker = new Object();
>>
>>
>>		public Dictionary<string, string> Tables
>>		{
>>			get 
>>			{
>>				lock (TableLocker)
>>				{
>>					return Tables_;
>>				}
>>			}
>>			private set
>>			{
>>					Tables_ = value;
>>			}
>>		}
>>
>>		public void ReInitalizeTables()
>>		{
>>			lock (TableLocker)
>>			{
>>				Tables_ = new Dictionary<string, string>();
>>				Tables_.Add("1", "one");
>>				Tables_.Add("2", "two");
>>			}
>>		}
>>	}
>>
>>
>
>This seems to be fantastic. I never thought about applying a lock at the reset of an object. I will verify this and will let you know. Thanks


On second thought, you may not need a lock at all - but don't set Tables to null. Create a tmp variable, initialize that, and assign afterwards
Locking gives a bit of overhead
	public class AClass
	{
		private Dictionary<string, string> Tables_;
	
		public Dictionary<string, string> Tables
		{
			get; private set; 

		}

		public AClass()
		{
			// Initialize Tables
			Tables_ = new Dictionary<string, string>();
			Tables_.Add("1", "one");
			Tables_.Add("2", "two");
		}
		public void ReInitializeTables()
		{
			// do not set to null

			Dictionary<string, string> tmp = new Dictionary<string, string>();
			tmp.Add("3", "three");
			tmp.Add("4", "four");

			Tables_ = tmp;

		}
	}
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform