• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 616
  • Last Modified:

Using C# Array in many funtions

Hi,

I need to use a global array where I can use it in many functions using C#, what is the best way to do it?

Thank you,
Rad1
0
Rad1
Asked:
Rad1
  • 5
  • 4
  • 2
  • +3
10 Solutions
 
informaniacCommented:
Store it in a Session variable

Session["globarr"] = myArray;

Open in new window

0
 
Manoj PatilSr. Software EngineerCommented:
Are you going to use the same array in different pages ?
If yes then you can use Session to keep it live....
If you are using this on same page, make sure that post back doesn't reset your array.
For this, you just need to declare array as a global variable...
And use this in all your function.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
aplusexpertCommented:
Yes, if you want to use in other pages of application then store array in Session variable and if  that array is only used for particular page then you can define array gobaly on that page or store in viewstate.

Thanks...
0
 
Todd GerbertIT ConsultantCommented:
If every user needs to share the same array, store it in an Application variable. If the array is static, that is it's contents never change - like a list of the days of the week, for example - store it in the web.config.
0
 
Todd GerbertIT ConsultantCommented:
...on an unrelated note, I think that might wrap that array in a class - as a collection perhaps - depending on the particular details of your application, to make it easier to use, and my code easier to read.
0
 
Rad1Author Commented:
I need to populate the Array in one event and use it in another event.

Sorry, I should have mentioned this.

I thought by using a global Array I can do it.  Can I ?
0
 
Manoj PatilSr. Software EngineerCommented:
Is this means, that if the value of an array is changed in first event, and again after some time if second event occur, then you need that array without change in value, ON SAME PAGE ?

If yes then a better way is to store that array in Session.
1. First declare a global array variable of required type. (Don't initialized values)
2. Change value of an array in first event.
3. Access same array in second event, Because it's an global variable.
0
 
Todd GerbertIT ConsultantCommented:
In my opinion it's poor programming to use global variables, I avoid them at all costs. If you can give me a better idea of what your page does, and how it'll use the array, I can give you an example as soon as I get in front of a computer.
0
 
Rad1Author Commented:
One more thing I found is that after I created a global Array, I got it populated but when I wanted to use it I found it empty.

So the idea of Global Array will not work :(

What is the best way to save Row indexes numbers such as (2,5,7,9) and retrieve them when needed?

Do I have to save them in a Database or create a temp file to save them in or etc.......
0
 
Todd GerbertIT ConsultantCommented:
Here is one of many possible approaches; but, if you really want to get anything useful out of us then you need to give us more details on your actual project.

In Global.asax, the "Session_Start" event the array is created and populated. The array is then stored in the Session with the name "rowIndexes". The Session is kinda a per-user thing, and is available from page-to-page (to over-simplify).

Then, in Default.aspx, the Page_Load checks that the Session contains a variable named "rowIndexes", and that it is in fact an int[] array.  Then we retrieve that variable, and type-cast it to a variable declared as "int[]" (since all variables in the Session are stored as the generic "object", casting is necessary).

Then the silly example just loops through the array, appends each number to a StringBuilder, and assigns the StringBuilder's contents to a Label control on the WebForm.

The Global.asax:
using System;

namespace WebApplication1
{
	public class Global : System.Web.HttpApplication
	{
		protected void Session_Start(object sender, EventArgs e)
		{
			int[] rows = { 2, 5, 7, 9 };

			Session["rowIndexes"] = rows;
		}
	}
}

Open in new window


The WebForm's HTML:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="rowIndexesLabel" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

Open in new window


The WebForms C# Code-Behind:
using System;
using System.Text;

namespace WebApplication1
{
	public partial class Default : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			// Does the Session variable exist?
			if (Session["rowIndexes"] != null)
			{
				// Is that variable actually an int[] array?
				if (Session["rowIndexes"].GetType() == typeof(int[]))
				{
					// Now we know it exists, and is an int array, we
					// can safely cast it to an int[] variable
					int[] rows = (int[])Session["rowIndexes"];

					StringBuilder rowsText = new StringBuilder();

					for (int i = 0; i < rows.Length; i++)
						rowsText.AppendFormat("{0}<br />{1}", rows[i], Environment.NewLine);

					rowIndexesLabel.Text = rowsText.ToString();
				}
			}
		}
	}
}

Open in new window

0
 
Rad1Author Commented:
This is great example tgerbert and thank you!!!!

I wander if the session array will keep it's content for use later on???
I will be populating the array by what the user choose Rows from the Gridview checkBoxes.

If the user choose row 3, 4, 7 , 8.  I want to store these numbers in the array and use them to only display these rows in the Gridview. Also, when the user click on sort, the Gridview should only sort these rows only.
0
 
Todd GerbertIT ConsultantCommented:
The Session contents will be valid until the session ends; when it times out, Session.End() is called,  the web application stops, server is shutdown, etc.

if you want to persist data longer, you can either use a database or cookiesn
0
 
Rad1Author Commented:
Excellent!!!!

Thank you for a great support ALL!!!

Rad1
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now