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);
}
}
}

New Topic/Question
Reply



MultiQuote






|