Most efficient way to solve this circular reference? - c#

New to C# here. So I got my first class (Form1) and second class (Class1) in different projects. I added Form1 to Class1's references because Form1 had data from its GUI that Class1 needed to make computations from its method. Problem is, I cant get the results from the method in Class1 to Form1 because I can't reference it because of circular reference.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label3_Click(object sender, EventArgs e)
{
}
public void button1_Click(object sender, EventArgs e)
{
// for getting data from Class1
// ClassLibrary1.Class1 c = new ClassLibrary1.Class1();
// label7.Text = c.GetDate(); }
private void button2_Click(object sender, EventArgs e)
{
}
}
public class Class1
{
private int daysz;
private int GetDate()
{
Activity3_Espiritu.Form1 f = new Activity3_Espiritu.Form1();
daysz = (f.lastDay - f.firstDay).Days;
return daysz;
}
}
What's a clean way to get around this? I've tried interfaces but I've absolutely no idea how to use it, even after looking online for solutions.

Class1 should never need a reference to your Form1, instead the code from Form1 should call the GetDate() method in Class1 and pass in the appropriate parameters for GetDate() to evaluate. When GetDate() returns the result you simply assign it to a variable or back into the user control that needs to show it (would that be Label7?).
public void button1_Click(object sender, EventArgs e)
{
var c = new Class1();
var yourResult = c.GetDate(lastDay, firstDay);
label7.Text = yourResult;
}
public int GetDate(DateTime lastDate, DateTime firstDate)
{
return (lastDate - firstDate).Days;
}

If you can change signature of GetDate method you can try this code:
public class Class1
{
private int daysz;
private int GetDate(__yourDatType__ lastDay, __yourDatType__ firstDay)
{
daysz = (lastDay - firstDay).Days;
return daysz;
}
}
now, in button1_Click write this:
ClassLibrary1.Class1 c = new ClassLibrary1.Class1();
label7.Text = c.GetDate(this.lastDay, this.firstDay);

Related

Pass Textbox Value From Windows Form To Class

I am learning C# and have run into an interesting issue to me. I have a class variable defined as public and I instantiate a new instance of my form in my class and access the value of the public variable it is always null.
To further explain my issue - this syntax prints the appropriate value
System.Diagnostics.Debug.WriteLine(tboxvalue.ToString());
However, this syntax is always outputting a 0
System.Diagnostics.Debug.WriteLine(f1.tboxvalue.ToString());
How do I need to alter my syntax so that the correct value is passed to the class Functions?
public partial class Form1 : Form
{
public double tboxvalue;
private string exportdata;
public Form1()
{
InitializeComponent();
}
private void btnClicker_Click(object sender, EventArgs e)
{
Functions.EE();
}
private void txtData_CheckedChanged(object sender, EventArgs e)
{
bool #checked = ((CheckBox)sender).Checked;
if (#checked.ToString() == "True")
{
exportdata = "Yes";
tboxvalue = Convert.ToDouble(this.txtData.Text);
System.Diagnostics.Debug.WriteLine(tboxvalue.ToString());
}
else
exportdata = "No";
}
}
class Functions
{
public static void EE()
{
Form1 f1 = new Form1();
System.Diagnostics.Debug.WriteLine(f1.tboxvalue.ToString());
}
}
To access properties of the form, you need to change two Things. First you have to pass the form to the 'EE' method, then you can access the form's properties. Second, don't create a new form in 'EE' method.
public partial class Form1 : Form
{
public double tboxvalue;
private string exportdata;
public Form1()
{
InitializeComponent();
}
private void btnClicker_Click(object sender, EventArgs e)
{
Functions.EE(this);
}
private void txtData_CheckedChanged(object sender, EventArgs e)
{
bool #checked = ((CheckBox)sender).Checked;
if (#checked.ToString() == "True")
{
exportdata = "Yes";
tboxvalue = Convert.ToDouble(this.txtData.Text);
System.Diagnostics.Debug.WriteLine(tboxvalue.ToString());
}
else
exportdata = "No";
}
}
class Functions
{
public static void EE(Form1 f1)
{
System.Diagnostics.Debug.WriteLine(f1.tboxvalue.ToString());
}
}
If i understood your question i guess you are recreated Form1 with own textbox or labels when you click btnClicker button. You can reassign your form objects where you created it.
You might add static Form1 object and Setter routine to Functions class:
private static Form1 _form;
public static void SetForm(Form1 form)
{
_form = form;
}
and pass the form to the class in Form_Load event-click on the form twice:
private void Form1_Load(object sender, EventArgs e)
{
Functions.SetForm(this);
}
Then you can play with the form in Functions class using the object _form
good luck!

I can't get a member inside a private class to be accessible from another private class

{
public partial class form1 : Form
{
public form1()
{
InitializeComponent();
}
public Button ButtonName { get { return } }
public static int initFaggleCount;
private void button1_Click(object sender, EventArgs e)
{
int faggleCount = initFaggleCount++;
string finalCalc = faggleCount.ToString();
label1.Text = finalCalc;
/*
Console.WriteLine(faggleCount);
Console.ReadLine();*/
}
private void button2_Click(object sender, EventArgs e)
{
/*TextWriter tw = new StreamWriter("SavedFaggleCount.txt");
tw.WriteLine();
tw.Close();*/
Console.WriteLine(faggleCount);
Console.ReadLine();
}
}
}
I would like the integer faggleCount to be accessible from button2 so that I can successfully Console.WriteLine(fagleCount); from button2. I'm a noob and any help is appreciated. Thanks!
instead of declaring local variable inside method body you can declare instance variable so that all members of class can access it.
public partial class form1 : Form
{
int faggleCount; //declare instance variable.
public form1()
{
InitializeComponent();
}
public Button ButtonName { get { return } }
public static int initFaggleCount;
private void button1_Click(object sender, EventArgs e)
{
faggleCount = initFaggleCount++; //use instance variable
string finalCalc = faggleCount.ToString();
label1.Text = finalCalc;
/*
Console.WriteLine(faggleCount);
Console.ReadLine();*/
}
private void button2_Click(object sender, EventArgs e)
{
/*TextWriter tw = new StreamWriter("SavedFaggleCount.txt");
tw.WriteLine();
tw.Close();*/
Console.WriteLine(faggleCount); //use instance variable
Console.ReadLine();
}
}
In your code, faggleCount is local to the method i.e. only code in the method can access it. What you need to do is to move the variable to the class-level. like this:
public class form1 : Form {
int faggleCount;
//your other code here
}
As you can see, the variable is now in the class, not in the method. This way all the methods in the class can access it, and even a inner class can access it too!
This problem is very common among beginners. Understanding the scope of the variable is pretty hard. In short, A variable in a class can be accessed in the class, a variable in a method can be accessed in the method.

Pass variable form to class and retrieve it from another form

C# situation....i've set a value to a variable from another class using form1 using this...
CLASS
public int _a;
public int a
{
get
{
return _a;
}
set
{
_a = value;
}
}
Form 1
private void btnchangevalue_Click(object sender, EventArgs e)
{
class x = new class();
x.a = 1;
}
Form 2
private void btngetvalue_Click(object sender, EventArgs e)
{
class x = new class();
messagebox.show(x.a);
}
the problem is the class variable that ive set always turns to null when i've tried to retrieve it..
You should use a static variable
static variable is a variable that has been allocated statically, whose lifetime extends across the entire run of the program.
public static int a
What is happening is that new variables
Are created each time
You create the form object
Well i think its because you are instantiating a new instance of class every time....create a new instance of class once and then just use the same whenever you click the button.
class x = new class();
private void btnchangevalue_Click(object sender, EventArgs e)
{
x.a = 1;
}
private void btngetvalue_Click(object sender, EventArgs e)
{
messagebox.show(x.a);
}

Updating GUI form Separate Class

so I've been searching for the past few hours, reading on everything about how to update the GUI of the form from another class. I tried, backgroundworker, and Invoke, but nothing seems to work, or rather I'm not doing it right. (I'm still pretty new to c#) So..why doesn't this method work at all?
Form 1:
private void button2_Click_1(object sender, EventArgs e)
{
prog.stuff();
}
public void Updateprogressbar(int input)
{
progressBar1.Value = input;
}
Class Prog
public static void stuff()
{
Form1 f = new Form1();
int up = 100;
f.Updateprogressbar(up);
}
I know this is probably a very easy question, but I still can't figure it out. The progress-bar just won't update. And I do have it all enabled to public in the properties. Thanks anyway.
private void button2_Click_1(object sender, EventArgs e)
{
prog.stuff(this);
}
public void Updateprogressbar(int input)
{
progressBar1.Value = input;
}
public static void stuff(Form f)
{
int up = 100;
f.Updateprogressbar(up);
}
So you can see the reason your code doesn't work is because your instantiating a new instance of Form1 thats only alive in the stuff() method. In my code I pass a reference of Form1 into class Prog.Stuff there by giving me access to form1's methods.

Get the value of a property from a Class in Form2, and that value have been set in Form1 in C#

Here is the scenario. I want to set the value of Server in Class1, i am setting the value in Form1.
Then get the value of Server in Class1 in Form2. Here is what i have.
class Class1
{
private string server;
public string Server
{
get { return server; }
set { server = value; }
}
}
//Form1 where i want to set the value of server
private void setBtn_Click_1(object sender, EventArgs e)
{
Class1 sample = new Class1();
sample.Server = serverTxt.Text;
}
//Form2 where i want to get the value of server that i've set in Form1
private void setBtn_Click_1(object sender, EventArgs e)
{
Class1 sample = new Class1();
string serVer = sample.Server;
}
I know i can't have a value of server because i declared a new instance of Class1. But is there any way that i can still get the value of Server in Form2 that i have set in Form1?
Please spare with me, i am new in C#, thanks in advance guys :D
There are number of alternatives but static instance of Class1 would be easier.
In form1, declare/create static instance of Class1 class
//Form1 where i want to set the value of server
public static Class1 sample=new Class1();
private void setBtn_Click_1(object sender, EventArgs e)
{
sample.Server = serverTxt.Text;
}
and in Form2,
//Form2 where i want to get the value of server that i've set in Form1
private void setBtn_Click_1(object sender, EventArgs e)
{
string serVer = Form1.sample.Server;
}
Not only you can't do that, but in your code after the execution of setBtn_Click_1 the object of type Class1 that you created is gone - this is because you only have a reference to it in the method, so when the method executes the reference is gone!
You could send it in a constructor when creating the second form.
Something like this then
class Class1
{
private string server;
public string Server
{
get { return server; }
set { server = value; }
}
}
//form 1
private void setBtn_Click_1(object sender, EventArgs e)
{
Class1 sample = new Class1();
sample.Server = serverTxt.Text;
prevForm = sample;
}
//form 2
private void setBtn_Click_1(object sender, EventArgs e)
{
Class1 sample = new Class1{ Server=prevForm.Server };
}
For this you should keep the result or the reference to you first form somewhere so you can acces it later on
You have to set the value of serverTxt.Text in Form1 to the Global variable(the simpliest way). Then just take the value of this global variable in Form2
You can send the relevant data in the Form2 constructor and initialize it from Form1 (pass the data when you initialize Form2 in Form1)
[EDIT]
You could also pass the information via a database that keeps that data or using an external file that both forms have access to.
one solution to this is to declare the server property in the Calss1 as static
class Class1
{
public static string Server { get; set; }
}
so that you can get its value between the two forms
private void setBtn_Click_1(object sender, EventArgs e)
{
Class1.Server = serverTxt.Text;
}
private void setBtn_Click_1(object sender, EventArgs e)
{
string serVer = Class1.Server;
}
use this only if you if you have one Server for all the instances of Class1

Categories