This is probably quite obvious but i am new to .net

if i create a form and then have a sumbit button
that is something like

public void SubmitButton_Click(object sender, EventArgs e)
        {
            String name;
            Int ref_no;
           
            ref_no = this.REF.Text;
            name = "this.NAME.Text;

            return name, ref_no;
        }

Then that doesn't work cos it has the wrong return type, how can i return data retrieved from the form in different data types?

Thanks

Recommended Answers

All 7 Replies

well first of all, you can return only one variable from any function. So 'return name, ref_no;' is wrong.

I dont think you can change the return type of an event handler. An alternative can be to create global variables, and setting the values of those variables when the button is clicked. And then accessing those variables from wherever you want after the click.

regards

put your variable declarations for string and integer to top of your class right after the class declaration. then when you assign them values, you dont need to return anything from the function to access them. your problem is you declare your variables within the scope of the function.

You could also use a "form entry point" to return a value.

Caller:

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 daniweb
{
  public struct SearchValues
  {
    public string ref_no { get; set; }
    public string name { get; set; }
    public bool HasData { get; set; }
  }
  public partial class frmSearchScreen : Form
  {
    public frmSearchScreen()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      SearchValues val;
      using (frmSearchScreenEdit frm = new frmSearchScreenEdit())
      {
        val = frm.Edit();
      }
      MessageBox.Show(val.name);
    }
  }
}

Calle:

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 daniweb
{
  public partial class frmSearchScreenEdit : Form
  {
    private SearchValues _result = new SearchValues();

    public frmSearchScreenEdit()
    {
      InitializeComponent();
    }
    public SearchValues Edit()
    {
      this.ShowDialog();
      return _result;
    }

    private void button1_Click(object sender, EventArgs e)
    {
      _result.name = "abc";
      _result.ref_no = "123";
      _result.HasData = true;
      this.Close();
    }
  }
}

Ok, thanks, the global variables was what i needed.

I'm glad you got it working

Please mark this thread as solved if you have found the answer to your original question and good luck!

As you see sknake, he refers to my post not your overwhelming long coding :) and notice that he uses the term that i used in my post "Global variable". Kidding :) , you are better coder than me i know that.

akkkk, please mark this thread as solved.

This isn't the first.. second.. or third time my overly long code has complicated someone and not answered their question and one of you guys have helped them out more :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.