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

Send data from main class to other class as funtion data?

First: I need to pass a string of data from the main class to a math class for processing.
Second: I need to get the data back to the main class.

You'll notice by the code below that the data needs to pass to the math.cs then run the ProcessData method and send the data back to the main form.

----------------------------------------------------------------------------------------------------------

[MAIN.CS]
[Attempting to pass data from the main.cs to the math.cs]

string UserAge;  // needs passed to math.cs


[Attempt to get the data back to the main class from the math class, Keep getting null returns..]
private void btnUserNumber_Click(object sender, EventArgs e)
        {
            Math math = new Math();
            math.ProcessData();
            listBox1.Items.Add(math.UserNumber);
        }

------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------

[MATH.CS]
 private string userNumber;
 
        public string UserNumber
        {
            get { return userNumber; }
            set { userNumber = value; }
        }

public void ProcessData()
        {
            string userPosition = 201;          
            math.UserNumber =   userPosition * (main.cs UserAge);                            
        }

---------------------------------------------------------
0
kvnsdr
Asked:
kvnsdr
  • 2
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
Just add a parameter to you method:

        public void ProcessData(string val)
        {
            string userPosition = 201;          
            math.UserNumber =   userPosition * (int)val;
        }

And call it like:

    Math math = new Math();
    math.ProcessData(this.UserAge);
0
 
vo1dCommented:
change your math.cs like this:

[MATH.CS]
      private int userNumber;
 
        public intUserNumber
        {
            get { return userNumber; }
            set { userNumber = value; }
        }

public int ProcessData(int val)
        {
            int userPosition = 201;          
            math.UserNumber =   userPosition * val;
        }

i do not recommend that you use strings for holding numbers.
if you need to store your values in a listview, use the ToString() method to convert the int to a string.
like :

private void btnUserNumber_Click(object sender, EventArgs e)
        {
            Math math = new Math();
            int newValue = math.ProcessData(3);
            listBox1.Items.Add(newValue.ToString());
        }
0
 
kvnsdrAuthor Commented:
Sorry, can't get either code to work.

Q. What does the (3) represent?

Q. Not all code paths return a value " int ProcessData" ?
0
 
vo1dCommented:
sorry for my incomplete code. the ProcessData method should be like this:
public int ProcessData(int val)
        {
            int userPosition = 201;          
            math.UserNumber =   userPosition * val;
            return (math.UserNumber);
        }

the 3 is an integer parameter, which is for your calculation.
it was only an example.
so what the method do is multiply your userposition 201 with the value you pass to this method.
another example is this:
public int Sum(int a, int b)
{
  return(a+b);
}

so if you call the method like this: Sum(2,3),
you would get 5 as return value.
the advantage of return values is, that you will get a result right after the method is executed.

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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