Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

C# Help: A local variable is already defined in this scope.?

I am brand new to C# programming. And I am struggling to figure out what I have done wrong.

The problem is in my string declaration for the variable that will be returned. The error I receive is "a local variable named 'seconds' is already defined in this scope. I haven't been able to come up with a way to solve this problem. I'm sure it's me being an idiot... but I can't find it.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace Mumby_iLab5

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

public void btnConvert_Click(object sender, EventArgs e)

{

int seconds = Convert.ToInt32(txtSeconds.Text);

this.lblOutputMinutes.Text = Minutes();

this.lblOutputHours.Text = Hours();

}

public string Minutes()

{

throw new NotImplementedException();

int seconds = Convert.ToInt32(txtSeconds.Text);

int minutes = (seconds / 60);

int secondsR = (seconds % 60);

string minutesText = "{0:n} second(s) is {1:n} minute(s) and {2} second(s).", seconds, minutes, secondsR; //this is where the error occurs

return minutesText;

}

public string Hours()

{

throw new NotImplementedException();

int seconds = Convert.ToInt32(txtSeconds.Text);

int hours = (seconds / 60) / 60;

int minutes = (seconds / 60) - 60;

int secondsR = seconds - (3600 * hours) - (60 * minutes);

string hoursText = "{0:n} second(s) is {1:n} hour(s), {2} minute(s) and {3} second(s).", seconds, hours, minutes, secondsR; //and also here

return hoursText;

}

private void btnClear_Click(object sender, EventArgs e)

{

//Clears text box and restores labels.

txtSeconds.Clear();

lblOutputMinutes.Text = "Seconds Converted to Minutes";

lblOutputHours.Text = "Seconds Converted to Hours";

}

private void btnExit_Click(object sender, EventArgs e)

{

//Terminates the program

this.Close();

}

}

}

2 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Looks pretty impressive for "brand new".

    You need to pass the format string and arguments to format to String.Format(). The first blowup should be:

    string minutesText = String.Format("{0:n} second(s) is {1:n} minute(s) and {2} second(s).", seconds, minutes, secondsR); //this is where the error occurs

    To C# the old version looked like (changing names and text):

    string var1="some text", var2, var3, var4;

    ...which declares 4 strings, but only initializes the first. So, it looked like you were re-declaring "seconds" and that's why you got the message.

  • ?
    Lv 4
    4 years ago

    the two. There are purposes that "come" with C++, and programmers may additionally define their very own. as an occasion the functionality sqrt() is predefined in C++ and provides the sqare root of the argument. yet you additionally can define a functionality like addition(int a,int b) and you're able to enforce so as that it provides a+b. void ability not something, you need to use to tell c++ that a functionality returns not something,(eg. a functionality that in basic terms prints to the demonstrate screen).

Still have questions? Get your answers by asking now.