Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am building a program for my course in which i need global objects as i intend to have the object accessible from many forms and edited also. Before saying just use global variables i cant the specs state for use of OOP.
my latest attempt to fix this is using a public class but this gave a protection error problem
Code:
Form1.cs (forgot to rename and not re doing all the code and design)
public class ObjectsGlobal
{
Bays bay1 = new Bays();
Bays bay10 = new Bays();
}
frmInput.cs
private void btnAdd_Click(object sender, EventArgs e)
{
if ( 1 == Convert.ToInt32(nudBayNum))
{
ObjectsGlobal.bay1.CarMake = txtMake.Text;
}
}
any ideas are welcome at this point
Change it to:
public static class ObjectsGlobal
{
public static Bays bay1 = new Bays();
public static bay10 = new Bays();
}
Also, as recommended in a comment I have now read, take a look at the Singleton Pattern.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
1. Summarize the problem
The following for cycle keeps on running as i want, but always give me the same clicked button that is "0".
It does not give me an error. But by playing the game i can see that it's always the same number.
2. Describe what you've tried
I've tried searching around the internet for people like me. but sadly i couldn't find anything.
3. Show some code
Code that i'm talking about.
int ButtonNum;
public void Start()
{
for (int i = 0; i < ButtonsPage.Length; i++)
{
ButtonsPage[i].GetComponent<Button>().onClick.AddListener(delegate { ButtonClicked(ButtonNum); });
}
}
public void ButtonClicked(int i)
{
Debug.Log("Clicked" + i);
if (WhichType == "Nose")
{
NoseColor.sprite = NosesColor[i];
NoseOutline.sprite = NosesOutline[i];
}
//ButtonNum will be used to say which one is clicked. Still haven't add it though cause i wanted to fix this problem before
}
You are not modifying ButtonNum in any way, I assume the goal is to use i as button number, try changing your code to:
public void Start()
{
for (int i = 0; i < ButtonsPage.Length; i++)
{
var temp = i;
ButtonsPage[i].GetComponent<Button>().onClick.AddListener(delegate { ButtonClicked(temp); });
}
}
Temporary variable is required due to how closures work in C#.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I've searched this question here before asking and all of the answers seem to be people putting code after return break or others. I am having an issue where no matter what I put in an if statement, the code reads that it is unreachable.
private const double quarterPrice = 4.50;
private const double halfPrice = 7.50;
private const double fullPrice = 10.00;
private const double taxRate = .08;
private int orders = 0;
private double sales = 0;
private void btnFindMax_Click(object sender, EventArgs e)
{
if (quarterPrice > halfPrice)//if i put something in here, it is unreachable
{
int i = 1;//unreachable
if (quarterPrice > fullPrice)//unreachable
{
}
}
}
This is frustrating because I have nor idea why it's wrong, or what to do to fix it. It doesn't give me the red error underline, only the green suggestion line. However, when compiled, none of the code inside of the if statement executes.
I even tried to do:
private void btnFindMax_Click(object sender, EventArgs e)
{
if (quarterPrice < halfPrice)
{
Close();
}
}
And the code still didn't execute. I have no idea what is going on..
You have defined the variables as constants. The compiler knows that the condition in your if statement will never be true.
You have defined quarterPrice and halfPrice as constants. The compiler knows that quarterPrice will never be greater than halfPrice and is providing you with a warning.
For example you can generate the same warning like this.
if (false)
{
int i = 1;
// Do other work.
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Maybe I've misunderstood how constructors work, but in any case, I'm trying to create an array and populate it in the constructor.
I have the following code --
class ClsDeck
{
private string[] deck = new string[52];
private string[] hand = new string[12];
BuildDeck()
{
//lots of code assigning images to each individual element of the "deck" array.
}
//many other methods that need to be called by a form.
}
Visual Studio 2012 insists that the method have a return type. I have simply added "void" to the BuildDeck method, and the error disappeared, but every example I've seen of a constructor has to have the same name as the class, and it has been the only method in the class.
That won't even compile. BuildDeck() has no return type. Constructor names need to match the class name (including case). Replace BuildDeck with ClsDeck().
By definition, a constructor is a method that 1.) has the same name as the class, and 2.) has no return value.
In your example above, "BuildDeck" is not a constructor ... it is a method, and therefore must specify a return type (or "void" if it doesn't return anything).
If you want a constructor, rename "BuildDeck" to "ClsDeck".
The constructor of your class is actually missing.
Make the following changes, and your code will compile:
class ClsDeck
{
private string[] deck = new string[52];
private string[] hand = new string[12];
public ClsDeck()
{
// Place your array initializations here.
}
private void BuildDeck()
{
//lots of code assigning images to each individual element of the "deck" array. }
//many other methods that need to be called by a form.
}
}
That will not work or compile. In order to achieve what you want, you could have a constructor for ClsDeck and call BuildDeck
class ClsDeck {
private string[] deck = new string[52];
private string[] hand = new string[12];
ClsDeck() { //lots of code assigning images to each individual element of the "deck" array. }
//many other methods that need to be called by a form.
BuildDeck();
}
private void BuildDeck() {
//Build your deck
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am trying to learn programming and I am starting with a book called Software Development Fundamentals. However I am having loads of difficulty understanding certain subjects. Especially because my native language is not English. I am stuck at the subject (events) and (delegates). I feel like this is to difficult for me, I can not even get this code to work!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lesson02
{
class Program
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.Changed += new EventHandler(r_Changed);
r.Length = 10;
}
static void r_changed(object sender, EventArgs e)
{
Rectangle r = (Rectangle)sender;
Console.WriteLine(
"Value Changed: Length = {0}",
r.Length);
}
}
class Rectangle
{
public EventHandler Changed;
private double length;
public double Length
{
get
{
return length;
}
set
{
length = value;
Changed(this, EventArgs.Empty);
}
}
}
}
I get this error:
Error 1 The name 'r_Changed' does not exist in the current context 14 59 Lesson02
C# is case-sensitive language. You have defined function as r_changed and using it as r_Changed
Use
r.Changed += new EventHandler(r_changed);
instead of
r.Changed += new EventHandler(r_Changed);
I'm pretty sure you'd know by now that C# is a case sensitive programming language.
This should work
static void r_Changed(object sender, EventArgs e)
{
Rectangle r = (Rectangle)sender;
Console.WriteLine("Value Changed: Length = {0}", r.Length);
}
Notice how r_Changed is capitals (r_changed is what you originally defined)
I would suggest using this because it is easier to read.
There is a little typo mistake in your code. It should be r_Changed instead of r_changed in the your Event Handler.
i.e write
static void r_Changed(object sender, EventArgs e)
in place of
static void r_changed(object sender, EventArgs e)
(Remember C# is Case-sensitive)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am trying to convert vb .net code to c# but I am having problem with the following code:
Dim MI_Display_Channel As New MethodInvoker(AddressOf display_channel)
Private Sub display_channel()
TextBox1.Text = fv_channel
End Sub
How can I convert this piece of code into c#?
I see two answers machine translated where, apparently, a field initializer of an instance field, refers an instance member of the class. That is not allowed.
To be explicit:
class Xxx
{
MethodInvoker MI_Display_Channel = display_channel; // compile-time error!
void display_channel()
{
TextBox1.Text = fv_channel;
}
}
will not compile. When the field initialization is not allowed in a field initializer, use a constructor:
class Xxx
{
public Xxx() // other instance constructors may want to chain : this()
{
MI_Display_Channel = display_channel; // fine
}
MethodInvoker MI_Display_Channel; // no initializer here
void display_channel()
{
TextBox1.Text = fv_channel;
}
}
From here, you get this:
MethodInvoker MI_Display_Channel = new MethodInvoker(display_channel);
private void display_channel()
{
TextBox1.Text = fv_channel;
}
I'm not sure why it was difficult.