|
-
Jun 14th, 2006, 01:38 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] [1.0/1.1] Create Object to Use Method
Hi All,
I have a method to tell me if a user has entered an invalid key during a textbox's key press event.
Code:
private bool InvalidCharacters(System.Windows.Forms.KeyPressEventArgs e)
{
bool returnValue = false;
if(e.KeyChar == '\''){returnValue = true;}
else if(e.KeyChar == '`'){returnValue = true;}
else if(e.KeyChar == ','){returnValue = true;}
return returnValue;
}
I would like to use this method in different classes that are all part of the same name space and class library. [Here comes the DUH part] The only way I have been able to call the method, is to create an object of the class that the method is in and then call the method. Is there a way to call the method directly if it is in the same namespace as the code without having to create the object like shown below?
Code:
private void txtUserOSName_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(InvalidCharacters(e)){e.Handled = true;}
}
I hope that makes sense.
Thanks Steve
-
Jun 14th, 2006, 03:59 PM
#2
Re: [1.0/1.1] Create Object to Use Method
Mark is "static" and you won't have to instantiate an object to use it, just use "ClassName.StaticMember"
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 14th, 2006, 06:44 PM
#3
Re: [1.0/1.1] Create Object to Use Method
You shouldn't be passing the KeyPressEventArgs object to the InvalidCharacters method when all you're using is one of its properties. You should be passing just that property:
Code:
private void txtUserOSName_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
e.Handled = IsCharacterInvalid(e.KeyChar);
}
private bool IsCharacterInvalid(char c)
{
switch (c)
{
case '\'':
case '`':
case ',':
return true;
default:
return false;
}
}
As cj says, make it a static member of some class and it can be called from anywhere that can access that type, but you sould not just make it a static member of the class that it's currently a member of. Unless it is appropriately a member of some particular existing class you should create a new class specifically for utility functions and make it a member of that.
-
Jun 14th, 2006, 08:52 PM
#4
Thread Starter
Fanatic Member
Re: [1.0/1.1] Create Object to Use Method
 Originally Posted by ComputerJy
Mark is "static" and you won't have to instantiate an object to use it, just use "ClassName.StaticMember"
Because I do not know any better, is it wrong to have a bunch of members in a class all set to static? I can see me having a number of helper methods like the one above. I do not know if this would be the best way to handle this type of scenario or if there is a better way.
Steve
-
Jun 14th, 2006, 09:44 PM
#5
Re: [1.0/1.1] Create Object to Use Method
Every method has to be a member of a class. You make each method a member of the class that it is most appropriate that it belong to. If it's a utility function that doesn't logically belong to any other class then you must declare a class specifically for this and other utility functions. This is basically how modules are (correctly) used in VB.NET. The VB.NET Runtime contains numerous modules, like Strings, Interaction, DateAndTime, etc. and each of those modules contains utility functions that relate to that area. When it's compiled a VB.NET module becomes a NotInheritable class with all Shared members. The C# equivalent is a sealed class with all static members, which is what you should create.
-
Jun 15th, 2006, 04:28 AM
#6
Re: [1.0/1.1] Create Object to Use Method
 Originally Posted by steve65
Because I do not know any better, is it wrong to have a bunch of members in a class all set to static? I can see me having a number of helper methods like the one above. I do not know if this would be the best way to handle this type of scenario or if there is a better way.
Steve
It's always up to you. But I'd do it
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 15th, 2006, 06:04 AM
#7
Thread Starter
Fanatic Member
Re: [1.0/1.1] Create Object to Use Method
Thank you both for your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|