Hi friends,
I want to know about the params keyword and how to use the params keyword.?
thank you.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted May 8, 2012, 1:02 PM
The params keyword allows methods to have a variable length parameter list.
For example, the following class defines a method called "MultiPrint", which can have any number of string's passed to it.
using System;
public class MyApplication
{
public static void MultiPrint(params string[] list)
{
for ( int i = 0 ; i < list.Length ; i++ )
Console.WriteLine(list[i]);
}
public static void Main()
{
MultiPrint("First", "Second", "Third");
MultiPrint("Fourth");
MultiPrint("Fifth", "Sixth");
}
}
Running this program prints out:
First
Second
Third
Fourth
Fifth
Sixth
There can only be one params keyword per method declaration, and it can only be on the final parameter in the list.
Parameter array in vb.net
Condition of using this array
1.It must be the lone argument in the parameter declaration or it is used only once.
2.It is passed by call by value.
3.It must be the one dimensional array.
4.It must be the last argument in the parameter declaration.
5.A keyword param array is used after by val.
Program
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
sname("Student Name", "raj", "ravi", "abhijit", "rahul", "adarsh")
End Sub
Private Sub sname(ByVal s As String, ByVal ParamArray ar() As String)
Dim i As Integer
TextBox1.Text += s + vbCrLf
For i = 0 To UBound(ar)
TextBox1.Text += ar(i) + vbCrLf
Next
End Sub
End Class
Output
Student Name
raj
ravi
abhijit
rahul
adarsh
Please refer the below link
http://www.csharpfriends.com/articles/getarticle.aspx?articleid=49
Thanks
VulpesPosted May 8, 2012, 12:13 PM
Check this link for an example:
http://msdn.microsoft.com/en-us/library/w5zay9db.aspx