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