1

I am trying to get a list from another form. I have made the list public and put it in its own class to no avail.

Form1:

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Common;
using System.Configuration;

namespace eAnfonebu
{
    public class variables
    {
        public List<int> rhifAnfoneb = new List<int>();
        public List<int> rhifArcheb = new List<int>();
        public string[] ddydiadArcheb;
        public string[] enwArchebwr;
        public string[] eBost;
        public int[] gair;
        public decimal[] prisMilGair;
    }
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void createNew_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.ShowDialog(); // Shows Form2
        }

    }
}

Form2 (addInvoice):

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace eAnfonebu
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void AddInvoice_Click(object sender, EventArgs e)
        {
            try
            {
                variables.rhifAnfoneb.Add(variables.rhifAnfoneb[variables.rhifAnfoneb.Length - 1] + 1);
            }
            catch
            {
                variables.rhifAnfoneb.Add(1);
            }
        }
    }
}

I am getting the error (An object reference is required for the non-static field, method, or property 'variables.rhifAnfoneb') in

            try
            {
                variables.rhifAnfoneb.Add(variables.rhifAnfoneb[variables.rhifAnfoneb.Length - 1] + 1);
            }
            catch
            {
                variables.rhifAnfoneb.Add(1);
            }

I am sorry for different languages in same code but I am billingual and sometimes I set out to make an app in one language but then it slowly morphs to another.

If awnsering could you please awnser as simply as you could because I am new to c#

Any help would be greatly appreciated.

Eoin

Eoin
  • 27
  • 1
  • 6
  • 2
    Have you tried researching the compiler error? There are a lot of resources explaining that you need an _instance_ of your `variables` class, or make it static. – CodeCaster Feb 27 '20 at 13:24
  • And why the upvote? Is this question unique? Does it show research effort? – CodeCaster Feb 27 '20 at 13:24
  • I have looked into it and with my limited coing knowledge I made the class variables static but that sent an error – Eoin Feb 27 '20 at 13:26
  • 1
    A class named `variables` is a really bad name. You should consider to name classes like entities that encapsulate behaviour. What does `variables` do? Anyway in order to make the entire class `static`, you have to make every member static as well. – MakePeaceGreatAgain Feb 27 '20 at 13:29
  • @RenéVogt I considered [that duplicate](https://stackoverflow.com/questions/498400/), but it's a really poorly answered one. A static method on a form to set a label's text? That label is an instance field, it won't even compile! And the second code block, instantiating a form to call a method on it, but never showing it? What if it's already shown before? And that has 370 upvotes? WTH? – CodeCaster Feb 27 '20 at 13:35
  • 1
    And in addition to what @HimBromBeere says, the bigger picture here is that you want to share data between multiple forms. You don't use plain variables nor just lists and arrays for this in WinForms, you use data binding and events and INotifyPropertyChanged and stuff. The problem isn't that you're just learning C#, you're trying to learn a GUI framework along with it, and to **properly** answer this question requires way more than just "use `new` or `static`". – CodeCaster Feb 27 '20 at 13:41
  • 1
    @CodeCaster ok, I reopened. Unfortunately, there is no really good canonical question/answer for CS0120, though this error pops up on SO almost once a day. – René Vogt Feb 27 '20 at 13:47
  • @René I know, right? See also my latest comment. This question is not simply and shortly answered. The answers given are incorrect or shortcoming at best. I think I've found a better one and closed it as such. – CodeCaster Feb 27 '20 at 13:48

2 Answers2

1

I am getting the error (An object reference is required for the non-static field, method, or property 'variables.rhifAnfoneb') in

Instantiate the Class then use the its members

public static Variables variabs {get;set;}
public Form2()
{
  variabs = new variables();
  InitializeComponent();
}
private void AddInvoice_Click(object sender, EventArgs e)
{
 try
 {
  variabs.rhifAnfoneb.Add(variables.rhifAnfoneb[variables.rhifAnfoneb.Length - 1] + 1);
 }
.
.
}

To Access from Form1

public static Variables variabs {get;set;}
public Form1()
{
   variabs= Form2.variabs;
}


Clint
  • 6,011
  • 1
  • 21
  • 28
  • 1
    What use will this local variable have, if they want to share it between forms? – CodeCaster Feb 27 '20 at 13:27
  • it will contain the number of an invoice and I need to share it between forms so that I can add a set of data in one form then generate invoices in another form with that data – Eoin Feb 27 '20 at 13:30
  • Let me put it this way: what use has the code `var sl = new List(); sl.Add("foo");`, when it goes out of scope? If a variable goes out of scope before you can do anything useful with it, what use does it have? – CodeCaster Feb 27 '20 at 13:32
  • @CodeCaster, understood, thanks and let me update. – Clint Feb 27 '20 at 14:19
  • @Eoin, can you confirm if the solution answered your query, if yes, you can mark as resolved by clicking the green tickbox – Clint Feb 29 '20 at 22:45
1

change

public class variables

to

public static class variables

And make all fields static as well. Because, A static class can only contain static data members, static methods.

or

create an instance of variables

variables v = new variables();
v.rhifAnfoneb.Add(variables.rhifAnfoneb[variables.rhifAnfoneb.Length - 1] + 1);

Reference

Static Classes and Static Class Members

A Farmanbar
  • 4,381
  • 5
  • 24
  • 42