PDA

Click to See Complete Forum and Search --> : [RESOLVED] [1.0/1.1] Create Object to Use Method


steve65
Jun 14th, 2006, 01:38 PM
Hi All,

I have a method to tell me if a user has entered an invalid key during a textbox's key press event.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?private void txtUserOSName_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(InvalidCharacters(e)){e.Handled = true;}
}I hope that makes sense.

Thanks Steve

ComputerJy
Jun 14th, 2006, 03:59 PM
Mark is "static" and you won't have to instantiate an object to use it, just use "ClassName.StaticMember"

jmcilhinney
Jun 14th, 2006, 06:44 PM
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: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.

steve65
Jun 14th, 2006, 08:52 PM
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

jmcilhinney
Jun 14th, 2006, 09:44 PM
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.

ComputerJy
Jun 15th, 2006, 04:28 AM
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

steve65
Jun 15th, 2006, 06:04 AM
Thank you both for your help.