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

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";
}

Related

For cycle always give same output [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 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#.

Unreachable Code - Anything put inside if statement [closed]

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

how to call a method in another method with return? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
I am relatively new to c# programming. I am working with Forms and I want to print a value in text box which is not working. I am getting an error "not all code paths return a value"
public void button1_Click(object sender, EventArgs e)
{
double res = test();
tbox.Text = res.ToString();
}
public double test()
{
if (Cbtest.Checked == false)
{
return 10 + 5.1;
}
}
The issue is with the test method, you need to consider the Cbtest.Checked==true condition as well otherwise your code will raise error as "not all code paths return a value", so better you change the signature as like the following:
public double test()
{
if (!Cbtest.Checked)
{
return 10 + 5.1;
}
return 0.0; // or some other values
}
Try to return something if your box is checked:
public double test()
{
if (Cbtest.Checked == false)
{
return 10 + 5.1;
}
return -1.0;
}
Your method always needs to have a return value, even if you know that condition will be false, compiler doesn't.

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