|
Until you've fully explored enough of the wrong ways, you'll never truly appreciate the right way.
|
|
|
|
|
You missed the word "grasshopper" from the end of that sentence.
|
|
|
|
|
I'm only just starting to learn LINQ: I have pretty reasonable SQL abilities already.
The main reason I am learning it is that I feel that the stronger typing and compile time syntax checking available in LINQ to SQL has got to produce better, cleaner, more reliable solutions than hand crafted SQL.
Once I've got a good feeling for it, I'll make the decision to either go with it or back - but until I have that level of confidence in my abilities with it, I can't make that call. If I don't learn LINQ, I made that call without sufficient information.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
|
|
|
|
|
OriginalGriff wrote: If I don't learn LINQ, I made that call without sufficient information.
Hear hear!
I finally dabbled in a little bit of Linq (to SQL?) a few weeks ago. It gave me no benefit and I don't see how it can deliver any. Especially considering I use several different database systems, not just Sql Server.
OriginalGriff wrote: LINQ to SQL has got to produce better, cleaner, more reliable solutions than hand crafted SQL.
Not if you've been hand crafting clean reliable SQL for twenty years.
If Linq produces better SQL than you do, then go with it.
|
|
|
|
|
PIEBALDconsult wrote: Not if you've been hand crafting clean reliable SQL for twenty years.
Not quite what I meant: I was thinking more that finger trouble with LINQ is more likely to cause a compilation error, where in SQL it is run time and so harder to test and detect.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
|
|
|
|
|
Oh, sure, but you can develop and test your more-complex SQL statements with another tool and then copy-and-paste them into your application.
An additional benefit of that is that it drives parameter use rather than concatenation.
|
|
|
|
|
I would assume that Linq generates parametrized queries anyway: I always use them with SQL.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
|
|
|
|
|
OriginalGriff wrote: I always use them with SQL
Yes, but others don't, because concatenation is easier unless your framework (e.g. Linq) makes parameters easier.
|
|
|
|
|
PIEBALDconsult wrote: Yes, but others don't, because concatenation is easier they are lazy idiots who like leaving their DB wide open to an SQL injection attack
FTFY!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
|
|
|
|
|
|
Hello - need some help please....
I am using Visual Studio 2010, and have created a "simple" test program in C#
Basically, there is a main form that is called upon startup from the Program.cs - this is called frmParentForm.
I have a second form called frmChildForm
I only want one instance of frmChildForm, hence the Singleton usage. In This example, what i am aiming for is to type something into frmParentForm and press the button. Whatever is in the parent's text box will be appended to whatever is in the child form already.
(by the way, I use separate tabs to hold each form's coding - both have the same namespace. This keeps the coding tidy).
This is the code of frmParentForm:-
using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
<br />
namespace PassingDataExample<br />
{<br />
public partial class frmParentForm : Form<br />
{<br />
public frmParentForm()<br />
{<br />
InitializeComponent();<br />
}<br />
<br />
public void btnSendToNextForm_Click(object sender, EventArgs e)<br />
{<br />
frmChildForm.frm.Enabled = true;<br />
frmChildForm.frm.Visible = true;<br />
frmChildForm.frm.Show();<br />
frmChildForm.frm.Focus();<br />
frmChildForm.frm.textBox1.Text = "xyz";<br />
}<br />
}<br />
}
This is the code of frmChildForm:-
using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
<br />
namespace PassingDataExample<br />
{<br />
public partial class frmChildForm : Form<br />
{<br />
<br />
public static frmChildForm frm;<br />
<br />
public static frmChildForm GetInstance()<br />
{<br />
if (frm == null)<br />
{<br />
frm = new frmChildForm();<br />
}<br />
return frm;<br />
}<br />
<br />
private frmChildForm()<br />
{<br />
InitializeComponent();<br />
} <br />
private void btnClose_Click(object sender, EventArgs e)<br />
{<br />
this.Close();<br />
}<br />
<br />
}<br />
}
When I run this code, frmParentForm is displayed ok, but when I press the btnSendToNextForm_Click, I get the error "Object reference not set to an instance of an object" on the first access line "frmChildForm.frm.Enabled = true;" - Why? I thought a Singleton Form was meant to be usable program-wide.
By the way, the list of frmChildForm code in the parent form may or may not be correct - it's just where I have been trying different things. The essence is that the first instance gives the error.
Anyone have any ideas please?
|
|
|
|
|
Something is wrong. Your child form gets created inside GetInstance() yet you never call that method, instead you refer directly to the frm member.
This is what you could do:
- make frm private;
- add a public Frm property, basically doing what GetInstance() did (note the case difference);
- get rid of GetInstance() ;
- whenever the parent form needs the child form, use frmChildForm.Frm
The better alternative would be to:
- make frm private;
- add a child member to your parent form;
- set it to the result of GetInstance() before using it, then use it instead of frmChildForm.frm
Same result, fewer public members, hence more reliable code.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Thanks Luc
Tried answer 1:-
In child form, as
private static frmChildForm frm;
removed the GetInstance block
added
public string Frm<br />
{<br />
set { frm.textBox1.Text = value; }<br />
}
Then in parent form, removed all the existing "frmChildForm.frm" lines and added instead
frmChildForm.Frm = "xyz";
Now the Intellisense immediately gives me the same Object Reference error on this new parent line.
I will have a look at your second suggestion.
|
|
|
|
|
my first approach said:
add a public Frm property, basically doing what GetInstance() did
which means it (creates if necessary) and returns a form; the Frm you concocted is putting a string, not getting a form.
That is quite different from what I meant.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
is doesn't work because frm is not initialised. You could call GetInstance() to make it work. Either...
frmChildForm.GetInstance().Enabled = true;
frmChildForm.GetInstance().Visible = true;
or...
frmChildForm form = frmChildForm.GetInstance();
form.Enabled = true;
form.Visible = true;
...Actually where is frmChildForm initialised?
Illogical thoughts make me ill
|
|
|
|
|
Thanks Musefan - your second suggestion did it for me.
|
|
|
|
|
As an addition to the first alternative given by Luc's reply, add a static constructor to initialize the private static frm field, so you will be able to get rid of the if (frm == null) statement every time the Frm property is called.
|
|
|
|
|
Ok, thanks for the replies - very helpful all.
Just to wrap this up, here is the working example of taking text from one form and appending it onto a second form.
frmParentForm has a textBox, plus two buttons now - one to send the text and one to hide/show the second form.
frmChildForm is the second form that has to have text appended. It has a textBox plus one button to close this form - if you close it, a new instance will be created next time you send to it.
frmParentForm code:-
using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
<br />
namespace PassingDataExample<br />
{<br />
public partial class frmParentForm : Form<br />
{<br />
public frmParentForm()<br />
{<br />
InitializeComponent();<br />
}<br />
<br />
public void btnSendToNextForm_Click(object sender, EventArgs e)<br />
{<br />
frmChildForm form = frmChildForm.GetInstance();<br />
form.Enabled = true;<br />
form.Visible = false;<br />
form.Hide();<br />
form.textBox1.AppendText(textBox1.Text + "\r\n");<br />
}<br />
<br />
private void btnShowChild_Click(object sender, EventArgs e)<br />
{<br />
frmChildForm form = frmChildForm.GetInstance();<br />
if (form.Visible == false)<br />
{<br />
form.Visible = true;<br />
form.Show();<br />
}<br />
else<br />
{<br />
form.Visible = false;<br />
form.Hide();<br />
}<br />
}<br />
}<br />
}
frmChildForm code:-
using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
<br />
namespace PassingDataExample<br />
{<br />
public partial class frmChildForm : Form<br />
{<br />
<br />
private static frmChildForm frm;<br />
<br />
public static frmChildForm GetInstance()<br />
{<br />
if (frm == null || frm.IsDisposed)<br />
{<br />
frm = new frmChildForm();<br />
}<br />
return frm;<br />
}<br />
<br />
private frmChildForm()<br />
{<br />
InitializeComponent();<br />
}<br />
<br />
private void btnClose_Click(object sender, EventArgs e)<br />
{<br />
this.Close();<br />
}<br />
}<br />
}
Hope this helps someone else - I do like practical examples.
modified on Tuesday, February 15, 2011 6:47 PM
|
|
|
|
|
Hi,
Assuming I have four items in my collection, i.e.
list.add("Method");
list.add("Method");
list.add("Method2");
list.add("Method");
I would like the list now to have method and Method2 and the rest should be removed.
How is this done please?
Thanks
|
|
|
|
|
Try this code :
public static void removeDuplicate(ArrayList arlList)
{
HashSet h = new HashSet(arlList);
arlList.clear();
arlList.addAll(h);
}
Hope this helps.
All the best.
|
|
|
|
|
I really would like to have list colection instead of hashtable because my data is inside a list collection at present.
Thanks
|
|
|
|
|
Where are you getting HashSet from? as System.Collections.Generic.HashSet does not compute
Illogical thoughts make me ill
|
|
|
|
|
|
I tested in .Net 4 originally. But I have just set target to 3.5 and it still complains...
1. HashSet requires a type argument
2. (when 1 is addressed) best overload method has some invalid arguments (i.e. cannot pass ArrayList to constructor
Illogical thoughts make me ill
|
|
|
|
|
That was left as an exercise.
|
|
|
|