How to Pass a Parameter from Vb.Net

Passing a parameter to a function to get a string back

You can't return something from a Sub; it has to be a Function:

Private Function GetValueFromFunction(Optional ValueFromFunc as String = Nothing) As String
Dim returnValue As String
'do stuff...
If someCondition Then
returnValue = "OK"
Else
returnValue = "NOTOK"
End If

Return returnValue
End Function

A Function is a method that returns something, whereas a Sub is a method that doesn't return something (it's more like an action). In C#, a Sub would return void.

How to pass a parameter from vb.net

//These are the date variables.. if u need them seperately

Dim TodayDt As DateTime = DateTime.Today
Dim Tomorrow As DateTime = DateTime.Today.AddDays(1)
Dim TodayEnd as DateTime
TodayEnd = Tomorrow.AddSeconds(-1)

//This is the SQL Command that executes in SQL Server

  SELECT
SUM(QTY) AS Discounts
FROM
dbo.fFinancialDataFull('Date Range Report', startdate , enddate, '1', '1', 'ALL', 'ALL', 'ALL', 'ALL', '1', '1', '1', '1', '1') AS fFinancialDataFull_1
WHERE ReportCategoryID = 62
AND startdate = TodayDt
AND enddate = TodayEnd AS unlimitedtbl

//This is the function u need to write to make the same SQL run on VB

Public Function GetValueByDates() As String
Dim TodayDt As DateTime = DateTime.Today
Dim Tomorrow As DateTime = DateTime.Today.AddDays(1)
Dim TodayEnd as DateTime
TodayEnd = Tomorrow.AddSeconds(-1)
Dim ReportCategoryID = 62

Dim sql As String = " SELECT
SUM(QTY) AS Discounts
FROM
dbo.fFinancialDataFull('Date Range Report', startdate , enddate, '1', '1', 'ALL', 'ALL', 'ALL', 'ALL', '1', '1', '1', '1', '1') AS fFinancialDataFull_1
WHERE ReportCategoryID = @ReportCategoryID
AND startdate = @TodayDt
AND enddate = @TodayEnd AS unlimitedtbl"

Using cn As New SqlConnection("Your connection string here"), _
cmd As New SqlCommand(sql, cn)

cmd.Parameters.Add("@TodayDt", SqlDbTypes.DateTime).Value = TodayDt
cmd.Parameters.Add("@TodayEnd", SqlDbTypes.DateTime).Value = TodayEnd
cmd.Parameters.Add("@ReportCategoryID", SqlDbTypes.int).Value = ReportCategoryID
Return cmd.ExecuteScalar().ToString()
End Using
End Function

Passing parameters between two forms in VB.Net

If there is only one frmSummary, you could make it a singleton.

In frmSummary, put the following code:

Private Shared _instance As frmSummary

Private Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub

Public Shared Function GetInstance() As frmSummary
If _instance Is Nothing Then
_instance = New frmSummary()
End If
Return _instance
End Function

Public Sub PutDataInGrid(data As Object)
Me.DataGridView1.' put data in it
End Sub

And you would access it from other forms like this

Dim myFrmSummary = frmSummary.GetInstance()
myFrmSummary.PutDataInGrid(myData)

How to pass a form as the parameter of a function vb.net

You can pass the form object to a Sub() in a module as below

Module Printing

Dim StrToAdd As String
Sub MySub(ByVal frm As Form)
'The first line is your code
StrToAdd = "Firstname: " & addmember.txtName.Text
'Change it to as below using frm.Controls("controlname").Text
StrToAdd = "Firstname: " & frm.Controls("txtName").Text
End Sub

End Module

How Do I Pass A Subprocedure As An Argument VB.NET

You can declare your method (Sub) as taking an Action Delegate parameter. This allows you to pass the address of a void method (HelloWorld) as a parameter:

Private Sub DoSomething(a As Action)
' pick a random number 1-5
Dim v = RNG.Next(1, 6)
' call whatever v times
For n As Int32 = 0 To v
a()
Next
End Sub

Private Sub HelloWorld()
Console.WriteLine("Null Spark says 'Hello, World!'")
End Sub

Calling it would just be:

DoSomething(AddressOf HelloWorld)

As MSDN notes, Action encapsulates a method that has no parameters and does not return a value. To include parameters use Action(of T) (see next); to return a value (use a function), use Func, see this example


When there are params involved use Action(Of T). You can also declare variables as Action Delegates which can make the code easier to read:

Private HelloAction As Action(Of Int32)

Then somewhere like a form load:

HelloAction = AddressOf HelloWorld
...
Private Sub DoSomething(a As Action(Of Int32))
Dim v = RNG.Next(1, 6)
For n As Int32 = 0 To v
a(n) ' pass the int param
Next
End Sub

In this case, HelloWorld would be:

Private Sub HelloWorld(x As Integer)

Passing Parameters from one page to another VB.net

Haha wow, the whole time was because I had the parameter named "user" in the URL and was decrypting it as UserName... Face Palm

vb.net passing Function as argument

For passing functions as parameters in vb.net you can use AddressOf keyword.

Public Sub Execute(Action action)
Console.WriteLine("Before")
action()
Console.WriteLine("After")
End

Public Sub MyAction()
' Do something
End Sub

' And call
Execute(AddressOf MyAction)

But because you passing printer as argument to the function you want to call later, you can not use AddressOf.

Instead wrap your method with anonymous action where printer is already given

Main.setWaitScreen(msg:= "Printing...", action:= Sub() cr.print(printerName:=printer))

Pass function as a parameter in vb.net?

You can also use a delegate so you can pass function/subs that have a certain signature.



Related Topics



Leave a reply



Submit