Hello,

Hey guys, love the forums here, very nice.

I am having a bit of coding problem with an assignment from college. (I read the announement no worries I am not looking for you to do it for me ^_^)

Anyway, I have a form, which I am not done coding yet, that needs to send all the data that is entered on the form to an object. This happens when the user clicks thr "add" button. I need to know, how can I get my object to recognize m form and accept data from it?

Here is what I am thinking migh be the code to do so (in the object)

public int STR
        {
            get
            {
                return str;
            }
            set
            {
                 str = strBox.Text;
            }
           
        }

Am I close to the right idea? Also, could someone post a possible syntax for this?

I can post my existing code, but I do not think it will bw neccessary.

Recommended Answers

All 7 Replies

Well. A form is an object like any other object in c#, and you have many options. However, lets assume you're going to make a phonelist holder of your friends. On the main form you want a list maybe of names, so you can quickly find the person you're after. The second form holds more details, such as address, phone number, date of birth, stuff like that.

If you make a class to hold all that data, you can pass it as a single contained lump much like handing a cup of coffee over, you dont ask someone if they want one, and then hand over 1 cup a handful of hot water, ground stuff, maybe a dribble of milk and some sugar do you?

Then what you can do is setup a function to call in your second "details" form, where you send it the class as parameter, which can then populate the details form, and everyones happy.

That makes sense, but the assignment calls for a very specific action, and my instructor is, how we say....fickle.

AT any rate, is there a way to pass al this information like the cup of coffee from just one form to one class? If so, would it be possible to show some comparable code snippets? Thank you for all the help!

Well..

void DataInit(myclass data)
{
if (data!=null)
{
  txName.Text=Data.Name;
  txtAge.Text=Data.Age;
  txtAddress.Text=Data.Address;
}
else
{
 txName.Clear();
txAge.Clear();
txAddress.Clear();
}

}

There are a ton of alternatives.

For example, to be honest, you could make the form part of the data class, as its going to display it, you could make the data a property on the form and when it changes it knows to update the form..

A fickle instructor is a *good* thing.

Now, the way Ive suggested is semi OK, but obviously its not going to return the data to your form. If you want that you could of course pass by reference.

public int STR
        {
            get
            {
                return str;
            }
            set
            {
                 str = strBox.Text;
            }
           
        }

This looks like a property to me, but your syntax is not correct
do it this way:

private string str;

        public string STR
        {
            get
            {
                return str;
            }
            set
            {
                 str = value;            }
           
        }

To get the string : Myclass.str
To set the string : Myclass.str = strBox.Text;

thanks for the reference ddanbe :-)
just so happened to coincide with me posting

Lots of information to go over here!

I think my issue is I am over thinking this. The problem I had was I was trying to get my object to gather information from my form, when my form should have been sending the data to the object. I think I might have it here, thanks to your guys help! Thank you so very much!

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.