5 Replies - 355 Views - Last Post: 23 August 2013 - 03:59 PM Rate Topic: -----

#1 Cryru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-April 13

Question: Writing shorter code and for each loops

Posted 23 August 2013 - 03:21 PM

Hello,
I started coding an application that sort of manages other programs on my machine but I have run into a problem.
I do not really know how to make it so an operation is performed for each app without having to define a thousand strings and without having to use a separate block of code for each app.



Example:

Private Sub UpdateCounter
        app_updates_available = 0 'Refresh app update number
        'get number of updates for counter
        If version_buffer1 = app1_installedversion Then 'Check if update for counter
        Else
            app_updates_available = app_updates_available + 1 'Update App_updates counter
        End If

        If version_buffer2 = app2_installedversion Then 'Check if update for counter
        Else
            app_updates_available = app_updates_available + 1 'Update App_updates counter
        End If

        If version_buffer3 = app3_installedversion Then 'Check if update for counter
        Else
            app_updates_available = app_updates_available + 1 'Update App_updates counter
        End If

        If version_buffer4 = app4_installedversion Then 'Check if update for counter
        Else
            app_updates_available = app_updates_available + 1 'Update App_updates counter
        End If

        If version_buffer5 = app5_installedversion Then 'Check if update for counter
        Else
            app_updates_available = app_updates_available + 1 'Update App_updates counter
        End If

        If version_buffer6 = app6_installedversion Then 'Check if update for counter
        Else
            app_updates_available = app_updates_available + 1 'Update App_updates counter
        End If

[etc...]



Is there a way to cycle through them all without having to type them out? I did some research and came to the conclusion that I could use a 'for each' loop but I don't really understand how. I would love if someone knowledgeable can clear up and direct me in the right direction.
Cheers.

Is This A Good Question/Topic? 0
  • +

Replies To: Question: Writing shorter code and for each loops

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3624
  • View blog
  • Posts: 12,538
  • Joined: 12-December 12

Re: Question: Writing shorter code and for each loops

Posted 23 August 2013 - 03:29 PM

If you don't know how to use a for each loop then you need to stop trying to build an application and study the fundamentals of the language.

dotnetperls example

There is a tutorial linked in my signature.

BTW The accepted standard is that VB.NET does not use underscore-separation within identifiers. There is nothing wrong, as such, with you doing this but it looks odd to all other VB programmers, and probably an indicator that you have not studied the essentials of the language.

This post has been edited by andrewsw: 23 August 2013 - 03:35 PM

Was This Post Helpful? 1
  • +
  • -

#3 Cryru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-April 13

Re: Question: Writing shorter code and for each loops

Posted 23 August 2013 - 03:33 PM

That was certainly helpful but I'm still not sure how to apply it to my case.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3624
  • View blog
  • Posts: 12,538
  • Joined: 12-December 12

Re: Question: Writing shorter code and for each loops

Posted 23 August 2013 - 03:41 PM

View PostCryru, on 23 August 2013 - 10:33 PM, said:

That was certainly helpful but I'm still not sure how to apply it to my case.

You wouldn't. You would study the page, type the examples and run them, find and run other examples. Once you understand loops then you can begin to apply them to your own code.
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Question: Writing shorter code and for each loops

Posted 23 August 2013 - 03:41 PM

Underscore is often used as a visual identifier for private/internal variables, when it is the first character of the variable name. _Name
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3624
  • View blog
  • Posts: 12,538
  • Joined: 12-December 12

Re: Question: Writing shorter code and for each loops

Posted 23 August 2013 - 03:59 PM

View PostAdamSpeight2008, on 23 August 2013 - 10:41 PM, said:

Underscore is often used as a visual identifier for private/internal variables, when it is the first character of the variable name. _Name

Some people use m_Name , or even btn_Some, but I've never seen the underscore used with every variable :dontgetit:

This post has been edited by andrewsw: 23 August 2013 - 04:01 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1