Hi, I have created a module to calculate a level when given a score. How do I send the calculated level back into my main form? Thanks in advance

Dani AI

Generated

Brief practical guidance and examples for returning a computed value from a Visual Basic module, plus a few traps to watch for. Thanks to for the original question and to and for the early help — below are concrete patterns and checks that help future readers reproduce a working result reliably.

A simple, idiomatic VB.NET module function and how to call it from a form:

Module ScoreUtils

Public Function LevelFromScore(ByVal score As Integer) As Integer
    Select Case score
        Case Is < 100
            Return 1
        Case 100 To 199
            Return 2
        Case Else
            Return 3
    End Select
End Function

End Module

Call from a form and assign the result:

Dim score As Integer = Integer.Parse(txtScore.Text)
Dim level As Integer = LevelFromScore(score)
lblLevel.Text = level.ToString()

If you need more than one output, prefer returning a structure or use ByRef parameters for older code:

Public Sub ComputeLevel(score As Integer, ByRef level As Integer, ByRef badge As String)
    level = If(score < 100, 1, If(score < 200, 2, 3))
    badge = If(level = 1, "Bronze", If(level = 2, "Silver", "Gold"))
End Sub

Troubleshooting checklist: enable Option Strict On; ensure the procedure you call is a Function (or returns a value) and that the caller assigns the returned value; watch for name collisions with controls or variables; convert input types safely (Integer.Parse/TryParse); use the debugger/QuickWatch to confirm the function executes and returns the expected value. For reference on syntax and argument passing, see Microsoft’s documentation on function procedures and passing arguments:
Function procedures and .

Recommended Answers

All 4 Replies

Kindly post your code..

call the function from module and calculate in form

Thanks, I have got it to work now. I was using a sub instead of function by mistake, so no value was being returned!

great work. happy coding friend :)
don't forget to mark this thread solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.