Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Pass Generic List (List) as Parameter to Function in C#, VB.NET

Jan 9, 2017
Introduction:

Here we will learn how to send list (List<string>) as parameter to function in asp.net using c#, vb.net with example or asp.net pass generic list as parameter to method using c#, vb.net with example or asp.net pass list object as parameter in function using c#, vb.net with example or pass list<class> as an argument to the function in asp.net using c#, vb.net with examples. We can easily send list values as a parameter to the function normal like other functions with parameters.

Description:

In previous articles I explained get first element from ienumerable list in c#, vb.net, convert datatable to ienumerable list in c#, vb.net with example, difference between iqueryable and ienumerable in c#, vb.net, Different types of constructors in c#, vb.net, destructor in c#, vb.net with examples and many more articles related to in asp.net, c#, vb.net. Now I will explain how to send or pass generic list as a parameter to the function in asp.net using c#, vb.net with example.

Generally in c# or vb.net to send generic list as a parameter in function we need to write the code like as shown below.

C# Code


public void yourfunction(List<UserDetails> user)
{
----------------------
---- your code -----
---------------------
}

VB.NET Code


Public Sub yourfunction(user As List(Of UserDetails))
--------------------------------
--------- your code -----------
--------------------------------
End Sub

If you want complete example of sending list as a parameter in function open web application page and write code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Send Generic List as a Parameter to Function in C#, Vb.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvDetails" runat="server">

</asp:GridView>
</div>
</form>
</body>
</html>

Now open code behind file and write the code like as shown below

C# Code


using System;
using System.Collections.Generic;

public partial class SendListAsParameter : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<UserDetails> userinfo = new List<UserDetails>();
UserDetails user = new UserDetails();
user.userid = 1;
user.username = "Suresh Dasari";
user.education = "B.Tech";
user.location = "Hyderabad";
userinfo.Add(user);
user = new UserDetails();
user.userid = 2;
user.username = "Rohini Alavala";
user.education = "Msc";
user.location = "Guntur";
userinfo.Add(user);
user = new UserDetails();
user.userid = 3;
user.username = "Praveen Kumar";
user.education = "B.Tech";
user.location = "Bangalore";
userinfo.Add(user);
user = new UserDetails();
user.userid = 4;
user.username = "Madhav Sai";
user.education = "MBA";
user.location = "Nagpur";
userinfo.Add(user);
BindGridview(userinfo);
}
}
public void BindGridview(List<UserDetails> user)
{
gvDetails.DataSource = user;
gvDetails.DataBind();
}
}

public class UserDetails
{
public int userid { get; set; }
public string username { get; set; }
public string education { get; set; }
public string location { get; set; }
}

VB.NET Code


Imports System.Collections
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim userinfo As New List(Of UserDetails)()
Dim user As New UserDetails()
user.userid = 1
user.username = "Suresh Dasari"
user.education = "B.Tech"
user.location = "Hyderabad"
userinfo.Add(user)
user = New UserDetails()
user.userid = 2
user.username = "Rohini Alavala"
user.education = "Msc"
user.location = "Guntur"
userinfo.Add(user)
user = New UserDetails()
user.userid = 3
user.username = "Praveen Kumar"
user.education = "B.Tech"
user.location = "Bangalore"
userinfo.Add(user)
user = New UserDetails()
user.userid = 4
user.username = "Madhav Sai"
user.education = "MBA"
user.location = "Nagpur"
userinfo.Add(user)
BindGridview(userinfo)
End If
End Sub
Public Sub BindGridview(user As List(Of UserDetails))
gvDetails.DataSource = user
gvDetails.DataBind()
End Sub
End Class

Public Class UserDetails
Public Property userid() As Integer
Get
Return m_userid
End Get
Set
m_userid = Value
End Set
End Property
Private m_userid As Integer
Public Property username() As String
Get
Return m_username
End Get
Set
m_username = Value
End Set
End Property
Private m_username As String
Public Property education() As String
Get
Return m_education
End Get
Set
m_education = Value
End Set
End Property
Private m_education As String
Public Property location() As String
Get
Return m_location
End Get
Set
m_location = Value
End Set
End Property
Private m_location As String
End Class

Demo

When we run above code we will get result like as shown below.


Pass Generic List (List<string>) as Parameter to Function in C#, VB.NET

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

0 comments :

Give your Valuable Comments

Other Related Posts

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.