For cycle always give same output [closed] - c#

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#.

Related

CS8321: The local function 'TextChange' is declared but never used [closed]

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 1 year ago.
Improve this question
please help me
public Text changingText;
int Cash = '0';
int CashMulti = '1';
int CashPerClick = '1';
public void OnMouseDown()
{
Cash = Cash + CashPerClick * CashMulti;
void TextChange()
{
changingText.text = "Cash";
}
}
The function should be declared outside the other function and then you can call it whenever you want.
public void OnMouseDown()
{
Cash = Cash + CashPerClick * CashMulti;
TextChange();
}
void TextChange()
{
changingText.text = "Cash";
}

Reading a text file with c# [closed]

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
So far this is my code. The problem I am encountering is that the file is not being found.
namespace Assignment_Forms_Aplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Define Variable
string[] words = new string[10];
// Read the text from the text file, and insert it into the array
StreamReader SR = new StreamReader(#"Library.txt");
//
for (int i = 0; i < 10; i++)
{
words[i] = SR.ReadLine();
}
// Close the text file, so other applications/processes can use it
SR.Close();
}
}
}
Hi Gailen use the following method:
File.ReadAllLines(#"location");
If location is correct then this will work
Assign to variable, for example

How to create thread according to integer value? [closed]

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 7 years ago.
Improve this question
I'm trying to create thread according to integer value. For example if the variable is '5', program should create 5 threads or variable is '2', program should create 2 threads, etc. But I can't understand which path I must follow.
It's just a matter of creating the Thread and start it. But I wouldn't suggest you to handle the thread explicitly, but to use Tasks or ThreadPool in order to execute multithreading work.
using System;
using System.Threading;
public class Program
{
public static void Main()
{
int numberOfRequestedThreads = 3;
for (int i = 0; i < numberOfRequestedThreads; i++)
{
var tempThread = new Thread(new ThreadStart(DoWork));
tempThread.Name = i.ToString();
tempThread.Start();
}
}
public static void DoWork()
{
Console.WriteLine("Thread#{0} is now working!", Thread.CurrentThread.Name);
}
}

C# Using for loops to process multiple elements in the WinForm Environment [closed]

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 9 years ago.
Improve this question
I am pretty new to C#, I have been searching for an hour or so now for what I need and can't find it. I am trying to look at the contents of 32 different text box's using for loops.
The code I have at the moment is:
private void btnCalculate_Click(object sender, EventArgs e)
{
string ElementString;
Control ElementControl;
double Num;
Boolean errorMsg = false;
for (int x = 1; x <= 4; x++)
for (int y = 1; y <= 4; y++)
{
ElementString = "txtA" + x.ToString() + y.ToString();
ElementControl = this.Controls[ElementString];
ElementString = ElementControl.Text.Trim();
if (!double.TryParse(ElementString, out Num))
{
errorMsg = true;
break;
}
}
if (errorMsg)
MessageBox.Show("Error Processing Input Matricies, invalid entries");
}
Ok changing this part, sorry for not putting more information, but hopefully this will help.
The program crashes when the button is clicked. It crashes when running the line:
ElementString = ElementControl.Text.Trim();
And gives the error message:
Object reference not set to an instance of an object.
Thank you
The first thing you need to do is check if ElementControl is null. Do that right before you call the line that is throwing the exception.
Another way to do this would be to search for all of the TextBox controls in your container and check them that way. You can use the following to do that:
foreach(Control c in this.Controls)
{
if (c is TextBox)
{
// Do whatever you want to do with your textbox.
}
}
That is much more dynamic than trying to call retrieve the controls by name.
put an if statement around the part where it crashes:
if(ElementControl != null)
{
ElementString = ElementControl.Text.Trim();
}
else
{
//Handle error if element control is null
}

How to add values per character in c#? [closed]

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 8 years ago.
Improve this question
I am new to c# and do not know how to explain this but I wanted to know if it was possible to add a charge for every character that is entered into a text box.
For example if the customer was to enter "HELLO HI" they should be charged £5 with a additional cost of £1 per letter or number this £1 charge should also apply for any spaces entered.
Sorry if I have not explained this properly but this is the best way I can.
Thank you
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int amount;
private void textBox1_TextChanged(object sender, EventArgs e)
{
amount = 5;
amount = amount + textBox1.Text.Length;
label1.Text = amount.ToString();
}
}
}
Well, it pretty much looks like this, I guess:
string str = "Happy Birthday";
int price = str.Length + 5;
Find the length of entered string in textbox using TextboxID.text.Length and add your fix charge that 5 , as ALEX explain you.
please see alex's answer.

Categories