# how to create an array and send it to another form like a variable

Page 1 of 1

## 7 Replies - 1060 Views - Last Post: 13 March 2011 - 06:21 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=221678&amp;s=6696c484e99fed50ce8f1c0578e2dcca&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 h131200

• New D.I.C Head

Reputation: 0
• Posts: 12
• Joined: 12-March 11

# how to create an array and send it to another form like a variable

Posted 12 March 2011 - 04:35 AM

Hey .
I have projekt work " Advanced calculator " that the user can calculate derivative .
I've been thinking about an array like this .
2 forms
the first one contains textbox and button .
the other on label .
the user have to specify the number of how many products in the function by typing in the textbox in the first form . then the button send this number to the other form .
now when the secund form recesivs this numer , it will creat an array of length [ number from the fist form ]

and then run foreach kod .
f(x) = X^n . will be n*X^n-1 .
så the number will determine how many X^n . so foreach( ( X^n ) in array ) derivative is n*X^n-1 .

I do not know if im thinking right.

when i use this in console application
```int  [] r = new int [10] ;
for ( i= 0 ; i < r.Length; i=i+1)
{
console.writeline (r[i]) ;
}
```

i get output 1, 2, 3 ..... 9

but when i use it in Form applicatoin i got the sum of the number .

This post has been edited by JackOfAllTrades: 12 March 2011 - 05:42 AM
Reason for edit:: Added code tags

Is This A Good Question/Topic? 0

## Replies To: how to create an array and send it to another form like a variable

### #2 Aphex19

• Born again Pastafarian.

Reputation: 618
• Posts: 1,873
• Joined: 02-August 09

## Re: how to create an array and send it to another form like a variable

Posted 12 March 2011 - 04:56 AM

That looks like Java to me, this is the C/C++ forum.

### #3 ryuurei

• New D.I.C Head

Reputation: 1
• Posts: 5
• Joined: 09-March 11

## Re: how to create an array and send it to another form like a variable

Posted 12 March 2011 - 05:07 AM

Aphex19, on 12 March 2011 - 04:56 AM, said:

That looks like Java to me, this is the C/C++ forum.

Are you sure? I'm not entirely familiar with either but that looks like C# to me. I recall seeing the Console object referenced in C# code before.

### #4 h131200

• New D.I.C Head

Reputation: 0
• Posts: 12
• Joined: 12-March 11

## Re: how to create an array and send it to another form like a variable

Posted 12 March 2011 - 05:25 AM

:s yes , this is C#

:s yes , this is C#

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: how to create an array and send it to another form like a variable

Posted 12 March 2011 - 05:35 AM

Moved to C#

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: how to create an array and send it to another form like a variable

Posted 12 March 2011 - 05:43 AM

I think you're going to have to show more code here. Post your code IN CODE TAGS as shown below:

### #7 h131200

• New D.I.C Head

Reputation: 0
• Posts: 12
• Joined: 12-March 11

## Re: how to create an array and send it to another form like a variable

Posted 13 March 2011 - 05:39 AM

JackOfAllTrades, on 12 March 2011 - 05:43 AM, said:

I think you're going to have to show more code here. Post your code IN CODE TAGS as shown below:

```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace test_send_data
{

// imoprt data from another form

public partial class MySecondForm : Form
{
public int Myint;
public MySecondForm()
{
InitializeComponent();
}

public MySecondForm(int MyVal)
{
InitializeComponent();
Myint = MyVal;
}
private void button1_Click(object sender, EventArgs e)
{
lblshowresult.Text = Myint.ToString();

}

private void MySecondForm_Load(object sender, EventArgs e)
{
string s = "";

for (int i = 0; i < (Myint); i++)
{
s += "kX^n+";

lblshowresult.Text = "f(x) =" + s;

}
}
}

// derivative when butten click

private void button1_Click_1(object sender, EventArgs e)
{
// creat array of length myint which i import from rhe first form
double[] k = new double[Myint+1];
double[] n = new double[Myint+1];
//  array items
n[0] = Convert.ToDouble(txtbxn1.Text);
n[1] = Convert.ToDouble(txtbxn2.Text);
k[0] = Convert.ToDouble(txtbxk1.Text);
k[1] = Convert.ToDouble(txtbxk2.Text);

double f ;
double c;

string s = "";
string s2 = "";

foreach (double i in n )
{
f = (i - 1);

foreach (double r in k)
{
c = (r * i);
s += Convert.ToString(c) + "X^" + Convert.ToString(f) + "+";
}
txtbxresul.Text = "Derivative is  :\r\n f(x) =" + s;
}

}

}

```

my problem now is
http://img718.images...capturealh.jpg/

the derivative should be f'(x) = 12X^3 + 12X^3 .
why does the program repeat the items many times ???

This post has been edited by h131200: 13 March 2011 - 05:41 AM

### #8 Momerath

• D.I.C Lover

Reputation: 1020
• Posts: 2,463
• Joined: 04-October 09

## Re: how to create an array and send it to another form like a variable

Posted 13 March 2011 - 06:21 AM

In line 53 and 54 you declare arrays of double that are Myint+1 in size (and your comment above them is wrong). These arrays are initialized to the default value of a double, which is zero. You then put values into the first two array spots.

Line 68 you iterate through all the doubles in the array, even those that have default values. Same with line 73. This is where all the odd -1X stuff comes from.

As for the repeating, you have 4 values that you have split into 2 groups, and you match each group to the other group. That makes 2 times 2 = 4 values you are going to output. You need to rethink your logic here.

This post has been edited by Momerath: 13 March 2011 - 06:22 AM