Results 1 to 9 of 9

Thread: [RESOLVED] better way to do this?

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Resolved [RESOLVED] better way to do this?

    I came up with this little trick on my own but i have a feeling that its wildly inefficient


    I tried saying for each MatchType1 in the tabpage.controls

    but it didn't seem to understand that i would onlly want to retreive matchtyp1's and not labels or textboxes


    so i set it to loop through everything but if i do that i can't access the contols methods directly i have to create a new one and then set it equal to what C is pointing at.

    anyway i thought i would run this past the experts to see if there is a better way to do this



    c# Code:
    1. private void ClearRegion(string Region)
    2.         {
    3.             foreach (Control C in StringtoTabPage(Region).Controls)
    4.             {
    5.             if (C is MatchType1)
    6.             {
    7.                 MatchType1 holder = new MatchType1();
    8.                 holder = (MatchType1)C;
    9.  
    10.                 holder.ClearInfo();
    11.            
    12.             }
    13.                 if(C is MatchType2)
    14.                 {
    15.                     MatchType2 holder = new MatchType2();
    16.                     holder = (MatchType2)C;
    17.  
    18.                     holder.ClearInfo();
    19.                 }
    20.                 if (C is TextBox)
    21.                 {
    22.                     TextBox holder = new TextBox();
    23.                     holder = (TextBox) C;
    24.                     holder.Clear();
    25.                
    26.                 }
    27.            
    28.             }
    29.         }

  2. #2
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: better way to do this?

    there are ways to improve it... but since im cut for time, ill quickly (And probably missing some things) point out a couple of things

    check for null objects, otherwise you will get a NullReferenceException when dealing with the object in question

    you can also do this:



    if (C is MatchType1)
    {

    MatchType1 holder = ((MatchType1)C);
    // OR even better:
    // MatchType1 holder = C as MatchType1;

    if (holder != null)
    {

    holder.ClearInfo();

    }
    }

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

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

    Re: better way to do this?

    There are several issues with your code. First up, when you find the type of the control you're creating a new instance of that type, then discarding that and using the one on the form:
    Code:
    MatchType1 holder = new MatchType1();
    holder = (MatchType1)C;
    You ONLY use the 'new' key word to invoke a constructor and create a new instance. Do you want to create a new instance here? Of course not.

    Next, you should be using 'else if' for the second and third blocks. If the first condition is true and C is type MatchType1 then what's the point of testing to see if it's MatchType2 or TextBox? You know for a fact that it's not so why check?

    Finally, I could be wrong but I'm guessing that MatchType1 and MatchType2 are both derived from the same base class. If not then I'm guessing that they should be. If the ClearInfo method is inherited from that base class then you don't have to test for both separately. You can just test for the base type and call its ClearInfo method.

    Techno's idea is what you generally should have used in the past and quite possibly should now too. It's good to keep LINQ in mind too though. It's probably less attractive as you're looking for more than one type but you can enumerate the result of a LINQ query, which allows you to filter:
    CSharp Code:
    1. foreach (TextBox txtbx in from Control ctl in this.Controls
    2.                           where ctl is Button
    3.                           select ctl)
    4. {
    5.     txtbx.Clear();
    6. }
    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
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: better way to do this?

    thats a good demo about using LINQ - interesting. havent seen that before. must investigate further on that....

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

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

    Re: better way to do this?

    Quote Originally Posted by Techno
    thats a good demo about using LINQ - interesting. havent seen that before. must investigate further on that....
    People may tend to assign the result of the query to a variable and then use that in the foreach loop but I hate using intermediate variables that don't serve to clarify. The IDE formats the LINQ query nicely so it's quite clear what's happening, as long as you've got a bit of a grasp of LINQ. A LINQ to Objects query returns an IEnumerable object and that's all that a foreach loop requires.
    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
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: better way to do this?

    absolutely. awesome!

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  7. #7

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: better way to do this?

    again thank you I'll give it a whirrl at work and post my final results or any questions to clarify i have.

    thanks

  8. #8

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: better way to do this?

    Better?

    also im using the generic method to return the type i need and access it directly rather than do a shell game with creating new contorls

    c# Code:
    1. private void ClearRegion(string Region)
    2.         {
    3.             foreach (Control C in StringtoTabPage(Region).Controls)
    4.             {
    5.                 if (C is MatchType1)
    6.                 {
    7.                     FindControl<MatchType1>(C.Name).ClearInfo();
    8.                 }
    9.                 else if (C is MatchType2)
    10.                 {
    11.                     FindControl<MatchType2>(C.Name).ClearInfo();
    12.                 }
    13.                 else if (C is TextBox)
    14.                 {
    15.                     FindControl<TextBox>(C.Name).Clear();
    16.                 }
    17.             }
    18.         }

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

    Re: [RESOLVED] better way to do this?

    Um, that doesn't really make a lot of sense. You're looping through the controls and, for each one, you get it's Name and then pass that to another method to go and get the very same control. Why do you need this FindControl method to find the control when you've already got it? If you're going to loop through all the controls anyway then the FindControl method is pointless. Just cast C as the appropriate type and then call the method:
    CSharp Code:
    1. if (C is MatchType1)
    2. {
    3.     ((MatchType1)C).ClearInfo();
    4. }
    From what I can see of this thread and the other one of yours related to this one, what you should be doing is this:
    CSharp Code:
    1. foreach (TextBox tb in this.Controls.OfType<TextBox>())
    2. {
    3.     tb.Clear();
    4. }
    You just do that three times: once for each type.
    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

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