|
miss YY wrote: string str="select * from Furn_Info where Time between ‘"+from+"’
and ‘"+to+"’";
Rather parameterize your queries like so:
cmd.CommandText = "select * from Furn_Info where Time between @from and @to";
cmd.Parameters.AddWithValue("@from", from.ToString("yyyyMMdd"));
cmd.Parameters.AddWithValue("@to", to.ToString("yyyyMMdd"));
|
|
|
|
|
Yes parameterize, but don't make the dates into strings.
|
|
|
|
|
Why not?
How would you do it, just put the DateTime object as the parameter value?
|
|
|
|
|
ScottM1 wrote: put the DateTime object as the parameter value?
Of course.
|
|
|
|
|
I am trying to understand delegates and events in C Sharp. I am finding the subject confusing. Please consider the following program:
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Text;<br />
<br />
public class StockArgs : EventArgs {<br />
public StockArgs( String symbol, string price, int volume )<br />
{<br />
this.symbol = symbol;<br />
this.price = Decimal.Parse(price);<br />
this.volume = volume;<br />
}<br />
public string Symbol {<br />
get<br />
{ return symbol; }<br />
}<br />
public string Price {<br />
get<br />
{ return Price; }<br />
}<br />
<br />
private string symbol;<br />
private decimal price;<br />
private int volume;<br />
}<br />
<br />
public class Stock {<br />
public delegate void StockPiraceAlertHandler( object source, StockArgs a);<br />
public event StockPiraceAlertHandler OnAlert;<br />
<br />
public delegate void VolumeAlertHandler(object source, StockArgs a);<br />
public event VolumeAlertHandler OnVolumeAlert;<br />
<br />
public void Alert()<br />
{<br />
if ( OnAlert != null )<br />
OnAlert( this, new StockArgs("ORCL", "14.50", 100000) );<br />
}<br />
<br />
}<br />
<br />
public class Main {<br />
public static void Main()<br />
{<br />
Stock stock1 = new Stock();<br />
stock1.OnAlert(null, null);<br />
}<br />
}
In the main routine (called Main) I want to trigger the alert called OnAlert. However, the code does not compile. Furthermore, the compiler error message implies that alerts can be only generated by the class that defines the alert. If that is true, then it seems like a major deficiency with alerts in C sharp. However, I suspect I am missing one or more key points. Please enlighten me.
I thank the group in advance for their responses.
Bob
|
|
|
|
|
Hi Bob,
When your Main (or any other code) wants to do something to stock1, it should set one of its properties, or call one of its methods.
events are meant to be used the other way around; they exist for the external world (Main) to be signaled by something that happens inside your class (Stock), as in:
class MyForm : Form {
public MyForm() {
Button btn=new Button();
this.Controls.Add(btn);
btn.Click+=new EventHandler(myClickHandler);
...
public void myClickHandler(object sender, EventArgs e) {
}
}
and the Button class would contain something like:
if (a click got detected) {
if (this.Click!=null) this.Click(this, new EventArgs());
}
so it is Button who executes its own event, and MyForm who subscribed to it by adding its myClickHandler to the public event.
And all this holds true for .NET, not just for C#.
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
For triging event in C# you have to do more like :
<code>
public static void Main()
{
Stock stock1 = new Stock();
stock1.OnAlert += new Stock.StockPiraceAlertHandler(stock1_OnAlert);
stock1.Alert();
}
static void stock1_OnAlert(object source, StockArgs a)
{
}
</code>
|
|
|
|
|
The Main class should not tell the Stock class to raise its event. It can be done, but it's not a good design. Usually we see this error when a derived class tries to raise the event of a base class; I've never seen anyone try to do what you are trying to do.
|
|
|
|
|
Stock class owns those events. Main is just a subscriber who will get notification when the event occurs. To get event notification, you need to subscribe to it first.
public static void Main()
{
Stock stock1 = new Stock();
stock1.OnAlert += AlertOccured;
stock1.Alert();
}
private void AlertOccured(object source, StockArgs a)
{
} Delegates are the backbone of events. The main difference is events can be raised only by the class which declares them where a delegate instance can be invoked by anyone who have access to it. Following example shows what I meant.
public delegate void ADelegate();
class Foo
{
public ADelegate ADelegateInstance;
public void FireADelegate()
{
if (ADelegateInstance != null)
ADelegateInstance();
}
}
class Program
{
static void AMethod()
{
Console.WriteLine("AMethod()");
}
static void Main(string[] args)
{
Foo f = new Foo();
f.ADelegateInstance += AMethod;
f.ADelegateInstance();
}
} Now if you change public ADelegate ADelegateInstance to public event ADelegate ADelegateInstance the code won't compile. f.ADelegateInstance() is invalid here.
If you are following the standard void method(object, EventArgs) pattern for events, you don't have to create your own delegates. Use the EventHandler<T> provided with framework. Your code can be written like,
public class Stock {
public event EventHandler<StockArgs> OnAlert;
public event EventHandler<StockArgs> OnVolumeAlert;
public void Alert()
{
if ( OnAlert != null )
OnAlert( this, new StockArgs("ORCL", "14.50", 100000) );
}
}
Best wishes,
Navaneeth
|
|
|
|
|
You have explained such better than me !
|
|
|
|
|
Thanks
Best wishes,
Navaneeth
|
|
|
|
|
anyone knowsw about any other web browser control other than Microsoft?!! I tried searching google but could not found any...
ifyou came across another web browser control,, please let me know...
|
|
|
|
|
|
my application is in C#
|
|
|
|
|
What do you need to do that the .Net browser control can't do?
|
|
|
|
|
Hi All,
How do you add a line type spacer to a Windows Forms context menu? I can only seems to add MenuItems to the menu.
Thanks,
|
|
|
|
|
In the designer, select the context menu strip and right click in the area for a new item (where it says Type Here) and choose Insert|Separator.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Hi Dave,
I'm actually creating the context menu all in code without the designer. How would I go about doing this using just code?
Thanks for the reply.
James.
|
|
|
|
|
This is a quick example - just creates one item in a context menu attached to the form
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
public class FormMain : Form
{
private IContainer components = null;
private ContextMenuStrip contextMenuStrip;
private ToolStripSeparator toolStripSeparator;
public FormMain()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
components = new Container();
contextMenuStrip = new ContextMenuStrip(components);
toolStripSeparator = new ToolStripSeparator();
contextMenuStrip.SuspendLayout();
SuspendLayout();
contextMenuStrip.Items.AddRange(new ToolStripItem[] {
toolStripSeparator});
AutoScaleDimensions = new SizeF(6F, 13F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(284, 262);
ContextMenuStrip = contextMenuStrip;
Text = "Form Main";
contextMenuStrip.ResumeLayout(false);
ResumeLayout(false);
}
}
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Brilliant! Thanks very much for this Dave, very helpful.
Regards,
|
|
|
|
|
Please, can you guide me how I can make a thread over a block of a code.
Thank you.
|
|
|
|
|
See this[^] set of articles.
/ravi
|
|
|
|
|
One good link for C# Threading.
Threading in C#[^]
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.
|
|
|
|
|
hi. thanks for your message. i can't improve itthats my problem
|
|
|
|
|
Please reply on proper thread.
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.
|
|
|
|