I can't see anything other than an normal class initialized.
Here is the class, class Bet.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lab_ADayAtTheRaces
{
public class Bet : Form1
{
public int Bets_Joe;
public int Bets_Bob;
public int Bets_Al;
public int Dog_Joe;
public int Dog_Bob;
public int Dog_Al;
public int Amount;
public int Dog;
public Guy Bettor;
public string GetDescription()
{
Amount = (int)numericUpDownBucksToBet.Value;
Dog = (int)numericUpDownDogToBetOn.Value;
//Bettor =
return Bettor + " placed a bet in the amount of " + Amount + " bucks on dog number " + Dog;
}
public int PayOut(int Winner)
{
return Winner;
}
public void MakeBets()
{
if (joeRadioButton.Checked == true)
{
Bets_Joe = (int)numericUpDownBucksToBet.Value;
Dog_Joe = (int)numericUpDownDogToBetOn.Value;
}
else if (bobRadioButton.Checked == true)
{
Bets_Bob = (int)numericUpDownBucksToBet.Value;
Dog_Bob = (int)numericUpDownDogToBetOn.Value;
}
else if (alRadioButton.Checked == true)
{
Bets_Al = (int)numericUpDownBucksToBet.Value;
Dog_Al = (int)numericUpDownDogToBetOn.Value;
}
}
}
}
Here is the code that throws the exception:
namespace Lab_ADayAtTheRaces
{
public partial class Form1 : Form
{
Bet bets = new Bet(); //**THIS LINE THROWS THE STACKOVERFLOW EXCEPTION**
Greyhound[] dogs = new Greyhound[3];
It wants me to say something more but I have nothing more to add so I'll just add some lines here and here.
It wants me to say something more but I have nothing more to add so I'll just add some lines here and here.
It wants me to say something more but I have nothing more to add so I'll just add some lines here and here.
It wants me to say something more but I have nothing more to add so I'll just add some lines here and here.
Any help is muchly appriciated... thanks in advance
Kristjan
your Bet inherits from Form1, so Bet() will call to Form1(), and Form1() will again call to Bet() inside it -> again and again -> StackOverflow
Tip: We should never call the constructor of a class in its constructor or its class definition like this:
public class Form1 : Form {
public Form1(){
}
public Form1(string s){
}
public Form1 f = new Form1();//this will throw StackOverflowException
public Form1 f = new Form1("");//this will also throw StackOverflowException
//Form2 inherits from Form1
public Form2 f = new Form2(); //this will throw StackOverflowException
}
//or
public class Form1 : Form {
public Form1(){
Form1 f = new Form1();//This will throw stackoverflowexception
Form1 f = new Form1("");//This won't throw any exception
}
public Form1(string s){
}
}
When a class is initialized, all the members are initialized first before calling the constructor, hence the line Bet bets = new Bet(); is executed before initializing your Form1. So you have to avoid it. To initialize new Bet(), you should call it in some event so that the event will never be fired by calling the constructor, such as Load.
Bet bets;//Just declare it without initializing it
private void Form1_Load(object sender, EventArgs e){
bets = new Bet();
}
Related
guys! I have a problem. My method doesn`t want to work in main class Form1.
Closer to the point. I have main class Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void GetLengthFirst_Click(object sender, EventArgs e)
{
string result = Microsoft.VisualBasic.Interaction.InputBox("Input elem:");
FirstArray.Text = result;
// WHEN I CALL METHOD FROM MY CLASS Array IT DOESNT WORK
Array _arr = new Array();
_arr.Masstrickt(result);
/* BUT WHEN I CALL PROPERTIES OF THIS CLASS FOR EXMPL
ReturnSecond = "12345";
IT WORK*/
}
public string ReturnSecond
{
get { return SecondArray.Text; }
set { SecondArray.Text = value; }
}
}
Second class:
class Array
{
public void Masstrickt(string x)
{
Form1 frm = new Form1();
frm.ReturnSecond = x;
}
}
Sorry for my grammar. I`m not native speaker
You want to pass the result of Masstrickt to your form, so you need to change your function return type from void to string.
If you declare Array out of Form1 class, set it to public
// public keyword would make Array visible to Form1
public class Array
{
// Return type is string
public string Masstrickt(string x)
{
return x;
}
}
In Form1
public void GetLengthFirst_Click(object sender, EventArgs e)
{
string result = Microsoft.VisualBasic.Interaction.InputBox("Input elem:");
FirstArray.Text = result;
Array _arr = new Array();
ReturnSecond = _arr.Masstrickt(result);
// ReturnSecond is the content of result
}
See fellows I have a problem and is that i think my code is in a loop
And before you answer "RePoST" I have visited all the other threads and i still did not hit with a solution
namespace Triangle_Proportions
{
public partial class Form1 : Form
{
public class Data : Form1
{
public static class Variables
{
public static int A;
public static int B;
public static int C;
public static int a;
public static int b;
public static int c;
}
public void Set_Variables()
{
Variables.A = label0A.Left;
Variables.B = label0B.Left;
Variables.C = label0C.Left;
Variables.a = label_a.Left;
Variables.b = label_b.Left;
Variables.c = label_c.Left;
}
}
public Form1()
{
InitializeComponent();
Data etc = new Data();
etc.Set_Variables();
}
}
}
This part of the code its only goal is to call Set_Variables(); after InitializeComponent();
I know I can solve this simply by just "quicking out" Variables and Set_Variables from Data but i want both to remain inside Data because is easier to look it up when searching this.
It doesn't have any syntax errors but every time I decide to call Set_Variables(); the program never opens
You have a recursion problem.
In the constructor of Form1 you instaniate a new class which is also inherited from Form1 which is calling the constructor again. See the problem?
Create a non static version:
public class Variables
{
public int A;
public int B;
}
And your Form:
public partial class Form1 : Form
{
private Variables _variables;
public Form1()
{
InitializeComponent();
_variables = new Variables
{
A = label0A.Left,
B = label0B.Left
};
}
}
As the title suggests, I have made/initialized/whatever a 2D list in Class 1, I fill it with stuff in class 2 and am trying to use its contents in class 3.
However, after filling the list in class 2 (where I have confirmed it has been filled), when calling the method in class 3 where the class is to be used, it is simply empty and I have no clue why.
Class 1
class class1
{
public List<List<String>> playerInfo = new List<List<String>>();
//it's a 2D list but as far as I know that shouldn't be a problem
Class 2
public sealed partial class class2: Page
{
class1 host = new class1();
private void joinButton_Click(object sender, RoutedEventArgs e)
{
if (host.playerInfo.Count() <= 4)
{
//fill list
It seems to go OK up to this point. If I do a Count it shows it contains 2 elements, cool.
Class 3
public sealed partial class MainPage : Page
{
GameHost host = new GameHost();
public void Init()
{
if (host.playerInfo.Count() >= 2)
{
//Do stuff
}
}
}
Yet here the list is simply empty. The Count simply returns 0.
What could this be?
If my example here is not very clear, let me know, I'm not very good with this Stack Overflow thing yet.
The problem is that you create a new instance in the MainPage Class. This instance has no values in the list. You could simply pass the instance into the Init method as parameter:
public sealed partial class MainPage : Page
{
public void Init(GameHost host)
{
if (host.playerInfo.Count() >= 2)
{
//Do stuff
}
}
}
If you want to use the GameHost instance throughout the entire MainPage class you can also pass it through the constructor and initialize the field like this:
public sealed partial class MainPage : Page
{
GameHost host;
public MainPage (GameHost _host)
{
host = _host;
}
}
Now it becomes usable in the class with all the initialized values that you had before.
public sealed partial class MainPage : Page
{
GameHost host;
public MainPage (GameHost _host)
{
host = _host;
}
public void Init(GameHost host)
{
if (host.playerInfo.Count() >= 2) // now it should have the desired items
{
//Do stuff
}
}
}
Here's how I would do it.
Initialize 2D List in App.xaml.cs on App Launch
private static List<List<String>> _playerInfo;
public static List<List<String>> PlayerInfo
{
get
{
return (_playerInfo == null) ? new List<List<string>>() : _playerInfo;
}
set { _playerInfo = value; }
}
Now there is no need for class 1.
class2 will be
public sealed partial class class2 : Page
{
private void joinButton_Click(object sender, RoutedEventArgs e)
{
if (App.PlayerInfo.Count <= 4)
{
// Fill your List Here
}
}
}
And MainPage will be
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
if (App.PlayerInfo.Count >=2)
{
// Do Stuff.
}
}
}
Anyway, the common root cause for that is overriding the list reference.
See the simple example below:
static void Main()
{
List<int> mainList = new List<int>();
UpdateList(mainList);
Console.WriteLine(mainList.Count); // prints 0
}
private static void UpdateList(List<int> numbers)
{
numbers = new List<int>();
numbers.Add(1);
numbers.Add(2);
Console.WriteLine(numbers.Count); // prints 2
}
The list reference is transferred in the UpfdateList function. Once you executed the first line in UpfdateList with the new keyword, you lost the reference to the list from the main and now the referrence is to the new list created in UpfdateList.
To fix it, don't override the list reference:
private static void UpdateList(List<int> numbers)
{
//numbers = new List<int>(); remove that
numbers.Add(1);
numbers.Add(2);
Console.WriteLine(numbers.Count); // prints 2. 'numbers' still holds the reference the the list from the main
}
Also, make sure you are using the same instance of Class1 in Class2 and Class3.
I am developing an application in C# with winforms. I am pretty good with C++ but very new to C# so please forgive my ignorance.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
class obj
{
private int member;
public obj(int n)
{ member = n; }
}
obj[] obj_arr = new obj[10];
obj_arr[0] = new obj(4); // Problem Here
}
}
This is a very simplified example of what I am trying to do, but as you can see I would like to declare an array of user defined objects. The problem I am having is that when I try to initialize the individual array member the compiler gives me an error. Actually it gives several errors. obj_arr[0] is highlighted with an error saying that it is a field but is being used as a type. The = is also highlighted with an error that says = is invalid token in class, struct, or interface declaration. Finally obj(4) is highlighted with an error saying method must have a return type.
I am a little stumped here, any help would be greatly appreciated.
New Code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
obj_arr[0] = new obj(4); // Problem Here
}
class obj
{
private int member;
public obj(int n)
{ member = n; }
}
obj[] obj_arr = new obj[10];
obj o1 = obj_arr[0];
}
You're trying to execute code within the class definition. Only member initialization con occur outside of methods. Move that code to to another method, or the constructor:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
obj_arr[0] = new obj(4); // Problem Here
o1 = obj_arr[0];
}
class obj
{
private int member;
public obj(int n)
{ member = n; }
}
obj[] obj_arr = new obj[10];
obj o1;
}
That should make all of the compiler errors go away (one syntax error is causing another).
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 );
}