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
Related
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.
I know this has been asked thousands of time but still after a lot of research I can't find a solution and I am really sorry about this post.
I want to access my Label from a class in another namespace.
This is a sample of code to understand better what I am trying to do:
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
}
//class in another namespace
class Servers
{
public void _SetlabelText()
{
Main.label1.Text = "New Text";
}
}
How am I supposed to do it the proper way?
One option is to store a reference to the form in the constructor like this:
public class Servers
{
private Form _frmMain;
public Servers(Form frmMain)
{
_frmMain = frmMain;
}
public void SetlabelText()
{
_frmMain.label1.Text = "New Text";
}
}
And use it like this:
public partial class Main : Form
{
public Main()
{
InitializeComponent();
var servers = new Servers(this);
servers.SetlabelText();
}
}
However, it's typically advised to return back to the Form class and set it there, like this:
public partial class Main : Form
{
public Main()
{
InitializeComponent();
label1.Text = Servers.GetTextForLabel();
}
}
public class Servers
{
public static string GetTextForLabel()
{
return "New Text"; //(I assume this will be much more complex)
}
}
I am doing exercises in Head First C# book.
This code is supposed to be about encapsulation.
class DinnerParty
{
private int NumberOfPeople;
....
public void SetPartyOptions(int people, bool fancy) {
NumberOfPeople = people;
.....
}
public int GetNumberOfPeople() {
return NumberOfPeople;
}
}
In form1 class
public partial class Form1 : Form
{
DinnerParty dinnerParty;
public Form1()
{
InitializeComponent();
dinnerParty = new DinnerParty() {NumberOfPeople = 5 };
...
Is this suppose to work?
Visual Studio is showing me an error. (cannot access due to its protection level)
I am very new at this.
Thanks
That's becouse NumberOfPeople is private means that it can't be accessible from outside the class DinnerParty, so you need to make it public.
No that is not supposed to work. I can't see the book, so I can't comment on the context. However:
public int NumberOfPeopoe {get;set;}
Is a reasonable fix.
You could use the idea of a "Fluent interface" by writing something like:
class DinnerParty
{
private int NumberOfPeople;
....
public DinnerParty SetPartyOptions(int people, bool fancy) {
NumberOfPeople = people;
.....
return this; // Return own instance to allow for further accessing.
}
public int GetNumberOfPeople() {
return NumberOfPeople;
}
}
And then call it:
public partial class Form1 : Form
{
DinnerParty dinnerParty;
public Form1()
{
InitializeComponent();
dinnerParty = new DinnerParty().SetPartyOptions( 5, true );
...
NumberOfPeople is a private member and you can not use it out of the class dude
The NumberOfPeople is private, so it will not work. The best solution here is to create public property instead of private field or add constructor and initialize this field there.
You can't initialize a private field outside your scope (class).
Make it a property.
public int NumberOfPeople { get; set; }
and now, this will work
dinnerParty = new DinnerParty() { NumberOfPeople = 5 };
I've searched Google all day and can't find the correct answer to my issue, hoping someone here can help me.
So, in the "Main" form I have the method to show a form that needs to be centered directly above the parent form (frmMain). Normally I would call ShowDialog(this) to see the parent, but for some reason I have to set the loadNewsFeedItem to static in order to see the method from the flpNewsFeedHeader : Label derrived class (below). The OnClick event triggers the method loadNewsFeedItem().
When I call this to set the parent, I'm getting the message "Keyword 'this' is not valid in a static property, static method, or static field initializer"
namespace NewsFeeds
{
public partial class FrmMain : Form
{
public static void loadNewsFeedItem()
{
frmNewsFeedView frmFeedView = new frmNewsFeedView(FrmFuncs.selFeedID);
frmFeedView.ShowDialog(this); // Error occurs on this line, when calling this via a static method
}
}
}
public class flpNewsFeedHeader : Label
{
private int FeedID = 0;
public int theFeedID
{
get { return FeedID; }
set { FeedID = value; }
}
protected override void OnClick(EventArgs e)
{
FrmFuncs.selFeedID = FeedID;
Thread thrShowFeed = new Thread(new ThreadStart(FrmMain.loadNewsFeedItem));
thrShowFeed.Start();
}
}
Can someone please give me a corrected code example or a hint as to how to get the loadNewsFeedItem() to be visible without setting the accessor to static, or how to work around this in a static accessor?
Thanks in advance!
Chris
Edit: used ActiveForm for owner.
public partial class FrmMain : Form
{
public static void loadNewsFeedItem(Form owner)
{
frmNewsFeedView frmFeedView = new frmNewsFeedView(FrmFuncs.selFeedID);
frmFeedView.ShowDialog(owner);
}
}
}
public class flpNewsFeedHeader : Label
{
private int FeedID = 0;
public int theFeedID
{
get { return FeedID; }
set { FeedID = value; }
}
protected override void OnClick(EventArgs e)
{
FrmFuncs.selFeedID = FeedID;
// Shouldn't need a new thread. Already on the GUI thread.
FrmMain.loadNewsFeedItem (System.Windows.Forms.Form.ActiveForm);
}
}
may be you mean this:
frmFeedView.Owner = System.Windows.Forms.Form.ActiveForm;
frmFeedView.ShowDialog();
In a static method, this is meaningless. One option is to skip the parameter
frmFeedView.ShowDialog();
The other option is to setup a static variable as shown below (but beware, it can have side effects if you try to open multiple instances of FrmMain)
public partial class FrmMain : Form
{
private static FrmMain staticInstance;
public FrmMain()
{
staticInstance = this;
InitializeComponent();
...
}
public static void loadNewsFeedItem()
{
frmNewsFeedView frmFeedView = new frmNewsFeedView(FrmFuncs.selFeedID);
frmFeedView.ShowDialog(staticInstance );
}
class Form1 : Form
{
public void enable()
{
//1st method which i want to call from another class
}
public void display()
{
//2nd method which i want to call from another class
}
}
class Buffer : signal
{
protected override Analyse()
{
//from here i want to call two functions in form class
}
}
this is how my code looks like anyone please reply this tread.........
When creating the Buffer class, you have to pass reference to the real instance of Form1 then just use that instance. Sample code:
class Form1 : Form
{
public void InitBuffer()
{
Buffer b = new Buffer(this);
...
}
public void enable()
{
//1st method which i want to call from another class
}
public void display()
{
//2nd method which i want to call from another class
}
}
class Buffer : signal
{
private Form1 form;
public Buffer(Form1 parent)
{
form = parent;
}
protected override Analyse()
{
form.enable();
form.display();
}
}
You can't grab the true instance of Form1 just like that out of nowhere.