|
How to correct the date writen in textBox.
Example: If in the textBox i write 0 or number >31 it will falls.
What i have to do, but in textBox no in other like comboBox.
Help.
Vasildb
|
|
|
|
|
Hi,
you should create a function that checks wether a string is a valid date (or whatever).
Main point then is to call that function at the right time.
On every text change (as with TextChanged event, or KeyDown event) is often
not adequate, since partial dates (i.e. while entering characters in order to
construct a valid date) would be refused.
Better is to use an event that occurs when the dialog closes, but best of all
seems to use TextBox.LostFocus event, which occurs whenever something else gets
focus (e.g. a button, another textbox,...).
Luc Pattyn
|
|
|
|
|
or you can use Compare Validator to validate that the input is a valid date:
<asp:CompareValidator id="<SomeID>" runat="Server" Operator="DataTypeCheck"<br />
Type="Date" ErrorMessage="Please enter a valid date...."<br />
ControlToValidate="<Your TextBoxID>" />
|
|
|
|
|
Hi,
Assuming you are using an n-tier architecture for a windows application.
Let say you populate a dataGridView (in winform .net 2.0 control) from a dataSet. Then you make several changes (i.e. delete, update, insert, ...) to this dataSet in the control.
How can you then update the database with these changes. Should you use dataAdapter.Update(dataSet) in the datalayer or should you pass the dataset changes to the dataLayer and loop through each record in the dataset in a transaction and commit according to the changes i.e. .modified, added, deleted...
Thanks
|
|
|
|
|
Hi All,
I'm having an issue with constructors. I want to prevent the creation internally in the following example:
using System;<br />
<br />
public class ConditionalObject<br />
{<br />
private string _MajorName;<br />
private string _MinorName;<br />
public string MajorName { get { return _MajorName; } }<br />
public string MinorName { get { return _MinorName; } }<br />
public ConditionalObject()<br />
{<br />
if ((_MajorName.Length == 0) || (_MinorName.Length == 0))<br />
{<br />
throw new Exception("Unable to initialise new object without identifier.");<br />
<br />
}<br />
}<br />
public ConditionalObject(string AMajorName, string AMinorName): base()<br />
{<br />
_MajorName = AMajorName;<br />
_MinorName = AMinorName;<br />
}<br />
}
Neither Dispose(); nor return null; nor this = null; can be applied in constructor. Could anybody help me guys?
Thanks for the answers.
|
|
|
|
|
You can't prevent the object from being created, that is already done. When you throw an exception the object's reference won't be returned by the constructor, and the object will be left unreferenced in memory and eventually garbage collected.
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
Thanks Guffa. Unfortunatelly this is the main problem: it returns the (not null) object reference whatever I've thrown exception or not.
It is a quite big problem because I couldn't even check the previously unidentified object in the next line externally.
Is there any other way to solve this?
|
|
|
|
|
Tony Lambert wrote: Unfortunatelly this is the main problem: it returns the (not null) object reference whatever I've thrown exception or not.
So you just catch the exception and ignore it, or what?
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
I do nothing with it. (should I? ) I just throw it, it raises the exception message and the constructor returns with the object reference in the caller method instead of null.
Actually, this is the missing part of the puzzle: how to destroy or give null operand to the object in its constructor or mark it as destroyable or return with null?
|
|
|
|
|
Tony Lambert wrote: I just throw it, it raises the exception message and the constructor returns with the object reference in the caller method instead of null.
No, it doesn't. If you throw an exception in the constructor, the created object is never used. You don't get a reference to the object, as the reference is not assigned to any variable.
Tony Lambert wrote: Actually, this is the missing part of the puzzle: how to destroy or give null operand to the object in its constructor or mark it as destroyable or return with null?
You can't do that, and you don't need to, as the reference to the object will never be used. Just make sure that the object is left in a state so that it can be garbage collected.
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
I fail to see the use for a constructor that is not sufficient for instanciating your object. Why have the constructor there if it will make an object you deem invalid? Remove it and only accept calls to constructors that initialize the fields you need set.
-Larantz-
|
|
|
|
|
"Remove it and only accept calls to constructors that initialize the fields you need set."
Well it is okay in end user application, but it's an abstract class in library. Since it will be called by end-developers, the API must be consistent and any internal operation must to be transparent to make the library clear, and understandable.
|
|
|
|
|
Quote:
<code>
public class ConditionalObject
{
public ConditionalObject(string name)
{
try
{
if (name.Equals(""))
{
throw new ArgumentOutOfRangeException(this.ToString() +
": Unable to create new object without identifier.");
}
}
catch (ArgumentOutOfRangeException AException)
{
Console.WriteLine(AException.Message);
}
}
}
</code>
Your next example was according to what I meant. No empty constructors
-Larantz-
|
|
|
|
|
|
Thanks again. Here is a simple code:
using System;<br />
<br />
public class ConditionalObject<br />
{<br />
public ConditionalObject(string name)<br />
{<br />
try<br />
{<br />
if (name.Equals(""))<br />
{<br />
throw new ArgumentOutOfRangeException(this.ToString() +<br />
": Unable to create new object without identifier.");<br />
}<br />
}<br />
catch (ArgumentOutOfRangeException AException)<br />
{<br />
Console.WriteLine(AException.Message);<br />
}<br />
}<br />
}<br />
<br />
public class CallerClass<br />
{<br />
public static void Main()<br />
{<br />
ConditionalObject obj = new ConditionalObject("");<br />
Console.WriteLine(obj == null ? "null" : "not null");<br />
}<br />
}
When I change this:
ConditionalObject obj = new ConditionalObject("");
to this:
ConditionalObject obj = new ConditionalObject("anything");
the last line gives the same value (not null). What did I wrong?
|
|
|
|
|
Because you catch the exception inside the constructor, the constructor never exits with an exception.
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
It is the same without catch (first example). When I throw an exception it only gets registered in an exception buffer till something catches. It does nothing with the constructor itself. That is the reason why it comes back with not null value all the time.
When I don't catch it, the application gets killed at the exit point of constuctor. That's another problem: I need that null value.
Anyway, somebody has given to me another solution with indirect static method initialisation in the next thread. I think I'll do it that way.
Thanks to All again.
|
|
|
|
|
Tony Lambert wrote: It is the same without catch (first example). When I throw an exception it only gets registered in an exception buffer till something catches. It does nothing with the constructor itself. That is the reason why it comes back with not null value all the time.
An exception buffer? Why do you think that?
When you throw an exception in the constructor it immediately exits. The exception isn't stored in any buffer anywhere, it's returned from the constructor.
Tony Lambert wrote: When I don't catch it, the application gets killed at the exit point of constuctor.
No, the application doesn't get killed. The constructor exits with an exception, and the exception travels through each level in the call stack until it's caught somewhere. As you don't catch it in your program, the program exits with the exception and it's caught by the system.
Tony Lambert wrote: That's another problem: I need that null value.
A constructor never returns a null value, it always returns the address of the object. If you wan't to abort a constructor, an exception is the only way to do that, and you have to catch the exception in the code that calls the constructor for it to be useful.
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
"An exception buffer? Why do you think that?"
call stack
Thanks Guffa. I think you're right, it comes back with object reference.
BTW, I've solved this issue across a static init method, where I do the try/cacth as well before the instance gets created. The constructor is hidden now.
|
|
|
|
|
Hi,
within your class you could provide a public static method that first checks whatever
needs checking, then creates and returns a new instance of your class.
Something like:
public static ConditionalObject Create(int parm) {
if (parm is OK) {
return new ConditionalObject(parm);
} else {
return null;
return aDefaultConditionalObject;
throw new ApplicationException(...);
}
}
The calling code must deal with null or exception of course.
Luc Pattyn
|
|
|
|
|
Thanks Luc. I wait for a while than I'll try to solve it this way.
|
|
|
|
|
Why don't you use the factory pattern
|
|
|
|
|
Thanks ejuanpp. Not a bad idea, but I'll be in the same problem: end-developers need to handle it externally or do some kind of validation afterwards and the API will not be consistent.
I'd like to avoid to leave the default object creation way or do anything "non-standard" to make easier other developers life.
|
|
|
|
|
I don't know what ur actual intention is,
If your intention is to prevent object creation using default constructor aking ur Default Constructor private will solve ur issue.
|
|
|
|
|
hi
i want to start network Programming with VCc#2005 (from begin to advance)
i want to create Client/Server Application that detect Clients on the Network and Send/Recive Data across the Network.
Can Somebody Help me ??
Note : I am beginner in Network Programming and i want to use Standard FrameWork Classes and don't want to use User Classes.
Thanks.
|
|
|
|