Maccyjam
Maccyjam

Reputation: 93

Passing arguments to a subroutine once, but never again in VB.Net

I have an application with multiple subroutines. The Main subroutine calls the MainMenu subroutine and passes the user's name to it.

I want another subroutine to be able to call the MainMenu again without having to pass an argument. I don't think Optional parameters would work considering they have to have a default value so I was wondering if there was a way to call the subroutine without passing arguments or some form of work around.

The MainMenu subroutine is, as you may have guessed, a menu which calls other subroutines when a user selects an option. I then want these subroutines to call the menu again once they have finished executing. I'm sure there must be some form of workaround here but I just can't seem to figure it out.

Thanks

Upvotes: 1

Views: 157

Answers (2)

tinstaafl
tinstaafl

Reputation: 6958

I think your question is a bit confusing. It sounds to me what you really need is a proper menuing method. Just in case I've got it wrong you may want to be looking at overloading calling the same method with different type of parameters. Here's an example based on an earlier one that shows both:

Friend Name As String = "Hello World"

Sub Main()
    MainMenu("John")

    Console.ReadKey()
End Sub
'Notice how the subroutines come right back to the menu and it resets without being called again.
Public Sub MainMenu(uname As String)
    Dim done As Boolean = False
    While Not done
        Console.Clear()
        Console.WriteLine("{0}{1}{2}{3}",
                          {
                              "Hello " & uname & vbNewLine,
                              "1. Get Upper Case" & vbNewLine,
                              "2. Get Lower Case" & vbNewLine,
                              "3. Done" & vbNewLine
                          })
        Select Case Console.ReadKey.KeyChar
            Case "1"c
                Console.Write(vbNewLine & "Enter string: ")
                Dim temp As String = Console.ReadLine
                If temp = "" Then
                    Console.WriteLine(BuildUpperCaseName())
                Else
                    Console.WriteLine(BuildUpperCaseName(temp))
                End If
            Case "2"c
                Console.Write("Enter string: ")
                Dim temp As String = Console.ReadLine
                If temp = "" Then
                    Console.WriteLine(BuildLowerCaseName())
                Else
                    Console.WriteLine(BuildLowerCaseName(temp))
                End If
            Case "3"c
                done = True
            Case Else
                Console.WriteLine("Invalid choice")
        End Select
        Console.Write("Press any key to continue: ")
        Console.ReadKey(True)
    End While
End Sub
Private Function BuildUpperCaseName() As String
    Return Name.ToUpper()
End Function
Private Function BuildUpperCaseName(Name As String) As String
    Return Name.ToUpper()
End Function
Private Function BuildLowerCaseName(Name As String) As String
    Return Name.ToLower()
End Function
Private Function BuildLowerCaseName() As String
    Return Name.ToLower()
End Function

Upvotes: 0

Karl Anderson
Karl Anderson

Reputation: 34834

Use a property of the class instead, that way any method implementation can choose to grab the property value if need be, like this:

Public Class YourClass
    Private Property Name() As String
         Get
             Return m_Name
         End Get
         Set
             m_Name = Value
         End Set
    End Property
    Private m_Name As String

    Private Function BuildUpperCaseName() As String
        Return Me.Name.ToUpper()
    End Function

    Private Function BuildLowerCaseName() As String
        Return Me.Name.ToLower()
    End Function
End Class

Note: Neither of the methods require a parameter for the Name string, because they grab it from the class itself.


UPDATE:

When using a Module, just declare a variable inside the module, but outside of the two functions so that they both can access the value, like this:

Module YourModule
    Friend Name As String = "Hello World"

    Sub Main()
        ' Call BuildUpperCaseName or BuildLowerCaseName here
        ' both of which will use the same Name variable

    End Sub

    Private Function BuildUpperCaseName() As String
        Return Name.ToUpper()
    End Function

    Private Function BuildLowerCaseName() As String
        Return Name.ToLower()
    End Function
End Module

Upvotes: 1

Related Questions