Share via

Elseif statement

Simon Scott 306 Reputation points
2021-02-02T21:33:10.14+00:00

Good evening,

Is someone able to tell me why this elseif statement isn't correct please?

    If Environment.MachineName = "file001" Then
        lblHostname.Text = "Fileserver 1"
    Elseif
        Environment.MachineName = "file001" Then
        lblHostname.Text = "Fileserver 2"
    Elseif
        Environment.MachineName = "file001" Then
        lblHostname.Text = "Fileserver 3"
    Elseif
        Environment.MachineName = "file001" Then
        lblHostname.Text = "Fileserver 4"
    End If

Apologies i'm not the greatest programmer in the world but learning :)

Thanks

Developer technologies | .NET | Other
0 comments No comments

Answer accepted by question author

Xingyu Zhao-MSFT 5,381 Reputation points
2021-02-03T02:54:08.297+00:00

Hi @Simon Scott ,
Also note that ‘ElseIf’ and ‘Then’ should be written on one line.

        If Environment.MachineName = "file001" Then  
            lblHostname.Text = "Fileserver 1"  
        ElseIf Environment.MachineName = "file002" Then  
            lblHostname.Text = "Fileserver 2"  
        ElseIf Environment.MachineName = "file003" Then  
            lblHostname.Text = "Fileserver 3"  
        ElseIf Environment.MachineName = "file004" Then  
            lblHostname.Text = "Fileserver 4"  
        End If  

Best Regards,
Xingyu Zhao
*
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?


3 additional answers

Sort by: Most helpful
  1. Simon Scott 306 Reputation points
    2021-02-03T13:36:20.437+00:00

    Hi,

    Thanks for all your answers.

    The idea is that this application will be installed on 4 different servers.

    Depending on which server it is installed on, my code will then write to a text file on that specific server.

    Unless of course a case statement would be better?

    Thanks
    Simon

    Was this answer helpful?


  2. Andriy Bilous 12,186 Reputation points MVP
    2021-02-02T21:47:28.36+00:00

    Hello @Simon Scott
    You are trying to check the Environment.MachineName = "file001" in all of you statements
    So if Environment.MachineName = "file001" is true that you will get lblHostname.Text = "Fileserver 1"
    if Environment.MachineName = "file001" is false, none of your statements will pass

    Was this answer helpful?

    0 comments No comments

  3. Anonymous
    2021-02-02T21:45:33.1+00:00

    All four lines are evaluating the same string.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.