-4

I have done the code but is unable for the webpage to compute it does everything else except for computing.

protected void btnCompute_Click(object sender, EventArgs e)
{
    string gradebook,
        article,
        homework,
        practice,
        total;

    Double IndividualRate = .40,
        ArticleRate = .10,
        HomeworkRate = .25,
        PracticeRate = .25;

    string individualamt,
        articleamt,
        homeworkamt,
        practiceamt;

    gradebook = Convert.ToString(txtindividualproject.Text);
    homework = Convert.ToString(txtHomeworkAssignements.Text);
    article = Convert.ToString(txtArticleReview.Text);
    practice = Convert.ToString(txtPracticeAssignment.Text);

    individualamt = gradebook * IndividualRate;
    articleamt = article * ArticleRate;
    homeworkamt = homework * HomeworkRate;
    practiceamt = practice * PracticeRate;

    lbltotal.Text = individualamt + articleamt + homeworkamt + practiceamt;

    lbltotal.Visible = true;

I am trying to compute the overall total.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Joe
  • 5
  • 1
  • 2
    `Convert.ToString(txtXxxxXxxx.Text);` seems to be converting your string to a string. You could try `Convert.ToDouble(txtXxxxXxxx.Text);` to try to convert to `Double`. If it throws an error, then the text couldn't be parsed to `Double` – Jaskier Feb 04 '19 at 20:25
  • 2
    You don't need to convert the `Text` property of a `TextBox` to a `string` because it already is one. You might try starting by replacing `Convert.ToString` with `double.Parse`. Also take a look at [double.TryParse](https://learn.microsoft.com/en-us/dotnet/api/system.double.tryparse?view=netframework-4.7.2) to avoid errors when the input is not a valid `double`. – Rufus L Feb 04 '19 at 20:26
  • 1
    Possible duplicate of [Double.TryParse or Convert.ToDouble - which is faster and safer?](https://stackoverflow.com/questions/586436/double-tryparse-or-convert-todouble-which-is-faster-and-safer) – mjwills Feb 04 '19 at 20:32
  • Please tell us the **exact** values that have been typed into every textbox. Also please let us know the value you **expect** to show in `lbltotal`, and the actual value being shown in `lbltotal`. Also, please put a breakpoint inside of `btnCompute_Click` - is the breakpoint getting hit? – mjwills Feb 04 '19 at 20:56

2 Answers2

-1

OOPS SORRY that was the wrong code ...

protected void btnCompute_Click(object sender, EventArgs e) { double gradebook, article, homework, practice, total,

        IndividualRate = .40,
        ArticleRate = .10,
        HomeworkRate = .25,
        PracticeRate = .25,
        individualamt ,
        articleamt,
        homeworkamt,
        practiceamt;

    gradebook = Convert.ToDouble(txtindividualproject.Text);
    homework = Convert.ToDouble(txtHomeworkAssignements.Text);
    article = Convert.ToDouble(txtArticleReview.Text);
    practice = Convert.ToDouble(txtPracticeAssignment.Text);


    individualamt = gradebook * IndividualRate;
    articleamt = article * ArticleRate;
    homeworkamt = homework * HomeworkRate;
    practiceamt = practice * PracticeRate;

    total = individualamt + articleamt + homeworkamt + practiceamt;

    lbltotal.Visible = true;
    lbltotal.Text = total.ToString("c");




}
Joe
  • 5
  • 1
-1

@Joe, Welcome to Stack Overflow (SO).

First, you are using Convert.ToString() when you should be using Convert.ToDouble(). However, I can't really suggest using Convert.ToDouble() either.

The problem with Convert.ToDouble() or any method in the System.Convert class is that it is not exception safe, meaning that it doesn't handle exceptions. If you try Convert.ToDouble("a"), it throws an exception. It does not allow for a default value if the value isn't a number, etc.

So there is Double.TryParse. That is much more safe.

    if (!double.TryParse(txtindividualproject.Text, out gradebook)))
        // Do something on false

The code gets ugly and verbose at this point. That is why I created my own string library called: Rhyous.StringLibrary. Actually, there a ton of NuGet packages out there just to make strings easier. Many poeple have their own string library or extensions because they write the same code above repeatedly and are annoyed by it.

You can use the above code, or you could use the Rhyous.StringLibrary NuGet package or consume the class file directly in your project. Here is a link. https://github.com/rhyous/StringLibrary/blob/master/src/Rhyous.StringLibrary.Shared/Conversion/PrimitiveStringExtensions.cs

Also, since SO likes the code posted, not just linked to, here it is:

using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;

namespace Rhyous.StringLibrary
{
    public static class PrimitiveStringExtensions
    {
        public static T To<T>(this string s, T defaultValue = default(T))
            where T : IComparable, IComparable<T>, IEquatable<T>
        {
            TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
            try { return (T)converter.ConvertFromString(null, CultureInfo.InvariantCulture, s); }
            catch { return defaultValue; }
        }

        public static object ToType(this string s, Type type, object defaultValue = null)
        {
            MethodInfo mi = null;
            MethodInfo method = null;
            if (type.IsEnum)
            {
                mi = typeof(StringEnumExtensions).GetMethods().FirstOrDefault(m => m.Name == "ToEnum" && m.GetParameters().Length == 4);
                method = mi.MakeGenericMethod(type);
                var defaultEnum = Enum.GetValues(type).GetValue(0);
                return method.Invoke(null, new object[] { s, defaultEnum, true, true });
            }
            mi = typeof(PrimitiveStringExtensions).GetMethod("To");
            method = mi.MakeGenericMethod(type);
            // Check if it is DateTime to handle this critical .NET bug
            // https://connect.microsoft.com/VisualStudio/feedback/details/733995/datetime-default-parameter-value-throws-formatexception-at-runtime
            if (type == typeof(DateTime))
                defaultValue = DateTime.MinValue;
            return method.Invoke(null, new object[] { s, defaultValue ?? Type.Missing });
        }

        public static byte ToByte(this string s, byte defaultValue = 0)
        {
            return To(s, defaultValue);
        }

        public static bool ToBool(this string s, bool defaultValue = false)
        {
            return To(s, defaultValue);
        }

        public static DateTime ToDate(this string s, DateTime defaultValue = default(DateTime))
        {
            return To(s, defaultValue);
        }

        public static decimal ToDecimal(this string s, decimal defaultValue = 0.0M)
        {
            return To(s, defaultValue);
        }

        public static double ToDouble(this string s, double defaultValue = 0.0D)
        {
            return To(s, defaultValue);
        }

        public static float ToFloat(this string s, float defaultValue = 0.0F)
        {
            return To(s, defaultValue);
        }

        public static int ToInt(this string s, int defaultValue = 0)
        {
            return To(s, defaultValue);
        }

        public static long ToLong(this string s, long defaultValue = 0L)
        {
            return To(s, defaultValue);
        }

        public static sbyte ToSByte(this string s, sbyte defaultValue = 0)
        {
            return To(s, defaultValue);
        }

        public static short ToShort(this string s, short defaultValue = 0)
        {
            return To(s, defaultValue);
        }

        public static uint ToUint(this string s, uint defaultValue = 0U)
        {
            return To(s, defaultValue);
        }

        public static ulong ToULong(this string s, ulong defaultValue = 0UL)
        {
            return To(s, defaultValue);
        }

        public static ushort ToUShort(this string s, ushort defaultValue = 0)
        {
            return To(s, defaultValue);
        }

        public static Guid ToGuid(this string s, Guid defaultValue = default(Guid))
        {
            return To(s, defaultValue);
        }
    }
}

You would use that class like this:

gradebook = txtindividualproject.Text.To<double>(0.0);
homework = txtHomeworkAssignements.Text.To(0.0);
article = txtArticleReview.Text.To(0.0);
practice = txtPracticeAssignment.Text.To<double>(0.0);

As you see, the code you get from Rhyous.StringLibarary handles exceptions, gives a default value, and is easy to use, and succinct and not verbose, and very readable.

Rhyous
  • 6,510
  • 2
  • 44
  • 50