|
Eddy Vluggen wrote: Singletons are not inherently evil.
Unless there are too many of them.
|
|
|
|
|
Luc Pattyn wrote: Unless there are too many of them
That would be confusing. It would become evil if you'd try to justify it by calling it obfuscation
Quoad vos spirum, vos sperum?
|
|
|
|
|
I was thinking that would be the one case (obfuscation) of good justification for too many globals.
In Christ,
Aaron Laws
http://ProCure.com
|
|
|
|
|
One thing to note about this example is that it's not a singleton. It's a global property called Instance that is accessed via the EnterMineAndDigForGold class.
The key point about a singleton is that there is only one instance at any point in time and there can never be more than one. There is nothing stopping me from ignoring the Instance property on EnterMineAndDigForGold and creating as many instances of EnterMineAndDigForGold as I want.
In order to make this a singleton you would need to make sure that no-one can create any instances of EnterMineAndDigForGold other than the one created and held as the static variable _instance.
The easiest way to do this is to make the constructor for EnterMineAndDigForGold private. That will stop any other class from creating one of these.
To be honest, looking at your class I don't think it really matters because your EnterMineAndDigForGold class doesn't have any instance state so it doesn't matter whether you create a new one every time, or share one around, or make all the methods static so that you never need an instance at all.
|
|
|
|
|
Singletons are unnecessary (though not necessarily evil) in C#, especially since the advent of static classes.
Only use singletons in lesser languages; like C++.
|
|
|
|
|
What is the best way to determine an Object Variable is null or Empty ?
(or is not Null and Empty)?
|
|
|
|
|
I know the best method for checking null, and that is very simple.
Just compare your object with null keyword.
if(obj==null)
{
}
But for checking emptiness of your object you need to prepare custom method
of your object and check emptiness of all member.
Class Employee
{
String FirstName;
String LastName;
public bool IsEmpty()
{
if(!String.IsNullOrEmpty(FirstName) || !String.IsNullOrEmpty(LasttName))
return False;
else
return True;
}
}
HTH
Jinal Desai - LIVE
Experience is mother of sage....
|
|
|
|
|
If it's a string then use string.IsNullOrEmpty .
If it's for another class then it depends on how you define empty. If this is for your own classes you could define an IEmpty interface and implement that.
public interface IEmpty
{
bool IsEmpty { get; }
}
then you can use a generic method such as
public static bool IsNullOrEmpty<T>(T obj) where T : IEmpty
{
if (obj == null)
return true;
return obj.IsEmpty;
}
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Thanks
Will You Please Give me an example on how to use this codes.
|
|
|
|
|
OK, here's an example class. The implementation of the interface is the method at the end.
public class IntString : IEmpty
{
private string text;
private int value;
public IntString()
: this(0, string.Empty)
{ }
public IntString(int value)
: this(value, string.Empty)
{ }
public IntString(string text)
: this(0, text)
{ }
public IntString(int value, string text)
{
this.text = text;
this.value = value;
}
public string Text
{
get { return text; }
set { text = value; }
}
public int Value
{
get { return value; }
set { this.value = value; }
}
public bool IsEmpty
{
get { return value == 0 && string.IsNullOrEmpty(text); }
}
}
To use it, combined with the method and interface declaration i posted earlier you can do something like this...
IntString intString = new IntString();
Console.WriteLine(IsNullOrEmpty<IntString>(intString));
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Thanks for reply,
I think that u didnt get my question:
I wanna something like :
Object objVariable;
if(!String.IsNullOrEmpty(objVariable))
Instead of doing this :
Object objVariable;
if(objVariable !=null && objVariable.ToString() !="")
|
|
|
|
|
As I posted above[^] then
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Thanks
Will You Please Give me an example on how to use this code.
|
|
|
|
|
you could say that again.
|
|
|
|
|
We could be heading for infinite recursion here
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Hi,
As an alternative, you could create an extension method to perform the check;
internal static class UtilityMethods
{
internal static bool IsEmpty(this object o)
{
return o == null;
}
} This would result in code that looks like this;
Object objVariable;
if(!objVariable.IsEmpty())
{
}
I are Troll
|
|
|
|
|
How to populate row number for datagridview in row header column with out any looping statements?
it will be very usful for me if you help.
Elango.N
modified on Tuesday, June 15, 2010 5:51 AM
|
|
|
|
|
You can implement your custom datagridview by overriding it and then use it's OnRowPostPaint method
to draw row number.
That's it. No whenever you use this grid it will automatically add row number to the datagridview.
This is the simplest solution I have to apply row number in datagridview without any kind of looping.
class MyDGV : DataGridView
{
public MyDGV()
{
}
protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
{
string strRowNumber = (e.RowIndex + 1).ToString();
while (strRowNumber.Length < this.RowCount.ToString().Length) strRowNumber = "0" + strRowNumber;
SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);
if (this.RowHeadersWidth < (int)(size.Width + 20)) this.RowHeadersWidth = (int)(size.Width + 20);
Brush b = SystemBrushes.ControlText;
e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
base.OnRowPostPaint(e);
}
}
HTH
Jinal Desai - LIVE
Experience is mother of sage....
|
|
|
|
|
I have a project for antiquity which need saving pictures and maps for specific impression.I search alot but articles often speak about saving path of picture in sgl.can anybody help me?
|
|
|
|
|
See here - may this will help you get started.
My signature "sucks" today
|
|
|
|
|
As everyone here is tired of hearing, I'm working on a simple User Control that's misbehaving. If you help me solve this, I promise to contribute an article about it.
The control displays various user-selectable shapes for water channels, then asks for the dimensions of the conduit. I have an event handler working nicely to notify the hosting Form when a value changes, but it's too responsive; too many events fire it. Specifically, when a user enters values into the textboxes for the dimensions of the channel, if for some ungodly reason the user decides to select another shape, the event fires for a change of value in the previous (unchanged) textbox. I added some code tonight to check to see that the value actually changed before the focus moves to another control, but it doesn't seem to be working, and I don't see why. The relevant code is as follows:
private void rbTrap_CheckedChanged(object sender, EventArgs e)
{
if (rbTrap.Checked == true)
{
Shape = myShape.Trap;
MyArgs.MyControl = "rbTrap";
RaiseEvent(rbTrap, MyArgs);
lblDim1.Text = "Depth, d";
lblDim2.Text = "Bottom Width, W1";
lblDim3.Text = "Top Width, W2";
lblDim3.Visible = true;
txtDim3.Visible = true;
Invalidate();
}
}
private double ValidateEntry(TextBox MyTextBox)
{
try
{
return Double.Parse(MyTextBox.Text);
}
catch (FormatException ex)
{
MessageBox.Show("Enter a valid numeric value" + ex.Message);
MyTextBox.Focus();
return 0.0;
}
}
private void txtDim1_Enter(object sender, EventArgs e)
{
txtDim1.SelectAll();
}
private void txtDim1_Leave(object sender, EventArgs e)
{
prev = dim1;
dim1 = ValidateEntry(txtDim1);
if (dim1 != 0.0 & prev != dim1)
{
MyArgs.MyControl = "txtDim1";
RaiseEvent(txtDim1, MyArgs);
}
}
private void txtDim1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
txtDim2.Focus();
}
}
In my understanding, if the value of dim1 is unchanged (dim1 = prev), the if test should fail, and RaiseEvent(...) should not execute. Maybe I've been staring at this too many hours, but I can't see where the error lies. For what it's worth, the event handling code is:
public event EventHandler ValueChanged;
public class MyEventArgs : EventArgs
{
private string myControl;
public string MyControl
{
get { return myControl; }
set { myControl = value; }
}
}
protected virtual void OnValueChanged(object sender, MyEventArgs e)
{
EventHandler eh = ValueChanged;
if (eh != null)
eh(this, e);
}
public void RaiseEvent(object sender, MyEventArgs e)
{
OnValueChanged(sender, e);
}
I adapted this from an article posted by DaveyM69 - an excellent article, by the way - and I admit that my understanding of event handling is shaky at best. But it does work nicely, albeit in weird ways.
Any assistance would be welcome...
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
Hi Roger.
I'm having trouble recreating your problem. I shoved two text boxes on a form and used the following code:-
double prev;
double dim1;
private void txtDim1_Leave(object sender, EventArgs e)
{
prev = dim1;
dim1 = ValidateEntry(txtDim1);
if (dim1 != 0.0 & prev != dim1)
{
MessageBox.Show("Event Fired");
}
}
private double ValidateEntry(TextBox MyTextBox)
{
try
{
return Double.Parse(MyTextBox.Text);
}
catch (FormatException ex)
{
MessageBox.Show("Enter a valid numeric value" + ex.Message);
MyTextBox.Focus(); return 0.0;
}
}
private void txtDim1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13) { txtDim2.Focus(); }
}
and it worked as expected. Is there anywhere else in your code that you could be changing the value of txtDim1.
The FoZ
|
|
|
|
|
I think Dave may be on to something; it has to do with the radio button somehow. The extra event does not occur if I use the mouse to move to another text box, only when I move to a radio button.
The messages displayed also show that the text did not change, but it does happen that the text changes from Selected to unselected when the focus is moved to the radio button. I wonder if that's being picked up as a text change and firing the event?
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
Hi Roger,
Thanks for the kind words
My only initial thought is to set a breakpoint inside if (rbTrap.Checked == true) . I have had wierd behaviour before with CheckBox es/RadioButton s where CheckedChanged has raised more often than expected.
If that doesn't help isolate the cause then feel free to drop me an email through the link at the bottom of this post and I'll happily take a look at the project for you and root the b**ger out!
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Hello, I am working on my final project at the university. I need a .net TrackBar or a similar control to set an upper-limit, a lower-limit and a setting value.
Could somebody help me?
Thank you so much.
Nicolás.
|
|
|
|