• 0

[C#] Overloaded Big Function..


Question

most examples i find of Overloaded functions are small simple things like..

public int Add(int a, int b)
{
  return a+b;
}

public int Add(int a, int b, int c)
{
  return a+b+c;
}

but.. i want to do this for a bigger function (~10 lines of code) without adding a lot of additional lines... is there any possible way to do it?

I want something like this..

public void Log(string msg);
public void Log(string msg, Color foreground);
public void Log(string msg, Color foreground, Color background);

public void Log(string msg, bool showTimeStamp);
public void Log(string msg, Color foreground, bool showTimeStamp);
public void Log(string msg, Color foreground, Color background, bool showTimeStamp);

but at ~10 lines of code each, thats easily 60 lines of code..

any ideas?

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

Are you asking if it is possible to have more than ~10 lines of code in each overloaded method? Yes, that's possible. Each method is different and executes independent of each other.

If you are worried about repetition of code, you can simply call one of the overloads and not repeat its code.

public int Add(int a, int b)
{
  return a+b;
}

public int Add(int a, int b, int c)
{
  return Add(a, b) + c;
}

Link to comment
Share on other sites

  • 0

It really depends on how well the functions depend on each other.

This is an example of constructors I use in one of the class library.

public FromTable(string ATableName) : this(ATableName, String.Empty) { }   // FromTable
public FromTable(string ATableName, string ATableAlias) : this(null, ATableName, ATableAlias) { }   // FromTable
public FromTable(MaxFromClause AParent, string ATableName) : this(AParent, ATableName, String.Empty) { }   // FromTable
public FromTable(MaxFromClause AParent, string ATableName, string ATableAlias)
{
	// Basic error checking
	if (String.IsNullOrEmpty(ATableName))
	{
		throw new ApplicationException(this.ToString() + " - TableName can't be null or empty.");
	}

	// Save reference to parent
	FParent = AParent;
	FTableName = ATableName;
	FTableAlias = ATableAlias;

	// Add to parent list if required
	if (AParent != null)
	{
		FParent.AddToList(this);
	}
}   // FromTable

Another example of what I am talking about is below.

internal string BuildQuery()
{
	return BuildQuery(false);
}   // BuildQuery

internal string BuildQuery(bool AIsFirstCondition)
{
	StringBuilder sQuery;

	sQuery = new StringBuilder();

	if (FStartGroup)
	{
		if (!FLogicalOperator.Equals(MaxLogicalOperator.loNone))
		{
			switch (FLogicalOperator)
			{
				case MaxLogicalOperator.loAnd:
					sQuery.Append(" AND ");

					break;

				case MaxLogicalOperator.loOr:
					sQuery.Append(" OR ");

					break;
			}
		}

		sQuery.Append("(");
	}
	else if (FEndGroup)
	{
		sQuery.Append(")");
	}
	else
	{
		if (!AIsFirstCondition)
		{
			switch (FLogicalOperator)
			{
				case MaxLogicalOperator.loAnd:
					sQuery.Append(" AND ");

					break;

				case MaxLogicalOperator.loOr:
					sQuery.Append(" OR ");

					break;
			}
		}

		sQuery.Append(FFieldName);

		switch (FComparisonOperator)
		{
			case MaxComparisonOperator.coBetween:
				sQuery.Append(" BETWEEN ");
				sQuery.Append(FFieldValue);
				sQuery.Append(" AND ");
				sQuery.Append(FFieldValue2);

				break;

			case MaxComparisonOperator.coEquals:
				sQuery.Append(" = ");
				sQuery.Append(FFieldValue);

				break;

			case MaxComparisonOperator.coGreaterOrEquals:
				sQuery.Append(" >= ");
				sQuery.Append(FFieldValue);

				break;

			case MaxComparisonOperator.coGreaterThan:
				sQuery.Append(" > ");
				sQuery.Append(FFieldValue);

				break;

			case MaxComparisonOperator.coIn:
				sQuery.Append(" IN ");
				sQuery.Append(FFieldValue);

				break;

			case MaxComparisonOperator.coLessOrEquals:
				sQuery.Append(" <= ");
				sQuery.Append(FFieldValue);

				break;

			case MaxComparisonOperator.coLessThan:
				sQuery.Append(" < ");
				sQuery.Append(FFieldValue);

				break;

			case MaxComparisonOperator.coLike:
				sQuery.Append(" LIKE ");
				sQuery.Append(FFieldValue);

				break;

			case MaxComparisonOperator.coNotBetween:
				sQuery.Append(" NOT BETWEEN ");
				sQuery.Append(FFieldValue);
				sQuery.Append(" AND ");
				sQuery.Append(FFieldValue2);

				break;

			case MaxComparisonOperator.coNotEquals:
				sQuery.Append(" <> ");
				sQuery.Append(FFieldValue);

				break;

			case MaxComparisonOperator.coNotIn:
				sQuery.Append(" NOT IN ");
				sQuery.Append(FFieldValue);

				break;

			case MaxComparisonOperator.coNotLike:
				sQuery.Append(" NOT LIKE ");
				sQuery.Append(FFieldValue);

				break;
		}
	}

	return sQuery.ToString();
}   // BuildQuery

Edited by wrack
Link to comment
Share on other sites

  • 0

public void Log(string message) {
	Log(message, null, LogSeverity.Debug);
}

public void Log(string message, string category) {
	Log(message, category, LogSeverity.Debug);
}

public void Log(string message, string category, LogSeverity severity) {
	if (message == null) {
		throw new ArgumentException("message");
	}

	StringBuilder builder = new StringBuilder();

	builder.AppendFormat(CultureInfo.CurrentCulture, "Severity: {0}", severity);

	if (!string.IsNullOrEmpty(category)) {
		builder.AppendFormat(CultureInfo.CurrentCulture, "\nCategory: {0}", category);
	}

	builder.AppendFormat("\n\nMessage: {0}", message);

	Console.Write(builder);
}

There are a few things to consider really. Do you need each one to specifically handle your parameters, or can you safely hand them to another overloaded function to do the processing for you?

In the above example, I can safely pass my parameters down to the most specific overload and do any validation in that method.

The simpler overloads can then pass any default values to the more specific overloads as required.

Link to comment
Share on other sites

  • 0

Another example!

public static string ToString(bool AValue)
{
	return ToString(AValue, ReturnValueType.OneZero);
}   // ToString

public static string ToString(bool? AValue)
{
	if (AValue.HasValue)
	{
		return ToString(AValue.Value, ReturnValueType.OneZero);
	}
	else
	{
		return null;
	}
}   // ToString

public static string ToString(bool? AValue, ReturnValueType AReturnValueType)
{
	if (AValue.HasValue)
	{
		return ToString(AValue.Value, AReturnValueType);
	}
	else
	{
		return null;
	}
}   // ToString

public static string ToString(bool AValue, ReturnValueType AReturnValueType)
{
	if (AValue)
	{
		switch (AReturnValueType)
		{
			case ReturnValueType.OneZero:
				return "1";

			case ReturnValueType.YesNo:
				return "Yes";

			case ReturnValueType.TrueFalse:
				return "True";

			case ReturnValueType.OnOff:
				return "On";

			default:
				return "1";
		}
	}
	else
	{
		switch (AReturnValueType)
		{
			case ReturnValueType.OneZero:
				return "0";

			case ReturnValueType.YesNo:
				return "No";

			case ReturnValueType.TrueFalse:
				return "False";

			case ReturnValueType.OnOff:
				return "Off";

			default:
				return "0";
		}
	}
}   // ToString

Link to comment
Share on other sites

  • 0
... pass my parameters down to the most specific overload and do any validation in that method.

The simpler overloads can then pass any default values to the more specific overloads as required.

This.

@OP: For your Log() example, all the code would be in your Log(string msg, Color fg, Color bg, bool showTimeStamp). The shorter overloads would call this long method and pass default values along.

E.g.

void Log(string msg, Color fg)
{
  Log(msg, fg, Color.White, false);
}

Link to comment
Share on other sites

  • 0

Luckily, C# 4.0 will have default parameters like C++, so we won't have to write all this crap. So, Antaris' example could be rewritten as :

// This is C# 4.0! It doesn't compile in VS2008!
public void Log(string message, string category = null, LogSeverity severity = Severity.Debug) {
	if (message == null) {
		throw new ArgumentException("message");
	}

	StringBuilder builder = new StringBuilder();

	builder.AppendFormat(CultureInfo.CurrentCulture, "Severity: {0}", severity);

	if (!string.IsNullOrEmpty(category)) {
		builder.AppendFormat(CultureInfo.CurrentCulture, "\nCategory: {0}", category);
	}

	builder.AppendFormat("\n\nMessage: {0}", message);

	Console.Write(builder);
}

And then the method can be called any way you like:

Log("Hello!");
Log("Hello!", "SuperLogs");
Log("Hello!", "SuperLogs", Severity.Horrible);
Log("Hello!", severity = Severity.Horrible); // Uses named parameter

Coming in 2010. :)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.