Hi All,
I have a issue I have 2 forms (form B and form C) that launches off 1 form (form A). How do I access information that was created in form B from form C?
Your help would be much appreciated.
Thank you!
2 Forms with 1 Launch form.
Started by Cody Maverick, Jun 13 2010 03:02 AM
7 replies to this topic
#1
Posted 13 June 2010 - 03:02 AM
|
|
|
#2
Posted 13 June 2010 - 05:15 AM
You can either make a public variable in C, pass it to A and then to B, or you can create a static class where you can add the values from form C and access them in form B.
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics
#3
Posted 13 June 2010 - 05:54 AM
Can you show me what you mean with some examples please?
Thank you
Thank you
#4
Posted 13 June 2010 - 06:52 AM
here is what he means ,
In form c do the code below
In form c do the code below
formb objFormb = new formb();then access the textbox,dropdownlist values of form b like below
lblMessage.Text = objFormb.txtName.Text;try it u will know ..Just remember that in C# everything is a Object.So the actual reason behind this is ,create a instance of the formB and then access the properties of the formB[textbox,dropdownlist..etc] using the form object
#5
Posted 13 June 2010 - 07:35 AM
Ok Cody, short C# lesson.
Classes usually must be constructed before they are used:
Classes usually must be constructed before they are used:
class MyClass {
//etc...
}
MyClass x = new MyClass(); //This is what I mean by the fact that they must be constructed
Yet, static classes can be used without declaration:
static class MyClass {
public static string MyString;
}
MyClass.MyString = "Hello People!"; //Can be used without being constructed
Might not be a proof of good coding, but it is really easy to use them when transfering data from a form to another. Just create a new static variable to hold the data from form C, then get the value from that variable in form B. If you have any other questions fell free to ask, classes can be a really big pain to understand.
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics
#6
Posted 15 June 2010 - 08:29 AM
like he mentioned having direct access to a private instance variable is very bad practice and all programmers should run from this like the bubonic plague :D (even a static class) It's better to have just a regular instance variable and access the variable with a getter method. For example:
This allows you to have indirect access to a variable through a simple method. So you could change the status by instantiating the Object and calling the method.
Static classes mean exactly that..they are static. You can change the value for a static variable and it is the same throughout every instance of the object. However, there is nothing wrong with that if you use it in a static class like the above method. There is just others ways to do it that may be safer.
public class MyClass
{
private string MyString;
.
.
.
public string myString
{
get { return this.MyString; }
set { this.MyString = value; }
}
}
This allows you to have indirect access to a variable through a simple method. So you could change the status by instantiating the Object and calling the method.
MyClass mc = new MyClass(); mc.myString = "Hi there!"
Static classes mean exactly that..they are static. You can change the value for a static variable and it is the same throughout every instance of the object. However, there is nothing wrong with that if you use it in a static class like the above method. There is just others ways to do it that may be safer.
#7
Posted 15 June 2010 - 09:14 AM
While(!EOF) said:
like he mentioned having direct access to a private instance variable is very bad practice and all programmers should run from this like the bubonic plague :D (even a static class) It's better to have just a regular instance variable and access the variable with a getter method. For example:
This allows you to have indirect access to a variable through a simple method. So you could change the status by instantiating the Object and calling the method.
Static classes mean exactly that..they are static. You can change the value for a static variable and it is the same throughout every instance of the object. However, there is nothing wrong with that if you use it in a static class like the above method. There is just others ways to do it that may be safer.
public class MyClass
{
private string MyString;
.
.
.
public string myString
{
get { return this.MyString; }
set { this.MyString = value; }
}
}
This allows you to have indirect access to a variable through a simple method. So you could change the status by instantiating the Object and calling the method.
MyClass mc = new MyClass(); mc.myString = "Hi there!"
Static classes mean exactly that..they are static. You can change the value for a static variable and it is the same throughout every instance of the object. However, there is nothing wrong with that if you use it in a static class like the above method. There is just others ways to do it that may be safer.
May I know why you used a property instead of a variable?
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics
#8
Posted 15 June 2010 - 10:06 AM
This is actually preference to use properties instead of methods...but i never try to have public variables because code can become harder to clean up later. Suppose, you didn't want them to be able to set the value in the class later on down the road..then this allows you to easily get rid of the set ability which gives only read access to the variable. Without this approach (which is a general "OO" programming practice) Then you could be spending years cleaning it up later. Just look at DOS well ok...Windows Command Prompt


Sign In
Create Account

Back to top









