|
Hi,
JoeRip wrote: That confuses me some. I want my struct to have usable default values.
To test struct initialization you had to use a class member "error", not a local variable.
The compiler insists local variables are initialized before they are used.
A class member "error" will have all its members initialized to zero/null/Nothing...
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- use PRE tags to preserve formatting when showing multi-line code snippets
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
"
this default constructor is only invoked if the struct is instantiated with new.
"
|
|
|
|
|
AH! That's my error, thanks. For a minute there I thought I was fundamentally misunderstanding what structs were for.
|
|
|
|
|
I just found out myself. I spent most of the day Sunday wrestling with a struct .
It should be possible to define a default constructor for struct s dagnabit!
|
|
|
|
|
Hi experts,
(I hope I didn't post my question in the wrong category of the forum)
I am using in C#, Asp.Net, SQL Server for my project.
I have a situation whereby I need to update a primary key.
In my table, I have these columns:
[U]proj_id[/U], emp_name, emp_email
I have the following method
public static void updatePID(String newpid,String oldpid)
{
SqlConnection conn = new SqlConnection(connString);
SqlCommand comm = new SqlCommand("Update table_name set proj_id = newpid where proj_id = oldpid");
conn.Open();
comm.Connection = conn;
comm.Parameters.AddWithValue("@projectid", newpid);
comm.Parameters.AddWithValue("@projectid", oldpid);
comm.ExecuteNonQuery();
conn.Close();
}
I have a problem with the following two statements.
comm.Parameters.AddWithValue("@projectid", newpid);
comm.Parameters.AddWithValue("@projectid", oldpid);
My connection with the database and the variables "newpid" and "oldpid" values are correct.
May I know if there is any other methods to update a primary using the old primary key? Thank You in Advance.
serene
|
|
|
|
|
Ummm... how about:
...
SqlCommand comm = new SqlCommand("Update table_name set proj_id = @newpid where proj_id = @oldpid");
...
comm.Parameters.AddWithValue("@newpid", newpid);
comm.Parameters.AddWithValue("@oldpid", oldpid);
...
|
|
|
|
|
Thanks but it's still working. There isn't any update in the database although there is no compliation or runtime error.
Er.... I guess there is something wrong with my logic? Please help..
protected void Page_Load(object sender, EventArgs e)
{
// It did display the first(old) project id into the textbox.
String projectID = (String)Session["projId"];
txtProjId.Text = projectID;
}
protected void btnConfirm_Click(object sender, EventArgs e)
{
//get project id
String newpid = txtProjId.Text;
String oldpid = (String)Session["projId"];
ProjectDAO.updatePID(newpid, oldpid);
}
I wanted to display the project Id that the user has first entered. It was saved in the database.
Then the system allows them to change the project Id at another page.
Erm.. and the only reason I can think of it that "String newpid = txtProjId.Text; " values is the old value instead.
Any idea how to make this work?
Thanks. Sorry for the trouble.
serene
|
|
|
|
|
Try this:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
String projectID = (String)Session["projId"];
txtProjId.Text = projectID;
}
}
Hope it helps
There are 10 kinds of people: those who understand binary and those who don't
|
|
|
|
|
A big thanks to both of you.
It's working already!
Thanks again
;)
serene
|
|
|
|
|
Pfft:
txtProjId.Text = (String)Session["projId"];
Stupid needless local variable.
|
|
|
|
|
|
Hi, I want to be able to send keystroke to anther app that is based directx. I am using SendInput for the major part of the keys, but there are serveral keys that dont work with this fuction. (most of the ones that dont, calls a action of some sort). I just woundering if my next step is to start looking into directx api hooking or is there another way that i can send the keystroke over the sendinput that might work. Also i have tried the Sendkeys function but all it gets is the same key action on all the keys (whish is the ESC key).
thank you very much
Danzar
|
|
|
|
|
How to work TTS in arabic
write sentence arabic and speak whin i need that
|
|
|
|
|
Your inquiry goes way beyond the scope of a forum post. Try google searches
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
Hi there,
I am making a stock take application for MC50 Pocket PC which runs on Windows 2003 Mobile OS. I have written some code to update stock after every qty entered for a stock item. This function updates the database (.sdf file on PDA) with the entered qty. I have found out that if battery runs out or if the PDA is hard rebooted WHILE doing stock take, my stock take is not saved at all. Seems like the data is rolled back or is not committed when PDA is rebooted while doing stock take. Please help me to save data every time i enter qty for a stock item even if the PDA is hard rebooted. Thanks very much. - KARAN
My code is:
"
#region Update Stock Take by Ref number
///
/// updates stock take table with new qty by ref number
///
/// <param name="ref_number" />
/// <param name="qty" />
/// <returns>
internal string UpdateStockTakeByRefNumber(string ref_number,decimal qty, string column)
{
string sql = "UPDATE Stock_Take SET " + column +" = ? , Updated_By_PDA = 1 WHERE Ref_Number = ?";
try
{
SqlCeHelper.ExecuteScalar(cnStockLink,sql, new SqlCeParameter(column,qty), new SqlCeParameter("Ref_Number",ref_number));
}
catch (SqlCeException e)
{
MessageBox.Show(e.Message.ToString());
}
return "Y";
}
#endregion
"
|
|
|
|
|
I just had an idea and I'd like your opinion.
When you use "for" cycles you have to use lock if more than one thread is executing that part because they may interfere with each other when incrementing the cycle-variable. You have to use lock():
lock(lockObject) {
for(int i = 0; i<x; i++)
{="" do="" work="" }="" }
my="" solution
first="" i="" use="" thread.name="" to="" name="" my="" threads="" with="" an="" int="" in="" a="" cycle,="" like="" this:
for(int="">threadArray.Length; i++)
{ threadArray[i].Name = i.toString(); }
I have to create an array of integers, with the same number of elements as the threads.
int[] intArray = new int[numOfThreads];
Finally my for looks like this:
for(; intArray[Convert.ToInt32(Thread.CurrentThread.Name)]) < x; Interlocked.Increment(ref intArray[Convert.ToInt32(Thread.CurrentThread.Name)]))
{ //Do work }
This way multiple threads can access a cycle instead of waiting until the running thread exits the lock. What do you think?
|
|
|
|
|
szolDat wrote: When you use "for" cycles you have to use lock if more than one thread is executing that part because they may interfere with each other when incrementing the cycle-variable.
Not at all. The variable is allocated on the stack, so it's local to each thread.
I don't know where you got that idea, but either you misread something, or you have read something written by a very confused person.
szolDat wrote: This way multiple threads can access a cycle instead of waiting until the running thread exits the lock. What do you think?
I think that you have totally misunderstood the problems with thread safety.
You have "solved" a problem that doesn't exist, and created a problem as the access to the array no longer is syncronised.
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
"You have "solved" a problem that doesn't exist"
Please explain why the following situation isn't possible (I'm somewhat clear about the heap and the stack):
Thread1 enters the cycle, increments i by 1.
The control is passed to Thread2 and it enters the cycle too, incrementing i by 1. i now equals 2.
Thread1 gets control again and tries to access an array's element (or perform any action) using the cycle variable. There you have it, a race condition where the value of i depends on the order of the threads' access to the cycle.
Another thing is, every calculation will give bad results which depends on the number of executions and there are variables involved which are mutually modifiable by the threads. In this situation you have to use a lock, or you can use the method I proposed.
Thank you for your attention.
|
|
|
|
|
Have you tried it the other way? Or just believed some rubbish that was floating around the Internet?
Because your i is defined within the method, each invocation will have its own i , not one shared one. Thread safety comes into effect with fields -- items that are defined in a class or struct.
Here, I whipped up a little (!) demo:
namespace Template
{
public partial class Template
{
private int[] values ;
private int classi ;
public Template
(
int HowMany
)
{
this.values = new int [ HowMany ] ;
for ( int i = 0 ; i < HowMany ; i++ )
{
this.values [ i ] = i ;
}
return ;
}
public void
PrintLocal
(
object Format
)
{
for ( int locali = 0 ; locali < this.values.Length ; locali++ )
{
System.Console.Write
(
(string) Format
,
this.values [ locali ]
) ;
}
return ;
}
public void
PrintClass
(
object Format
)
{
for ( this.classi = 0 ; this.classi < this.values.Length ; this.classi++ )
{
System.Console.Write
(
(string) Format
,
this.values [ this.classi ]
) ;
}
return ;
}
public void
PrintClassWithLock
(
object Format
)
{
lock ( this.values )
{
for ( this.classi = 0 ; this.classi < this.values.Length ; this.classi++ )
{
System.Console.Write
(
(string) Format
,
this.values [ this.classi ]
) ;
}
}
return ;
}
[System.STAThreadAttribute]
public static int
Main
(
string[] args
)
{
int result = 0 ;
Template t = new Template ( 100 ) ;
try
{
System.Threading.Thread a = new System.Threading.Thread ( t.PrintLocal ) ;
System.Threading.Thread b = new System.Threading.Thread ( t.PrintLocal ) ;
a.Start ( " A({0:00})" ) ;
b.Start ( " B({0:00})" ) ;
while ( a.IsAlive && b.IsAlive )
{
System.Threading.Thread.Sleep ( 100 ) ;
}
System.Threading.Thread c = new System.Threading.Thread ( t.PrintClass ) ;
System.Threading.Thread d = new System.Threading.Thread ( t.PrintClass ) ;
System.Console.WriteLine() ;
System.Console.WriteLine() ;
c.Start ( " C({0:00})" ) ;
d.Start ( " D({0:00})" ) ;
while ( c.IsAlive && d.IsAlive )
{
System.Threading.Thread.Sleep ( 100 ) ;
}
System.Threading.Thread e = new System.Threading.Thread ( t.PrintClassWithLock ) ;
System.Threading.Thread f = new System.Threading.Thread ( t.PrintClassWithLock ) ;
System.Console.WriteLine() ;
System.Console.WriteLine() ;
e.Start ( " E({0:00})" ) ;
f.Start ( " F({0:00})" ) ;
while ( e.IsAlive && f.IsAlive )
{
System.Threading.Thread.Sleep ( 100 ) ;
}
}
catch ( System.Exception err )
{
System.Console.Write ( err.Message ) ;
}
return ( result ) ;
}
}
}
|
|
|
|
|
szolDat wrote: Please explain why the following situation isn't possible (I'm somewhat clear about the heap and the stack):
Thread1 enters the cycle, increments i by 1.
The control is passed to Thread2 and it enters the cycle too, incrementing i by 1. i now equals 2.
Thread1 gets control again and tries to access an array's element (or perform any action) using the cycle variable.
Every time that you enter a method, a stack frame is created for the local variables in the method. That stack frame is unique for that single method call, so that method call has it's own set of local varables that isn't shared by anything else anywhere.
Even if the method call's itself (recursion) so that the method call is in the same thread and has the same stack, another stack frame is created, and the local variables in that call are separate from the local variables in the previous call.
If the method calls occur in separate threads, each call will have it's own stack frame, but now they are even in separate stacks.
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
Well, I think I'll read this article http://tinyurl.com/mbmaz as soon as I'll have time, and if something isn't clear than I'll ask.
Thank you.
|
|
|
|
|
You should take that article with a grain of salt. Although it looks like a good overview, some things in it are simply not correct.
For example:
"Reference to a Type"
- There is no such thing. A reference always goes to an instance of a class, not to the class itself.
"As the method executes, we need some memory for the "result" variable and it is allocated on the stack."
- Not at all. A stack frame is allocated when the method starts, which is large enough to hold all local variables in the method. The size of the stack frame does never change while the method is executing.
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
Hi guys, I am standing on a point where visual studio is not functioning properly. I am trying to compile my project and i am getting the following error message "The directory name is invalid." I did google but problems couldn't be solved. All of my project that were used to compile not even compiling now.
Another thing is, when iam trying to create a brand new Csharp project i am getting this error
"The given path's format is not supported".
What should i do. Any suggestion shall be highly appreciated. Thanks
|
|
|
|
|
What's the directory name? What is the project path?
Perhaps there is a problem with them that sight of them will solve.
Also, I've run into problems running VS with projects on an external FAT32 drive. It only has support for 260 characters in a full file path so I found I couldn't create some directories because they were too far down the path.
|
|
|
|
|
Thanks for replying. I have fixed my problems so letting you know if you or anyone ever get a chance to face these kind of problems.
Although, my project name was ABC and it was locted inside a folder that reside on desktop. As i said before I could run couple of my proejcts in the past without any errors and could also create and delete files/project.
Problems Solutions:
I was working on my other programming projects so i set some Environemt paths varialbes. I deleted all those path that i set and now I can run Visual studio as I was able to run before successfully.
|
|
|
|