3 Replies - 563 Views - Last Post: 22 August 2012 - 06:22 AM Rate Topic: -----

#1 Fish-E  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-August 12

Passing data between forms, not receiving expected result

Posted 22 August 2012 - 03:02 AM

Hello

I'll apologise in advance that I am quite new to C#, basically I'm trying to create a basic program that passes a string between two forms and a class. I thought I had it working, by using some code I found on the internet but when I run the program instead of giving me the input name (ie "John") it returns SimplePassing.YourName (SimplePassing is the name of the Visual C# 2010 express project and YourName is the name of the class in class1).

Here is the code that I've done;

Class1.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimplePassing
{
    public class YourName
    {
        public string NameMessage(string givenName) 
            
            //Creates a string called NameMessage that 
            //returns Your Name is and the value of givenName
        {
            return "Your name is" + givenName;
        }

        private string TheMessage; 
        
        //This is just an empty string called TheMessage. 
        //This will be used in the MyProperty code, it will be set equal to the value of RealName.MyProperty

        public string MyProperty {
            get { return TheMessage; } //gets the value of TheMessage (at the start this will be blank)
            set { TheMessage = value; } //sets TheMessage to be equal to the value of RealName.MyProperty
        }
    }
}



Form1.cs

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 SimplePassing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            YourName RealName = new YourName();
            string returnedMessage; //This is a string which will store the contents of RealName.MyProperty

            RealName.MyProperty = txtInputName.Text; 
            //Sets the name of RealName.MyProperty to 
            //be equal to the contents of the text box
            returnedMessage = RealName.MyProperty; //Sets returnedMessage to be equal to RealName.MyProperty

            Form2 frm2 = new Form2();
            frm2.obj2 = RealName;
            frm2.Show();
            this.Hide();
        }
    }
}



Form2.cs

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 SimplePassing
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public YourName obj2;

        private void button1_Click(object sender, EventArgs e)
        {
            txtYourName.Text = Convert.ToString(obj2);
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Passing data between forms, not receiving expected result

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,519
  • Joined: 23-August 08

Re: Passing data between forms, not receiving expected result

Posted 22 August 2012 - 03:32 AM

Where you're just starting out, you should learn the right way to do it from the start.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: Passing data between forms, not receiving expected result

Posted 22 August 2012 - 05:55 AM

How do I?

[*]Q: ...get Form 'A' to make a change or talk to Form 'B'
Spoiler

Was This Post Helpful? 0
  • +
  • -

#4 Fish-E  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-August 12

Re: Passing data between forms, not receiving expected result

Posted 22 August 2012 - 06:22 AM

I apologise for not using the search function, I was in a hurry when I posted earlier.

Thank you for the links. I'll have a look at them and if I have any further problems I'll post here.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1