View Full Version : سوال: تعریف آرایه با طول متغیر
InfiniteLoop
سه شنبه 27 اردیبهشت 1390, 21:23 عصر
چگونه میشه یک آرایه با طول متغیر تو برنامه تعریف کرد که در تمام برنامه بشه ازش استفاده کرد؟
اینو میدونم:
a = Convert.ToInt32(textbox1.text);
int[] arr = new int[a];
i
ولی نمیشه ازش تو قسمت های دیگه برنامه استفاده کرد. مثلا میخوام بهش طول بدم و بعد بیام مقادیر عناصر رو وارد کنم و مرتبش کنم یعنی این آرایه یا آرایه ها رو به متدی بفرستم.
NIMA_1981
چهارشنبه 28 اردیبهشت 1390, 00:35 صبح
توی قسمت public متغییر رو تعریف کن بعد در هر قسمت که حواستی طول آرایه رو دوباره new کن یا کلا از list استفاده کن
tooraj_azizi_1035
چهارشنبه 28 اردیبهشت 1390, 12:30 عصر
سلام،
شما باید از کلاس ArrayList که به این منظور طراحی شده استفاده کنید:
[C#]
using System;
using System.Collections;
public class SamplesArrayList {
public static void Main() {
// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add("Hello");
myAL.Add("World");
myAL.Add("!");
// Displays the properties and values of the ArrayList.
Console.WriteLine( "myAL" );
Console.WriteLine( "\tCount: {0}", myAL.Count );
Console.WriteLine( "\tCapacity: {0}", myAL.Capacity );
Console.Write( "\tValues:" );
PrintValues( myAL );
}
public static void PrintValues( IEnumerable myList ) {
System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.Write( "\t{0}", myEnumerator.Current );
Console.WriteLine();
}
}
/*
This code produces the following output.
myAL
Count: 3
Capacity: 16
Values: Hello World !
*/
haghft
چهارشنبه 28 اردیبهشت 1390, 15:26 عصر
int[] array = new int[textBox1.Text.Length];
vBulletin® v4.2.5, Copyright ©2000-1403, Jelsoft Enterprises Ltd.