|
Saksida Bojan wrote: No such thing.
What are you talking about? Of course there is. You might find reviewing this[^] useful.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I didn't know it was even possible.
|
|
|
|
|
I wouldn't use the terminology "static constructor" (as ALL constructors are NECESSARILY static - you cannot invoke an instance method until you've constructed an intance). But there certainly is something that (in C#) uses the keyword "static" followed by same syntax as a constructor - the class initializer.
Class initializers can be used if you want to perform initialization of some kind once before a type can be used. For example, let's say you are making a bookmaker system. The prices (odds) may vary from 1.01 to 1000, but not everything in between is legal. Instead you have something like this:
From To Increment
1.01 2.00 0.01
2.02 4.00 0.02
4.04 10.00 0.05
....
500 1000 10
You could use an Int32 to store the integral number of 1/100 units, i.e. 125 for 1.25, and cover more than the range you need. But if there are only 256 legal prices you could store it in just a byte. This could make a big difference when tens of thousands of transactions per hour are to be handled, especially in terms of writing data to a database.
Anyway, it doesn't really matter for our purposes whether or not the representation is useful! The point is this is a good candidate for a type initializer. Given a byte value of 135, we need to find price number 135 of the legal prices. So we can equip the class with an array and initialize the type before it's used like this:
public class OddsPrice
{
static int[] priceArray;
static OddsPrice()
{
priceArray = new int[256];
int price = 100;
int i = 0;
while ((price += 1) <= 200) priceArray[i++] = price;
while ((price += 2) <= 400) priceArray[i++] = price;
...
}
byte value;
public int Value
{
get { return priceArray[value]; }
}
}
The type initializer will be called automatically at an undefined time (in current implementations of the CLR I believe it happens when the assembly is loaded, but the spec says undefined so don't assume anything) prior to the type being used at run-time.
The advantage is that you do not need to handle any thread-syncing to ensure it runs once and only once and before anything else on the type can be used.
|
|
|
|
|
Have a look on MSDN[^]
Hope this helps.
|
|
|
|
|
|
If you have a static class, or there is init for the class which must be done once per application (rather than done once for each instance of the class) then use a static constructor.
See here: Static Constructor[^]
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
Thank you, this was helpful
Marcus Abrahão
|
|
|
|
|
Hi,
I have written a complex program in c#.net.
After the program is running for a long time by user interactions, its memory usage will increase to the limit of the system, then error will occur. How can the program alert the user that the memory usage is nearly close to its limit before it goes into error ?
I have tried memory garbage collection but the program is too complex that garbage collection cannot clean up the memory usage effectively.
Thanks
|
|
|
|
|
Kim0618 wrote: After the program is running for a long time by user interactions, its memory usage will increase to the limit
This is not good, I suspect that somewhere maybe stuff gets put into a collection of some kind and never removed.
I suggest getting a memory profiler to point out these flaws for you.
Hope you figure it out
Harvey Saayman - South Africa
Software Developer
.Net, C#, SQL
you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111
|
|
|
|
|
Agreed. Don't understand why anyone would vote that a bad answer.
Something which just keeps growing over time needs reviewing.
Regards,
Rob Philpott.
|
|
|
|
|
You're probably not disposing of something properly, or you're not maintaining a collection of object properly, or any number of other causes.
You need to look at your app run with a profiler to find out what's going on.
This is something that you should have been testing for before it got deployed to production.
Kim0618 wrote: I have tried memory garbage collection but the program is too complex that garbage collection cannot clean up the memory usage effectively.
This has nothing to do with the GC's inability to do it's job. It can still do it's job very effectively, even on the most complex of applications.
|
|
|
|
|
There are many ways to run out of memory. The most obvious one is keeping things around that you no longer need but for some reason still hold on to.
.NET sometimes has a problem with the "large object heap" (LOH) which never gets compacted, and may run out of space. A popular culprit is a collection growing larger and larger. As soon as it hold 20,000+ objects, the reference array is a large object (as it exceeds 80KB); any time it needs more capacity, it gets reallocated with twice its size. The space occupied by the old array is freed, but will not be reused by that same array as it keeps growing.
Of course the objects themselves in the collection also take memory; that may or may not come from the LOH, depending on their size.
If, I don't know you do, but if you are building a cache of some sort, you should keep its size in check, and probably use WeakReference techniques.
|
|
|
|
|
Thanks for your answer !
But what is the weak reference technique that you have mentioned ?
|
|
|
|
|
You're welcome.
You might want and read up on WeakReference class.
|
|
|
|
|
Hi everyone im actually working on an application which migrates the datas that are present in a xml file to my database.. Im coding it on c#.net.. The issue is in my xml file we have customers name which is fetched from the xml file and then inserted into the database.. so most of my customers name have appostrophe(') in their name so during execution of the insert query it throws me odbc exception the insert query that i use is as follows:
insert into EACS_User_1 values('"+uf_Id+"','"+uf_Title+"','"+uf_FirstName+"','"+uf_MiddleName+"','"+uf_LastName+"','"+uf_PhoneNumber+"','"+uf_MobileNumber+"','"+uf_EmailID+"')
here the first name contains appostrophe say for example the name is Moh’d Husam how do i insert such names... Do guide me...
Thanx in advance..
Regards,
Tash
|
|
|
|
|
Hi,
The resolution to your problem and also the approach to avoid sql injection is to use prepared statements. Have a look at this blog (especially the February 18 2006). There you can find an example how to do this in c#.
Regards
Sebastian
|
|
|
|
|
Which blog????
|
|
|
|
|
|
Thanx but i hav a doubt.. actually i googled and i found out that we can use strX.replace("'","''") something like this... but i dont know how to place it in my insert query....
Thanx in advance
Regards,
Tash
|
|
|
|
|
Well e.g. instead of uf_id you write: uf_id.Replace("'","''").
That's it. But again, this is not the way you should insert data into your sql server.
Post the code where you execute the statement and we can show you where to put in the prepared statement.
|
|
|
|
|
well my code is:
XmlDataDocument doc9 = new XmlDataDocument();
doc9.Load(ConfigurationSettings.AppSettings["usr"]);
XmlNodeList nodes9 = doc9.GetElementsByTagName("Row");
foreach(XmlNode node9 in nodes9)
{
if(node9.HasChildNodes && node9.ChildNodes.Count > 0)
{
XmlElement Element9 = (XmlElement) node9;
string u_Id = Element9.GetElementsByTagName("Id")[0].InnerText;
string u_Title = Element9.GetElementsByTagName("Title")[0].InnerText;
string u_FirstName = Element9.GetElementsByTagName("FirstName")[0].InnerText;
string u_MiddleName = Element9.GetElementsByTagName("MiddleName")[0].InnerText;
string u_LastName = Element9.GetElementsByTagName("LastName")[0].InnerText;
string u_FileName = Element9.GetElementsByTagName("FileName")[0].InnerText;
if(u_Id==ugf_UserId)
{
string path4 = ConfigurationSettings.AppSettings["usrfile"]+"//"+u_FileName+".xml";
if (File.Exists(path4))
{
XmlDataDocument doc10 = new XmlDataDocument();
doc10.Load(ConfigurationSettings.AppSettings["usrfile"]+"//"+u_FileName+".xml");
XmlNodeList nodes10 = doc10.GetElementsByTagName("User");
foreach(XmlNode node10 in nodes10)
{
if(node10.HasChildNodes && node10.ChildNodes.Count > 0)
{
XmlElement Element10 = (XmlElement) node10;
string uf_Id = Element10.GetElementsByTagName("Id")[0].InnerText;
string uf_Title = Element10.GetElementsByTagName("Title")[0].InnerText;
string uf_FirstName = Element10.GetElementsByTagName("FirstName")[0].InnerText;
string uf_MiddleName = Element10.GetElementsByTagName("MiddleName")[0].InnerText;
string uf_LastName = Element10.GetElementsByTagName("LastName")[0].InnerText;
string uf_Type = Element10.GetElementsByTagName("Type")[0].InnerText;
string uf_PhoneNumber = Element10.GetElementsByTagName("PhoneNumber")[0].InnerText;
string uf_MobileNumber = Element10.GetElementsByTagName("MobileNumber")[0].InnerText;
string uf_EmailID = Element10.GetElementsByTagName("EmailID")[0].InnerText;
string uf_Branch = Element10.GetElementsByTagName("Branch")[0].InnerText;
string uf_Department = Element10.GetElementsByTagName("Department")[0].InnerText;
string uf_NoOfLoginAttempts = Element10.GetElementsByTagName("NoOfLoginAttempts")[0].InnerText;
string uf_Status = Element10.GetElementsByTagName("Status")[0].InnerText;
if(uf_Id==u_Id)
{
cmd = new OdbcCommand("select UserID from eacs_user_1 where userid ='"+uf_Id+"'" ,con);
rdr = cmd.ExecuteReader();
if(rdr.Read())
{
rdr.Close();
}
else
{
rdr.Close();
Console.WriteLine("{0}",uf_Id);
cmd = new OdbcCommand("insert into EACS_User_1 values('"+uf_Id+"','"+uf_Title+"','"+uf_FirstName.Replace("'","''")+"','"+uf_MiddleName+"','"+uf_LastName+"','"+uf_PhoneNumber+"','"+uf_MobileNumber+"','"+uf_EmailID+"','"+uf_Branch+"','"+uf_Department+"','"+uf_NoOfLoginAttempts+"','"+uf_Status+"','','','','','','')",con);
cmd.ExecuteNonQuery();
}
}
else
{
Console.WriteLine("Please check User file-{0} fields do not match",u_FileName);
}
}
}
}
else
{
Console.WriteLine("User file {0} not present",u_FileName);
}
}
}
}
This is only a part of my code that i have posted...
Thanx in advance..
Regards,
Tash
|
|
|
|
|
Okay, so change the line where you build the command like this:
<br />
<br />
cmd = new OdbcCommand("insert into EACS_User_1 VALUES (@Id,@Title,@FirstName,...)", con);<br />
cmd.Parameters.AddWithValue("@Id", uf_id);<br />
cmd.Parameters.AddWithValue("@Title", uf_Title);<br />
cmd.Parameters.AddWithValue("@FirstName", uf_FirstName);<br />
.<br />
.<br />
.<br />
cmd.ExecuteNonQuery()<br />
<br />
That should do the work.
Regards
Sebastian
|
|
|
|
|
Actually i only find cmd.parameters.add i mean i dont get addwithvalue... should i include anything??? is it because i am using VS2003???
Thanx in advance..
Regards,
Tash
|
|
|
|
|
You are using .NET 2.0, that is why there is no AddWithValue-method. Instead of AddWithValue use Add like this:
cmd.Parameters.Add(new OdbcParameter("@Id", uf_Id));
Regards
Sebastian
|
|
|
|
|
I did as u said but i receive the following Exception:
System.Data.Odbc.OdbcException: ERROR [HY000] [INTERSOLV][ODBC SQL Server driver][SQL Server]Must declare variable '@Id'.
at System.Data.Odbc.OdbcConnection.HandleError(HandleRef hrHandle, SQL_HANDLE hType, RETCODE retcode)
at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method)
at System.Data.Odbc.OdbcCommand.ExecuteNonQuery()
at ConsoleApplication1.Class1.perform() in c:\documents and settings\900846\my documents\visual studio projects\consoleapplication1\class1.cs:line 635
Thanx in advance...
Regards,
Tash
|
|
|
|
|