PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 [RESOLVED] a better way to initialize a tn array of textbox-VBForums
Results 1 to 15 of 15

Thread: [RESOLVED] a better way to initialize a tn array of textbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2013
    Posts
    401

    Resolved [RESOLVED] a better way to initialize a tn array of textbox

    Hello
    I have been using this
    Code:
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            dim tbox(21) as textbox
            tbox(0) = TextBox1
            tbox(1) = TextBox2
            tbox(2) = TextBox3
            tbox(3) = TextBox4
            tbox(4) = TextBox5
            tbox(5) = TextBox6
            tbox(6) = TextBox7
            tbox(7) = TextBox8
            tbox(8) = TextBox9
            tbox(9) = TextBox10
            tbox(10) = TextBox11
            tbox(11) = TextBox12
            tbox(12) = TextBox13
            tbox(13) = TextBox14
            tbox(14) = TextBox15
            tbox(15) = TextBox16
            tbox(16) = TextBox17
            tbox(17) = TextBox18
            tbox(18) = TextBox19
            tbox(19) = TextBox20
            tbox(20) = TextBox21
            tbox(21) = TextBox22
    This is tedious there must be a better way.
    Thanks
    George

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,280

    Re: a better way to initialize a tn array of textbox

    It's much simpler like this:
    Code:
            dim tbox() as textbox = { TextBox1, TextBox2, TextBox3, ... TextBox21, TextBox22 }
    ..and if you want to make it easier to read, you can add line breaks after some or all of the textboxes.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2013
    Posts
    401

    Re: a better way to initialize a tn array of textbox

    Yes it is, thank you. But since the textbox names a sequential can enumeration or something be used?

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,383

    Re: a better way to initialize a tn array of textbox

    If it's all the TextBoxes in that container then you can do this:
    vb.net Code:
    1. Dim textboxes = Controls.OfType(Of TextBox)().ToArray()
    If that's not the case and you must use their sequential names then you can do this:
    vb.net Code:
    1. Dim textboxes = Enumerable.Range(1, 22).
    2.                            Select(Function(n) Controls("TextBox" & n)).
    3.                            Cast(Of TextBox)().
    4.                            ToArray()
    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

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,622

    Re: a better way to initialize a tn array of textbox

    One thing to highlight from what JMC just posted (some day I'll remember how to spell his name...but not today): All controls exist in a Controls collection. If the controls are on the form, they are in the controls collection of that form. If they are on a panel, in a group box, on a tab page, or anything like that, then they are in the controls collection for that container. All controls have a controls collection, so you could put controls into a textbox if you wanted to (though this is very rarely done). As long as you can identify the controls collection that has your controls, that's half the battle.

    Within the controls collection, your controls can be identified by name, hence the Controls("TextBox" & n) from the preceding example. Effectively, the controls collection is a dictionary where the control name is the key and the control itself is the value. Take your pick as to how to iterate through the controls in the controls collection. You've got a couple good examples there, but if you don't like LINQ/Lambda, you can just create your own loop from 1 to 22 and get the controls that way.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2013
    Posts
    401

    Re: a better way to initialize a tn array of textbox

    JMC's example has parentheses after Cast(Of TextBox)() and after ToArray() why? They don't seem to be needed, still works without them.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,622

    Re: a better way to initialize a tn array of textbox

    Without the .ToArray() the return should be a list. You asked for an array, so you got an array. Quite likely, you wouldn't care whether you got a list or an array. After all, a list is just a wrapper over an array that adds functionality for adding and removing, which makes no difference to you, since you wouldn't be using that functionality. Still, you asked for an array, so he gave you an array.

    As to the Cast(), take a look at the type of the textboxes object. I would expect that it would be type Control, not type Textbox. A Controls collection holds controls. Whether those controls are textboxes or something else doesn't matter to the Controls collection. If you have Option Strict OFF, it likely doesn't matter to your code whether you have a list of Textbox or a list of Control, because you will be implicitly converting Control to Textbox whenever you use it as a textbox. I would think that would cause issues with intellisense, but depending on how you are using the array, you may never see that.

    Option Strict OFF is a bad idea, though. Implicit conversions are easy...and they will easily cover up bugs that will catch you later on. Without that cast, that code may well be hiding a bug for later.
    My usual boring signature: Nothing

  8. #8
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,216

    Re: a better way to initialize a tn array of textbox

    Quote Originally Posted by Shaggy Hiker View Post
    One thing to highlight from what JMC just posted (some day I'll remember how to spell his name...but not today):
    After 10+ years here I can still only remember JMC

  9. #9
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,216

    Re: a better way to initialize a tn array of textbox

    Quote Originally Posted by georgesutfin View Post
    JMC's example has parentheses after Cast(Of TextBox)() and after ToArray() why? They don't seem to be needed, still works without them.
    Lots of things work that should not. I am going to go out on a massive limb here and be corrected as I am not 100% sure but its maybe to do with "Type assertions".

    Take:

    Code:
    dim ident() as string
    dim ident as string()
    both will work, but only one is good practice.

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,383

    Re: a better way to initialize a tn array of textbox

    Quote Originally Posted by georgesutfin View Post
    JMC's example has parentheses after Cast(Of TextBox)() and after ToArray() why? They don't seem to be needed, still works without them.
    In VB, you can omit the parentheses on a method call if there are no arguments. In a case like this:
    vb.net Code:
    1. Dim result = someObject.SomeMethod(someValue)
    the parentheses are required because an argument is passed to the method. If there are no arguments then you can do this:
    vb.net Code:
    1. Dim result = someObject.SomeMethod()
    or this:
    vb.net Code:
    1. Dim result = someObject.SomeMethod
    Personally, I always include parentheses on method calls to distinguish them from properties. The one place that I don't use optional parentheses is on constructors, i.e. I do this:
    vb.net Code:
    1. Dim someObject As New SomeType
    rather than this:
    vb.net Code:
    1. Dim someObject As New SomeType()
    That's because doing so means that I can easily distinguish between object creation and array declaration, i.e. omitting the New keyword would actually change the type if parentheses are used:
    vb.net Code:
    1. Dim someObject As SomeType()
    Those are my personal preferences and you can use whatever you like but, whatever you do choose, you should have a reason for it and stick to it consistently.
    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

  11. #11
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,216

    Re: a better way to initialize a tn array of textbox

    I noticed John you said you won't include parentheses regarding constructors. I know you use Resharper the same as me. Am I correct thinking resharper will add this or is it ctrl+k+d that will justify the text. I am not in a position to check but either way, I agree I also won't include extra noise however it appears VS or RS enforces this?

    I could be completely wrong.

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,383

    Re: a better way to initialize a tn array of textbox

    Quote Originally Posted by ident View Post
    I noticed John you said you won't include parentheses regarding constructors. I know you use Resharper the same as me. Am I correct thinking resharper will add this or is it ctrl+k+d that will justify the text. I am not in a position to check but either way, I agree I also won't include extra noise however it appears VS or RS enforces this?

    I could be completely wrong.
    ReSharper does indeed add parentheses to a method call but not to a constructor. I wasn't sure so I just tested and that was the case. I guess I've never actually noticed because that's what I used to do before I used ReSharper as well. I don't know what their logic is but I'd like to think that it's the same as mine.
    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

  13. #13
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,216

    Re: a better way to initialize a tn array of textbox

    Just fired up VS and that's correct. Guess I just assumed lol

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2013
    Posts
    401

    Re: a better way to initialize a tn array of textbox

    thank you

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,622

    Re: a better way to initialize a tn array of textbox

    Just realized that I misread, and answered, the wrong question back there.
    My usual boring signature: Nothing

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width