|
Yes, of course. The input is still valid if you add more characters to the beginning of the string. If you have more than nine digits, and no hyphens, the pattern will match the last nine digits.
If you also want to match the beginning of the text, use ^.
---
b { font-weight: normal; }
|
|
|
|
|
That was it, thanks!!
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
This is a quick question. Is it possible to do ADO.NET access from a static class? I'm guessing that it's not, because if I were to create a static class (say DBController) and then attempt to use it to access my database (eg with the method DBController.getNewClients) then the method (getNewClients) would have to create non-static instances of objects (ie the connection object).
From Java I know that if you try to reference non-static methods from static methods you get an error. Does this hold true for C# as well? Or would I be able to create a static DBController class that I could then use on any of my forms to access my database?
If that's not possible, then is it possible to pass a reference to my DBController to every form that's going to need access to it? How would I do that?
These may be dumb questions, but I'm the only tech guy in my company and my degree is in mathematics. Any help would be EXTREMELY appreciated.
It's lonely, oh so lonely, being the only tech guy in my company.
|
|
|
|
|
Yes, it's possible. As the connection has to be local to every thread that uses it, you would have to declare the reference to it as ThreadStatic.
---
b { font-weight: normal; }
|
|
|
|
|
Thanks for the response. I don't know exatly what a thread is. Could you give me a code snippet (just by example) so I can see what you're talking about? Or just a more specific explanation?
One is the loneliest number...
|
|
|
|
|
A thread is like a line of execution of code. You can have several threads running simultaneously in your process, for example one to manage the UI and respond to events, while other is processing data.
Multithreading is not that easy, since you can have problems when two threads try to access the same object at the same time. Then you have thread synchronization, so that while one thread is accessing certain object, other threads wait on it. But then if you have thread A holding object 1 and waiting for object 2, and thread B is holding object 2 and waiting for object 1, you have a deadlock. Debugging a deadlock can be very difficult, but using multiple threads can improve the performance of your application.
Search google for multithreading programming, I'm sure there are several articles on it.
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
If you use the class in asp.net, or from more than one program running at the same time, there will be more than one thread using the same class.
If you have a static reference in the class that is not thread static, it will be shared by every thread that uses the class. That means that when one program connects to a database, and then another program connects to a second database, both programs will be using the second database. Also when one program closes the connection, the other program will stop working.
---
b { font-weight: normal; }
|
|
|
|
|
theStorminMormon wrote:
if I were to create a static class (say DBController) and then attempt to use it to access my database (eg with the method DBController.getNewClients) then the method (getNewClients) would have to create non-static instances of objects (ie the connection object).
You can create instances of objects in static methods. The isntance with belong to the class and not to any instance. Like this:
class DBController
{
static DBController()
{
conn = new SqlConnection(....);
}
public static Customer GetCustomer(int id)
{
conn.Open();
SqlCommand cmd = new SqlCommand("...", conn);
SqlDataReader rdr = cmd.ExecuteReader();
rdr.Read();
Customer customer = new Customer()
customer.Name = (string) rdr["Name"];
rdr.Close();
conn.Close();
return customer;
}
private static SqlConnection conn;
} While is not precisely the prettiest code, it shows two things: A class can have static objects (in this case a connection that belongs to the class itslef, and not to objects of this class); and a static method can create and return instances of this or any other class.
I hope this helps!
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
Thanks! That really does help! I think I can make my plan work from here. Is this the method you would recommend if I want to be able to access my database methods from various forms? Or would you pass by reference an instantiated DBController object? Or something else?
Thanks for the help!
The ends can never justify the means. It is the means that determine the ends.
|
|
|
|
|
Yes, I would recommend a class with static methods over an instantiated object.
It dependes on how complex is your application, because sometimes I've written specific classes for each table in the database. Other times just the simple static methods.
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
Dear coders!
I've got the following problem:
i have got a windows form in which i show a jpg picture within a picture box.
The user is asked to make some "mouse drawings" on this picture.
As the last step i want to "export" the made image as a twain scanned image, so that applications working with twain devices can get this image.
Is there anyone out there who can help on this? Is this possible?
Hope for help and so far
...may the force be with you
Oliver
|
|
|
|
|
Hello All,
I was wondering if there is a way to supress the security error dialog that the framework pops up. I would like to show my own dialog with certain instructions. Im catching all exceptions in the static void Main. When I run the application in debug mode with the security mode set low, I catch the File IO security exception and it displays my dialog. If I compile the application and run it from the compiled assembly, I get the standard framework security error dialog. I would really like to supress that for my own dialog, what am I missing?
Thanks in advance,
Ryan
|
|
|
|
|
Hello!
Usually you won't catch all exceptions in your application by just having a single try/catch in your Main.
Put the try/catch around the block of code where you expect the SecurityException to occurr, then you should be able to catch it.
Regards,
mav
|
|
|
|
|
I completely realize how to do error handling in an application. I find it useful sometimes for debugging purposes to catch unhandled exceptions at the highest possible point, considering that they bubble up.
|
|
|
|
|
Considering your question and some rather unexpected and not really obvious behaviour of .NET I doubt you do.
Your program and exception handling will behave differently when run in the debugger than when started outside, for example, catching exceptions when run in the debugger but showing the default "an unhandled exception..." dialog when run outside VS.
That's something I've had to learn the hard way, too.
Try the following:
Create a new Windows Forms Project.
Add a second form to the project (Form2).
Put a button (button1) on your Form1 and a button (button2) on Form2.
Encapsulate your Application.Run(new Form1()); in a try/catch.
In the button eventhandler for button1 write
try<br />
{<br />
Form2 f2 = new Form2();<br />
f2.ShowDialog();<br />
}<br />
catch (Exception ex)<br />
{<br />
MessageBox.Show("Exception caught in button1_Click()\n"+ex.ToString());<br />
}
In the button eventhandler for button2 write
throw new ApplicationException("Test Exception");
Now run the application in the debugger, press button1 and then button2.
The exception is caught by the try/catch in button1_Click.
Now run the same application from explorer. The exception will NOT be caught, even though you have a try/catch in the button event handler AND in your Main() !
So either you do as I suggested and catch the exception where it's happening or consider adding an event handler for Application.ThreadException that's just rethrowing the exception from it's ThreadExceptionEventArgs . That way your application will behave the same in the debugger and outside.
Regards,
mav
|
|
|
|
|
Is there any way to insert the date of a project's build into a string variable automatically in C# without me having to manually change the date? Thanks in advance.
John 3:16: "For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life."
|
|
|
|
|
Don't know about automatic...
I would use the PreBuild event, there you can call a simple generator which might create something like "CurrentData.cs", content would be then a simple "const string CURRENT_DATE=...".
----------------------
~hamster1
|
|
|
|
|
Pardon my ignorance, but how would I deal with the PreBuild event (e.g.- is it a member of a certain class in .NET or a feature of the Visual Studio .NET IDE?) or could you at least please point me in the right direction to help me out. I'd appreciate it.
John 3:16: "For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life."
|
|
|
|
|
In VS.NET, open Solution Explorer, right-click on your project (.exe).
Go to Properties > Common Properties > Pre-build...
What you type there is basically a batch file.
----------------------
~hamster1
|
|
|
|
|
Hi all, the app. allows the user to create/add multiple textboxes (let's say) via a button click. Each textbox is docked at the bottom of the last added one. How do I change the order or in other words, move the one at the end to the 2nd place for example..
thanks in advance..
Radgar
"Imagination is more important than knowledge." - Albert Einstein
|
|
|
|
|
I think you should use DockManager to achieve this.
http://www.codeproject.com/cs/miscctrl/DockManager.asp
Live Life King Size
Alomgir Miah
|
|
|
|
|
You can do this by changing the Z order of the controls. BringToFront() and SendToBack() modify the Z order.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Homosexuality in Christianity
Judah Himango
|
|
|
|
|
thank you Judah. I tried that before I posted but the user may add 100 controls and when you modify the z-order, the action get slower and slower.. Thank you anyway.
Radgar
"Imagination is more important than knowledge." - Albert Einstein
|
|
|
|
|
That's probably a bad idea adding hundreds of controls. Each control contains an expensive HWND, could you fake the adding of controls? You could send a WM_Paint event to make the control draw to a surface, drawing it multiple times...
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Homosexuality in Christianity
Judah Himango
|
|
|
|
|
it's a complicated usercontrol Judah. That's why I cannot do what you suggested. thanx anyway.
Radgar
"Imagination is more important than knowledge." - Albert Einstein
|
|
|
|