I have following question, right now like 2 minutes ago a guy posted his code.
He couldn't use his instance obj , and I don't know why he deleted his question.
He had also some usings, I think he had using static N.Form1
using System;
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 WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
int Y(int a)
{
a = 10;
Console.WriteLine(a);
return a;
}
}
}
}
namespace WindowsFormsApp1
{
class Class1
{
N.Form1 obj = new N.Form1();
public void X(int a)
{
var v = obj.Y(a);
Console.WriteLine(v);
}
}
}
So first I've seperate the method, and made it public so I can create instance and use it in another class.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int Y(int a)
{
a = 10;
Console.WriteLine(a);
return a;
}
}
And then instead of using N.From1, I used the namespace for declaring instance WindowsFormsApp1.Form1 obj = new WindowsFormsApp1.Form1();
My Class1 looks like this now:
class Class1
{
WindowsFormsApp1.Form1 obj = new WindowsFormsApp1.Form1();
public void X(int a)
{
var v = obj.Y(a);
Console.WriteLine(v);
}
}
So there isn't any error, and my question is, is this the right way to declare and instance and use it later? Also are you allowed to use the namespace ?
Thanks,
Related
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class Namespaces
{
public Namespaces()
{
}
}
namespace NameSpace1
{
public class MyClass
{
public static int add(int a, int b)
{
return a + b;
}
}
namespace NameSpace2
{
public class MyClass
{ public static int add (int a, int b, int c)
{ return a + b + c; }
}
}
}
}`
```
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
int sum1 = NameSpace1.MyClass.add(1, 2);
int sum2 = NameSpace2.MyClass.add(1, 2, 3); //Attempting to call Namespace 2 from here
}
}
Previously, I tried rebuilding, checking for spelling mistakes or differences between my namespaces(didn't find any), checking for formatting mistakes or differences between the two original Namespace methods(the only one I could find really didn't make any difference).
NameSpace2 is within NameSpace1. Change it to int sum2 = NameSpace1.NameSpace2.MyClass.add(1, 2, 3); and it should work:
namespace ConsoleApp1
{
public class Program
{
public static void Main()
{
int sum1 = NameSpace1.MyClass.add(1, 2);
int sum2 = NameSpace1.NameSpace2.MyClass.add(1, 2, 3);
}
}
}
(Fiddle).
I have the following form with a method called setIndex
using System;
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 ProjectName
{
public partial class SettingsWindow : Form
{
internal readonly static SettingsWindow Instance = new SettingsWindow { Visible = false };
public SettingsWindow()
{
InitializeComponent();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string defaultsearch = listBox1.GetItemText(listBox1.SelectedItem);
Core.RegistryHelper.SaveSetting("Config", "ds", defaultsearch);
if (defaultsearch == "aaa") {
Core.LandingUrlOrig = Core.DomainName + "/defaulturl1.php";
} else {
Core.LandingUrlOrig = Core.DomainName + "/defaulturl2.php";
}
}
public static void setIndex(int i)
{
listBox1.SelectedIndex = i;
}
On another form called MainWindow during its initialization I call:
public MainWindow()
{
SettingsWindow.setIndex(0);
}
The error I get is:
An object reference is required for the non-static field, method, or property 'SettingsWindow.listBox1'
Initially the listbox method wasn't static and thus invisible from MainWindow. But now, listbox appears to not exist, even if the form has been instantiated. How do I solve this? I'm just learning C#.
Thank you in advance
because the function setIndex is static you need to use the Instance property:
public static void setIndex(int i)
{
Instance.listBox1.SelectedIndex = i;
}
or don't make that function static and then use instance in the mainwindow function:
public void setIndex(int i)
{
listBox1.SelectedIndex = i;
}
public MainWindow()
{
SettingsWindow.Instance.setIndex(0);
}
This question already has answers here:
Using list between forms
(3 answers)
Closed 2 years ago.
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
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
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;
}
I'm making a simple application to simulate the bankteller problem.
What I'm trying to simulate is:
You have 4 counters in a store. 1 counter is open. Customers start coming in and enter the line for the first counter.
When the fourth customer enters the line for the first counter, another counter should open. The line should be equally divided between the 2 counters.When the customer at the second counter is helped and no new customers enter the line, the counter should close. Basically 4 is too many.
I can't seem to figure it out. I know I need to use a queue. But how? Could someone give me an example in console application? Preferable C#.
Thanks in advance.
Here is what I tried so far,
register class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RegisterCounter
{
class Register
{
private int customerCount;
public Queue<Customer> Line = new Queue<Customer>();
public Register()
{
customerCount = 2;
}
public Register(int customerCount)
{
this.customerCount = customerCount;
}
public int getCustomers()
{
return customerCount;
}
}
}
Customer class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RegisterCounter
{
class Customer
{
private int checkoutTime;
public Customer()
{
checkoutTime = 3;
}
public Customer(int checkoutTime)
{
this.checkoutTime = checkoutTime;
}
public int GetCheckoutTime()
{
return checkoutTime;
}
}
}
Register manager:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RegisterCounter
{
class RegisterManager
{
public List<Register> registers = new List<Register>();
Register r1 = new Register();
Customer c1 = new Customer();
public RegisterManager()
{
registers.Add(r1);
}
public void ManageCustomers()
{
for (int i = 0; i < registers.Count; i++)
{
registers.Insert(i, new Register());
if (i / 4 <= registers..Line.Count)
{
}
}
}
}
}
I want to create a method which makes my application wait X number of seconds, then continues on down a line of scripts. For example, this is the code that I have so far, after reading many similar help topics:
using System;
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
methods.WriteTextToScreen(label1, "Hello!");
methods.sleepFor(1);
methods.WriteTextToScreen(label1, "Welcome!");
methods.sleepFor(1);
methods.WriteTextToScreen(label1, "Allo!");
}
public class methods
{
public static int timeSlept;
public static void WriteTextToScreen(Label LabelName, string text)
{
LabelName.Text = text;
}
public static void sleepFor(int seconds)
{
timeSlept = 0;
System.Timers.Timer newTimer = new System.Timers.Timer();
newTimer.Interval = 1000;
newTimer.AutoReset = true;
newTimer.Elapsed += new System.Timers.ElapsedEventHandler(newTimer_Elapsed);
newTimer.Start();
while (timeSlept < seconds)
{
Application.DoEvents();
}
Application.DoEvents();
}
public static void newTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timeSlept = IncreaseTimerValues(ref timeSlept);
Application.DoEvents();
}
public static int IncreaseTimerValues(ref int x)
{
int returnThis = x + 1;
return returnThis;
}
}
}
}
What I want to do is have my program do the methods.WriteTextToScreen(label1, "Hello!")
then wait for 1 second, then continue on in the same fashion. The problem is that the Form I'm displaying the text on doesn't show up at all until it has written "Allo!" onto the screen, so the first time it appears it already says that. Am I doing something wrong, or is there just no way to do this?
The form doesn't show until it has been constructed i.e. all the code in Form1 is run. See here for info on form constructors: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.form.aspx
To fix your problem you could move the writeTextToScreen and sleep code into the forms on load method. See http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onload.aspx