Results 1 to 7 of 7

Thread: [RESOLVED] [1.0/1.1] Create Object to Use Method

  1. #1

    Thread Starter
    Fanatic Member steve65's Avatar
    Join Date
    Jun 2000
    Posts
    610

    Resolved [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
    This space for rent...

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member steve65's Avatar
    Join Date
    Jun 2000
    Posts
    610

    Re: [1.0/1.1] Create Object to Use Method

    Quote 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
    This space for rent...

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [1.0/1.1] Create Object to Use Method

    Quote 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

  7. #7

    Thread Starter
    Fanatic Member steve65's Avatar
    Join Date
    Jun 2000
    Posts
    610

    Re: [1.0/1.1] Create Object to Use Method

    Thank you both for your help.
    This space for rent...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width