How to access array from another method in this example? I'm new in c# and i really appreciate if you will help me. Thanks in advance!
private void button1_Click(object sender, EventArgs e) {
int[] array1 = new int[5];
for (int i = 0; i < 5; i++) {
array1[i] = i;
}
}
private void button2_Click(object sender, EventArgs e) {
int[] array2 = new int[5];
for (int i = 0; i < 5; i++) {
array2[i] = array1[i];
}
}
As Jon Skeet mentioned in his comment, local variables are by definition local to the method which defines them. If you want something to be visible globally, you need to make it an instance variable, or return it from the method.
Assuming that only array1 needs to be visible, it suffices to declare that one outside.
// Declare the array globally.
int[] array1 = new int[5];
private void button1_Click(object sender, EventArgs e)
{
// Initially the array inside this method.
for(int i=0;i<5;i++)
array1[i]=i;
}
private void button2_Click(object sender, EventArgs e)
{
int[] array2 = new int[5];
// Copy from the global array
for(int i=0;i<5;i++)
{
array2[i]=array1[1];
}
}
Declare both arrays outside to enable access.
int[] array1, array2;
private void button1_Click(object sender, EventArgs e) {
array1 = new int[5];
for (int i = 0; i < 5; i++) {
array1[i] = i;
}
}
private void button2_Click(object sender, EventArgs e) {
array2 = new int[5];
for (int i = 0; i < 5; i++) {
array2[i] = array1[i];
}
}
Related
How can I draw a stock chart with List<> and DataPointCollection.DataBindY at once?
I know how to add stock data on a chart but I don't want looping and repeating drawing by Add because actual list is much longer.
To draw a chart at once I tried DataPointCollection.DataBindY but it doesn't work. It needs IEnumerable[] but I don't know how to change List<> to IEnumerable[].
public partial class Form1 : Form
{
double[] Candle;
List<double[]> CandleList = new List<double[]>();
public Form1()
{
InitializeComponent();
Candle = new double[] { 5, 0, 3, 1 };
CandleList.Add(Candle);
Candle = new double[] { 10, 5, 7, 9 };
CandleList.Add(Candle);
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < CandleList.Count; i++)
{
chart1.Series[0].Points.Add(CandleList[i]); // works well
}
}
private void button2_Click(object sender, EventArgs e)
{
chart1.Series[0].Points.DataBindY(CandleList); // System.ArgumentException
}
}
Because a list is an iEnumerable, iEnumerable[] means an array of lists.
private void button1_Click(object sender, EventArgs e)
{
List<double>[] Lists = new List<double>[4];
for (int i = 0; i < 4; i++)
{
Lists[i] = new List<double>();
}
Lists[0].Add(5); // high
Lists[0].Add(10);
Lists[1].Add(0); // low
Lists[1].Add(5);
Lists[2].Add(3); // close
Lists[2].Add(8);
Lists[3].Add(2); // open
Lists[3].Add(7);
chart1.Series[0].Points.DataBindY(Lists);
}
private void button5_Click(object sender, EventArgs e)
{
int gTotal = 1;
for (int gCount = 0; gCount < listBox3.Items.Count - 1; gCount++)
gTotal += (listBox3.Items.Add(gCount));
label1.Text = gTotal.ToString();
}
Hey guys i'm not entirely sure of how to use Listboxes, but my question is how can I display the numbers that populate in my list box 3 to go into the label?
private void button5_Click(object sender, EventArgs e)
{
int gTotal = 1;
var collection = listBox3.Items.Cast<String>().ToList();
for (int gCount = 0; gCount < collection.Count - 1; gCount++)
{
int item;
if (int.TryParse(collection[gCount], out item)
{
gTotal += item;
}
}
label1.Text = gTotal.ToString();
}
Cast the items to a List then make sure that items are ints, if so add them up.
Parse your items.
private void button5_Click(object sender, EventArgs e)
{
int gTotal = 1;
for (int gCount = 0; gCount < listBox3.Items.Count; gCount++)
gTotal += int.Parse(listBox3.Items[gCount].ToString());
// assuming all items in the listbox is an int.
label1.Text = gTotal.ToString();
}
What trying to do
I'm trying to add textbox and numericUpandDown values to an array, but it doesn't seem to be working.
Carro []carros = new Carro[1];
private Carro carro;
public Form1()
{
..
}
private void Form1_Load(object sender, EventArgs e)
{
..
}
private void AdicionarCarro()
{
this.carro = new Carro(textboxCor.Text, textboxMarca.Text, textboxModelo.Text,
(int.Parse(numUpDownCilindrada.Text)), (int.Parse(numUpDownVelocidade.Text)));
}
private Carro[] AdicionarArray(Carro carro, Carro[] array)
{
AdicionarCarro();
int novoTamanho = array.Length + 1;
Carro[] carros = new Carro[novoTamanho];
for (int i = 0; i < array.Length; i++)
{
carros[i] = array[i];
}
carros[novoTamanho] = carro;
return carros;
}
private void buttonGravar_Click(object sender, EventArgs e)
{
AdicionarArray(carro, carros);
}
When I type the values and click on the "buttonGravar", it gives me this
Error:
I'd be much delighted to get some tips/help on it.
Using System.Collection.Generic.List<T> would be much simpler, since it doesn't have a fixed size:
List<Carro> carros = new List<Carro>();
carros.AddRange(array);
carros.Add(carro);
return carros;
Better way:
private List<Carro> Carros;
public Form1()
{
Carros = new List<Carro>();
..
}
private void Form1_Load(object sender, EventArgs e)
{
..
}
private void AdicionarCarro()
{
var carro = new Carro(textboxCor.Text, textboxMarca.Text, textboxModelo.Text,
(int.Parse(numUpDownCilindrada.Text)), (int.Parse(numUpDownVelocidade.Text)));
Carros.Add(carro);
}
private void buttonGravar_Click(object sender, EventArgs e)
{
AdicionarCarro();
}
To help you understand your code:
carros[novoTamanho] = carro;
should be
carros[novoTamanho - 2] = carro;
Reason:
Array index starts from 0. novoTamanh represents new length (starting at 1, not 0 unlike index), which is outside array.
It's an index out of range exception because your array Carro is of size tmanho:
Carro[] carros = new Carro[novoTamanho];
and carros can contain exactly "novoTamanho" items indexed from "0" to "novoTamanho -1"
You can simply solve this by defining:
int novoTamanho = array.Length + 2;
Or if you do not want to manage indexes, use Lists:
List<Carro> listCarro = new List<Carro>;
listCarro.AddRAnge(array);
listCarro.Add(carro);
return listCarro.ToArray();
I want to declare Page scope array and fill from end user
protected void Button2_Click(object sender, EventArgs e)
{
String Knowledge_iloes = TextBox5.Text;
Knowlege.Add(Knowledge_iloes);
for (int i = 0; i < Knowlege.Count; i++)
{
Response.Write(Knowlege[i]);
}
}
![ http://i.stack.imgur.com/H3HlU.jpg][1]
I am thinking of finding an easy way to put numbers into an array using a for loop. I have made the easy design with text showing how the name system works!
My code is like this:
double[,] kast = new double[3, 8];
string[] navn = new string[8];
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSorter_Click(object sender, EventArgs e)
{
for(int i = 1; i > 4; i++)
{
for(int x = 1; i > 9; i++)
{
kast[i, x] = Convert.ToDouble(txtKast + i + x + .Text);
}
}
}
I don't know how I will make it work with the Convert.ToDouble part since "kast" is a double array.