I'm just learning C# after using VBA for many years, I'm not a professional and this is something I do in my leisure. I'm looking to replicate the logic of using a public variable that can be accessed from a method and incremented by one each time when clicking button cmdPublicVartest , Below is the code I have so far, but am getting the error
An object reference is required for the non-static field, method, or property, in the publicvar class, it looks because it's a static class, however if I remove it from a static class, I would have to call an instance of the class on the button cmdPublicVartest. Is there a way I can keep publicvar a static class, so I don't have to do an instance of the class on the button?
namespace testDB
{
public partial class Database : Form
{
public string publictest = "public test";
public int pUblicint = 0;
public static void PublicVar()
{
MessageBox.Show(publictest + pUblicint);
pUblicint++;
}
private void cmdPublicVartest_Click(object sender, EventArgs e)
{
testDB.Database.PublicVar();
}
}
}
You cannot access non-static fields from a static method because they belong to an instance of the class, and when calling a static method you do not have an instance.
You could either make the fields static like this
public partial class Database : Form
{
public static string publictest = "public test";
public static int pUblicint = 0;
public static void PublicVar()
{
MessageBox.Show(publictest + pUblicint);
pUblicint++;
}
private void cmdPublicVartest_Click(object sender, EventArgs e)
{
testDB.Database.PublicVar();
}
}
Or make the method non-static like this
public partial class Database : Form
{
public string publictest = "public test";
public int pUblicint = 0;
public void PublicVar()
{
MessageBox.Show(publictest + pUblicint);
pUblicint++;
}
private void cmdPublicVartest_Click(object sender, EventArgs e)
{
PublicVar();
}
}
Just revise your code to this:
namespace testDB
{
public partial class Database : Form
{
public string publictest = "public test";
public int pUblicint = 0;
public void PublicVar()
{
MessageBox.Show(publictest + pUblicint);
pUblicint++;
}
private void cmdPublicVartest_Click(object sender, EventArgs e)
{
PublicVar();
}
}
}
While PublicVar() is static, the fields it references (publictest, pUblicint) are not. You have to make them static as well or make PublicVar() not-static.
Related
While using Winform, I had defined a struct in a main GUI class and change the value of struct in another class. So this is my Program.cs:
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new App());
}
}
public struct StructExample {
public string str1;
public string str2;
}
and the codes for main GUI looks like:
public partial class App : Form {
public StructExample Example = new StructExample();
public App() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
ButtonToClick.Click += (s, evt) => {
AnotherGUI setWindow = new AnotherGUI(Example);
setWindow.ShowDialog();
};
}
}
and the codes for AnotherGUI looks like:
public partial class AnotherGUI : Form {
public StructExample Example;
public SettingsGUI(StructExample Example) {
InitializeComponent();
this.Example = Example;
}
private void DoSomething() {
//Change values in Example
Close();
}
}
But I have a problem that after closing AnotherGUI, the values of Example wouldn't change. What should I do to change the values of Example in AnotherGUI to use in App?
Struct is a value type so you will need to explicitly pass by ref in case you need the receiving method to modify it.
class Program
{
static void Main(string[] args)
{
var example = new Example() { Name = "Name"};
Console.WriteLine(example.Name);
WrongStructModifier(example);
Console.WriteLine(example.Name);
CorrectStructModifier2(ref example);
Console.WriteLine(example.Name);
}
static void WrongStructModifier(Example example)
{
example.Name = "Modified Name";
}
static void CorrectStructModifier2(ref Example example)
{
example.Name = "Modified Name";
}
}
struct Example
{
public string Name;
}
I have 2 classes where 1 class is like the main class and I have a secondary class which is instantiated from the main class. How am I able to use the main class' method from the secondary class. Here's code to give an illustration of what I want.
public class MainClass
{
private SecondaryClass secondaryClass;
private int testValue;
public MainClass()
{
this.secondaryClass = new SecondaryClass();
testValue = 0;
}
public void updateTestValue (int val)
{
testValue = val;
}
}
public Class SecondaryClass : Form
{
public SecondaryClass()
{
}
private void button1_click(Object sender, EventArgs e)
{
// I want to be able to do this:
primaryClass.updateTestValue(100);
}
}
You can make classes communicate without having one derive from another.
public class MainClass
{
private SecondaryClass secondaryClass;
private int testValue;
public MainClass()
{
this.secondaryClass = new SecondaryClass(this.UpdateTestValue);
testValue = 0;
}
public void UpdateTestValue (int val)
{
testValue = val;
}
}
public class SecondaryClass : Form
{
private Action<int> UpdateValue { get; }
public SecondaryClass(Action<int> updateValue)
{
this.UpdateValue = updateValue;
}
private void button1_click(Object sender, EventArgs e)
{
this.UpdateTestValue(100);
}
}
In this organization, primary class is passing a delegate to its own instance-level method when it creates the secondary class. Secondary class calls that delegate when appropriate, without ever knowing what function that is.
This is the example of the callback pattern.
There are other variants of the same idea. For example, primary class could implement an interface which defines the UpdateValue method. Then, it passes this reference to every object which needs access to that method. Other objects, like an object of secondary class, would then simply call a method of that interface, once again not knowing that it is in fact the primary class they are referencing.
public interface IListener
{
void Update(int value);
}
public class MainClass : IListener
{
private SecondaryClass secondaryClass;
private int testValue;
public MainClass()
{
this.secondaryClass = new SecondaryClass(this);
testValue = 0;
}
public void Update(int val)
{
testValue = val;
}
}
public class SecondaryClass : Form
{
private IListener Listener { get; }
public SecondaryClass(IListener listener)
{
this.Listener = listener;
}
private void button1_click(Object sender, EventArgs e)
{
this.Listener.Update(100);
}
}
The price of this solution is one additional type in the system (interface IListener), and the benefit is that you can avoid working with delegate syntax. Delegates have a drawback that their arguments have no names, and therefore you can easily make a bug if you mix them up.
public class MainClass: Form
{
private int testValue;
public MainClass()
{
testValue = 0;
}
public void updateTestValue (int val)
{
testValue = val;
}
}
public class SecondaryClass : MainClass
{
public SecondaryClass()
{
}
private void button1_click(Object sender, EventArgs e)
{
// I want to be able to do this:
updateTestValue(100);
}
}
A class can only have one base class
What you could do is move the :Form base class up to the primary class and then from your secondary class have it's base class as Primary class and use the functions as follows.
public class PrimaryClass : Form
{
private int testValue;
public void PrimaryClassMethod()
{
Console.WriteLine("Method from Primary Class");
}
public void UpdateTestValue (int val)
{
testValue = val;
}
}
public class SecondaryClass : PrimaryClass
{
public void CallPrimaryClassMethod()
{
this.PrimaryClassMethod();
this.UpdateTestValue(10000);
}
}
https://dotnetfiddle.net/PC2WVu
public class variables {
public static int edit{ get;set; }
}
And also tried:
public static int edit = 0;
public static int edit { get; set; }
public static int edits { get { return edit; } }
Using the form
form:form1 {
// Changing the value of variable to 1
private void Form1_Load(object sender, EventArgs e) {
variables.edit=1;
}
// Calling the new form where I'll use its value
private void Button_Click(object sender, EventArgs e){
form2 A=new form2();
A.Show();
}
}
form:form2{
// Showing the value of the variable in a message box
private void Form2_Load(object sender, EventArgs e){
MessageBox.show(variables.edit.ToSting());
}
}
The Message in all cases returned 0 least that call again. I need to know how to make the values initialize step as the first. I have to tab Use the many variable that keep data from one form to another and use in the load.
Make sure your class variables is static, not only the fields/properties:
public static class variables {
public static int edit = 0;
}
Another source of problem with static classes is when you use one field/property when setting another:
public static class variables {
public static int someValue = 2;
public static int other = someValue + 3;
}
AFAIK, you can't be sure of what field/property will be set first by the static constructor at runtime, unless you set the values in the static constructor, like this:
public static class variables {
static variables() {
someValue = 0;
other = someValue + 3;
}
public static int someValue;
public static int other;
}
If you are declaring static fields/properties in a really non-static class, check for the problem above, and if nothing else is changing the static field/property in another place, even inside the non-static class.
I have the below code in my C# program:
namespace test
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
string val = "0";
parent o1 = new parent();
}
}
public class parent
{
string test1()
{
string val = "2";
return val;
}
}
public class child:parent
{
string test1()
{
string val = "3";
return val;
}
}
}
How to access the method test of class parent?
I tried by creating object of parent but unable to access the method.
what's wrong in my code?
You need to add Access Modifier. Use internal or public with method because if you not mention it then it will be consider as private
public class parent
{
internal string test1()
{
string val = "2";
return val;
}
}
You have missed the public access modifier before the method declaration. By default the method is private
I have a problem in a same namespace:
public partial class frmForm1 : Form // Form1
{
public class Account
{
public string Username, Password, RePassword, Name, bd, dt, dc;
}
public class ListAcc
{
public static int count = 0;
private static List<Account> UserList;
public static List<Account> Data()
{
return UserList;
}
}
public partial class frmForm2 : Form // Form2
{
private void button2_Click(object sender, EventArgs e)
{
ListAcc A; // error
string n = A<Account>[0].Usename; // error
// What should i do?
}
}
Someone can help me fix this problem? Thanks a lot!
You've nested the Account and ListAcc class inside the frmForm1 class.
Move them outside of frmForm1's class definition or change it to be frmForm1.ListAcc A;
Also, I'm not sure what you're trying to do here. This wouldn't compile no matter what you do. Are you trying to make ListAcc a generic class?
string n = A<Account>[0].Usename; // error
If you need Account in more than one class maybe it's better not to put in in the frmForm1 but in a separate file. A class inside another class is not a good idea.
public partial class frmForm1 : Form // Form1
{
public class Account
{
//some code
}
public class ListAcc
{
//SomeCode
}
}
public partial class frmForm2 : Form // Form2
{
private void button2_Click(object sender, EventArgs e)
{
//Thats will work
frmForm1.ListAcc A = new frmForm1.ListAcc();
string n = A.Data()[0].Usename;
}
}
the way your class structure is defined you need to declare the variable like this
frmForm1.ListAcc A;
Move Account class outside the frmForm1 class.
Or you should address your Account type through its "parent" type frmForm1:
frmForm1.Account