|
Hi,
This is more of a theory question i guess. I've recently been looking at some old code and trying to improve it. I've always been told you should dispose of objects once you've finished with them, but never have as i've only been working on small projects.
However, its came time for me to start improving on this. In one section of my code i create a List<string> and pass it to my sql statement builder. My understanding is that now i have 2 Lists in memory, one used in the sql statements method and one in the method i used to create it.
Would it be better to pass it to the sql statements method as a ref and then clear its list at the end of the method i created it in? or just pass it normally and clear it in both methods?
Regards,
Gareth.
|
|
|
|
|
gareth111 wrote: create a List and pass it to my sql statement builder
If you're really interested in building better code, it would be wiser to use Stored Procedures and avoid other messes also.
only two letters away from being an asset
|
|
|
|
|
Mark,
I have used stored procs in other projects. The reason im not using them is because i think its far easier to use a sql statement class that has 5 or so methods than to use stored procs that has 20 or so.
Regards,
Gareth
|
|
|
|
|
gareth111 wrote: The reason im not using them is because i think its far easier to use a sql statement class that has 5 or so methods than to use stored procs that has 20 or so.
What do the number of methods have to do with it?
|
|
|
|
|
Because its easier to maintain.
Example: My project can connect to 5 different databases, which have 5 tables, so i need to create 25 stored procs just to do some simple select * from. With the sql builder class, i have 1 method.
It might just be me, but that seems like in the long run that will save me time. Their might be advantages of splitting it up so the data layer is actually handled by the db server, i am not saying there isn't.
Regards,
Gareth.
|
|
|
|
|
gareth111 wrote: Example: My project can connect to 5 different databases, which have 5 tables, so i need to create 25 stored procs just to do some simple select * from. With the sql builder class, i have 1 method.
What!?! That doesn't add up at all. If all the databases are in SQL Server you can access 5 databases from within one stored proc. Simply multiplying the number of databases by the number of tables does not give you the number of stored procedures. I have no idea where you logic is going with that but it isn't in the right direction.
As for using the SQL Builder class...
|
|
|
|
|
Colin Angus Mackay wrote: That doesn't add up at all
Must have been a simple mistake, like clicking 1 rather than 5
only two letters away from being an asset
|
|
|
|
|
Colin Angus Mackay wrote: I have no idea where you logic is going with that but it isn't in the right direction.
Same here.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
|
|
|
|
|
What you are referring to is what I believe is called "Garbage Collection".
This used to be very important in C as resources could easily be taken up very quickly if one did not dispose objects once they had been used.
Although with C# you can still dispose object I understand this not to be necessary as the .NET framework takes care of this for you.
With regards to passing the list by reference - yes only 1 copy would persist.
If you were coding in C then this might be necessary, however with C# I think the saving would be minimal.
Also with regards to future development when someone else looks at your code they may be puzzled as to why the list is passed by reference.
I am still learning C#, so I imagine one of the more experienced coders may have more valuable information.
You always pass failure on the way to success.
|
|
|
|
|
Hi,
yes, you should call Close() or Dispose() on those objects that offer them, when
you no longer need the object.
no, passing an reference type (i.e.an instance of a class) to some method does not create
another object; to create an object you must either use the "new" keyword, or call a
creational method (one that has a "new" inside, as in a Factory pattern), which I hope
has "Create" in its name.
no, passing a reference type with the "ref" keyword does not change the object count;
the only thing it does is it allows the called method to not only modify the object,
but also the caller's variable that references that object.
|
|
|
|
|
Hi Luc,
Thank you for clarifying.
Regards,
Gareth.
|
|
|
|
|
By doing the following
BackgroundWorker syncWorker = new BackgroundWorker();<br />
syncWorker.DoWork += new DoWorkEventHandler(SynchronizeDir);<br />
syncWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Synchronized);<br />
syncWorker.RunWorkerAsync(new Object[] { fromDir, toDir, filter, createRemotePath });<br />
Synchronized(syncWorker, new RunWorkerCompletedEventArgs(null, err, true));
A backgroundworker is spawned from the GUI thread into a new thread.
Is there a way to catch any errors that don't get handled by the new thread, in the GUI thread?
Google tells me the RunWorkerCompletedEventhandler (Synchronized in this case) should normally be fired and its RunWorkerCompletedEventArgs should contain the exception in the .Error field.
Rethrowing the error in the spawned thread doesn't get the exception through to the GUI thread.
Even plain ignoring the exception gives me an .Error field with null as value.
What am I doing wrong?
|
|
|
|
|
Not sure what resources you have researched thus far, but this might help.
http://msdn2.microsoft.com/en-us/library/hybbz6ke.aspx[^]
You may already know this information, but reviewing MS's example may reveal what change you need to make.
Just because we can; does not mean we should.
|
|
|
|
|
Yes, that is in the category of examples I found all over the net.
So I figure I'm doing something very wrong...
edit: for some reason the code tags don't interpret the indentation on this post ...
private void SynchronizeDir(object sender, DoWorkEventArgs e)<br />
{<br />
string from = (string)((object[])e.Argument)[0];<br />
string to = (string)((object[])e.Argument)[1];<br />
string filter = (string)((object[])e.Argument)[2];<br />
bool createRemotePath = (bool)((object[])e.Argument)[3];<br />
try<br />
{<br />
SynchronizeDir(from, to, filter, createRemotePath);<br />
}<br />
catch (Exception err)<br />
{<br />
throw err;<br />
} <br />
}
To me it looks like I'm doing the same steps as in the example:
- Extract the argument(s)
- Start the time-consuming operation
- I don't handle the cancel option(yet)
In my code, after extracting the arguments, I call the regular method with those arguments and catch anny errors that normal method would throw. They get caught as they normally are.. But what should I do to make the Synchronized event trigger with the .Error field filled in.
Right now the program just stops on the error throwing...
|
|
|
|
|
From here I would work towards handling the error here in your catch statement. It appears that the catch statement here is running on the main thread.
Just because we can; does not mean we should.
|
|
|
|
|
This is the method that gets called by the bgndWorker.
Like this:
public void BeginSyncingDir(string fromDir, string toDir, string filter, bool createRemotePath)
{
BackgroundWorker syncWorker = new BackgroundWorker();
syncWorker.DoWork += new DoWorkEventHandler(SynchronizeDir);
syncWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Synchronized);
syncWorker.RunWorkerAsync(new Object[] { fromDir, toDir, filter, createRemotePath });
}
As for now I pass the error as a result.
Which is extremely ugly.
|
|
|
|
|
the CODE tag is fine for single-line stuff,
for multi-line code snippets, use the PRE tags, and:
- add a space to empty lines (otherwise they will vanish)
- avoid HTML-like stuff (such as a less-than comparison in an if or for); if necessary
insert a "& l t ;" (without the spaces) instead of the < sign.
|
|
|
|
|
Hmm, Ok.
I'll take it into account for my other posts.
Weird that the code tag is not for code though
Thx for the reply
|
|
|
|
|
Hi,
My combobox is bound to a dataset1.tables[0].defaultview. The values of my combobox to dataset2.tables[0].defaultview. During inserting i set the rowfilter of dataset2.tables[0].defaultview which works fine. The nbr of items in the combobox is limited to the records i filtered.
After choosing a selection and after edit, my combobox.selectedvalue = 147 (the unique key in the dataset.)
But when i set the rowfilter back to string.empty my selectedValue of my combobox becomes <null> but in the combobox there's still the correct last selected text.
Result is that i get an error if i want to edit the same record again, even when everything looks fine to the user.
So it seems that the binding is mixed up or so.
Anyone any idea what goes wrong ?
Thx
Kurt
|
|
|
|
|
combobox.Text = ""; should fix you up.
Just because we can; does not mean we should.
|
|
|
|
|
thx mate and a happy new year.
Kurt
|
|
|
|
|
hi,
how can i remove the this error "Unsafe code may only appear if compiling with /unsafe" ?
I AM WORKING ON "PLOTTER ROBOT"(FYP).
|
|
|
|
|
Does it point you to any code?
Are you using pointers anywhere?
|
|
|
|
|
hi,
by adding /unsafe to the command line when invokind csc.exe,
or by checking the "unsafe" checkbox in Visual's project properties page.
|
|
|
|
|
Hi,
I have some problem using vector in C#.
for example I have vector of double, let say double[] vec1 = new double[] {1, 3, 5, 6, 7, 8, 9 };
And I want to get a view on a part of this vector without copy.
Let say I want to get the vector: double [] vec2=new double[]{5, 6, 7};( which is a part of vec1)
without creating a new vector or without copy the initial vector.
In C++ I can use iterator: data_it_begin = vec1.begin() + 2;data_it_end = vec1.begin() + 5.
I don't how to this in C#.
Does someone can help me.
Thanks!!!
|
|
|
|