I don't understand why I can't access a function from my Form instance from another class in the same namespace
-
I am new to c# and oop. I seem to be missing something here. I don't understand why I can't access a function from my Form instance (Form1) from another class in the same namespace. Why does it not recognise the Form1 instance? Here is my code - the problem is when trying to access Form1.SetFormData(); in the NumberProcessing class. Could you please insert the correct code where necessary to make it work so I can fully understand what I need to do. namespace RouletteV1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btn1_Click(object sender, EventArgs e) { NumberAttributes.Load(1); } public void SetFormData() { //here is where I will set the textbox properties textBox1.Text = "Hello"; } } } -------------- namespace RouletteV1 { public static class NumberAttributes { public void Load(short Number) { // load the number data into global properties here NumberProcessing.UpdateNumArray(); } } --------------- namespace RouletteV1 { public static class NumberProcessing { public void UpdateNumArray() { // Here I want to call the SetFormData()function in Form1 but it won't recognise it - why??? Form1.SetFormData(); } } } Thanks in advance for your help Haz
-
I am new to c# and oop. I seem to be missing something here. I don't understand why I can't access a function from my Form instance (Form1) from another class in the same namespace. Why does it not recognise the Form1 instance? Here is my code - the problem is when trying to access Form1.SetFormData(); in the NumberProcessing class. Could you please insert the correct code where necessary to make it work so I can fully understand what I need to do. namespace RouletteV1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btn1_Click(object sender, EventArgs e) { NumberAttributes.Load(1); } public void SetFormData() { //here is where I will set the textbox properties textBox1.Text = "Hello"; } } } -------------- namespace RouletteV1 { public static class NumberAttributes { public void Load(short Number) { // load the number data into global properties here NumberProcessing.UpdateNumArray(); } } --------------- namespace RouletteV1 { public static class NumberProcessing { public void UpdateNumArray() { // Here I want to call the SetFormData()function in Form1 but it won't recognise it - why??? Form1.SetFormData(); } } } Thanks in advance for your help Haz
-
Becouse you are trying to access a non static class from a static class... it's not possible in C#. I don't know how can I help you now, maybe someone else can.
-
The method
SetFormData()
can only be performed on an instance of aForm1
object. One way to achieve this is to passUpdateNumArray()
a reference to aForm1
object, as in:public void UpdateNumArray
(Form1 theForm)
{
theForm.SetFormData();
}/ravi
My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com
-
The method
SetFormData()
can only be performed on an instance of aForm1
object. One way to achieve this is to passUpdateNumArray()
a reference to aForm1
object, as in:public void UpdateNumArray
(Form1 theForm)
{
theForm.SetFormData();
}/ravi
My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com
-
Becouse you are trying to access a non static class from a static class... it's not possible in C#. I don't know how can I help you now, maybe someone else can.
Wrong. The problem is he's trying to access the non-static method on the Form1 class instead of an instance of Form1. The class trying to access an instance of Form1 can only get a reference to the instance of Form1 if it creates the form instance, or the form gives the using class an instance of itself. The following will work, but it's not a very wise design decision to allow processing classes to has instances of your Form. Better to have you working classes just do work with data and return values that your UI can use.
namespace RouletteV1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}private void btn1\_Click(object sender, EventArgs e) { NumberAttributes.Load(this, 1); } public void SetFormData() { textBox1.Text = "Hello"; } } public static class NumberAttributes { public void Load(Form1 form, short Number) { NumberProcessing.UpdateNumArray(form); } } public static class NumberProcessing { public void UpdateNumArray(Form1 form) { form.SetFormData(); } }
}
Try code model generation tools at BoneSoft.com.
-- modified at 11:46 Monday 24th July, 2006 Whoever voted this message down can kiss my hairy white ass, unless they'd like to actually post something to refute it.
-
Thanks for that, everything has just clicked!!!! I understand where I was going wrong!! Fundamentals of oop :)
Haz
-
Wrong. The problem is he's trying to access the non-static method on the Form1 class instead of an instance of Form1. The class trying to access an instance of Form1 can only get a reference to the instance of Form1 if it creates the form instance, or the form gives the using class an instance of itself. The following will work, but it's not a very wise design decision to allow processing classes to has instances of your Form. Better to have you working classes just do work with data and return values that your UI can use.
namespace RouletteV1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}private void btn1\_Click(object sender, EventArgs e) { NumberAttributes.Load(this, 1); } public void SetFormData() { textBox1.Text = "Hello"; } } public static class NumberAttributes { public void Load(Form1 form, short Number) { NumberProcessing.UpdateNumArray(form); } } public static class NumberProcessing { public void UpdateNumArray(Form1 form) { form.SetFormData(); } }
}
Try code model generation tools at BoneSoft.com.
-- modified at 11:46 Monday 24th July, 2006 Whoever voted this message down can kiss my hairy white ass, unless they'd like to actually post something to refute it.
Thanks a lot for that, you were exactly right! I have slightly altered my code to cut down on the form reference passing. However when I try compling it it throws up an error - Compiler Error CS0120 - Error Message "An object reference is required for the nonstatic field, method, or property 'member' " and refers to line
NumberProcessing.UpdateNumArray(this);
I am guessing there is a problem with the "this" value? Any suggestions what is wrong??? Code is below.namespace RouletteV1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btn1_Click(object sender, EventArgs e) { NumberAttributes.Load(1); NumberProcessing.UpdateNumArray(this); } public void SetFormData() { tboxNumArray.Text = "Hello"; } public class NumberProcessing { public void UpdateNumArray(Form1 form) { form.SetFormData(); } } }
Thanks in advance for your help :)Haz
-
Thanks a lot for that, you were exactly right! I have slightly altered my code to cut down on the form reference passing. However when I try compling it it throws up an error - Compiler Error CS0120 - Error Message "An object reference is required for the nonstatic field, method, or property 'member' " and refers to line
NumberProcessing.UpdateNumArray(this);
I am guessing there is a problem with the "this" value? Any suggestions what is wrong??? Code is below.namespace RouletteV1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btn1_Click(object sender, EventArgs e) { NumberAttributes.Load(1); NumberProcessing.UpdateNumArray(this); } public void SetFormData() { tboxNumArray.Text = "Hello"; } public class NumberProcessing { public void UpdateNumArray(Form1 form) { form.SetFormData(); } } }
Thanks in advance for your help :)Haz