C# Syntax Error Passing Web Control Text Property To Class Method Expecting Text

David Bach
David Bach used Ask the Experts™
on
Greetings:

I am converting VB.NET code to c# for ASP.NET web page code behind. I am learning c#.

Below is the aspx page, the aspx code behind and the invoked class method.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestIt.aspx.cs" Inherits="TestIt" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
	<title></title>
</head>
<body>
	<form id="form1" runat="server">
		<asp:Label ID="lblString" runat="server" Text="Some string"></asp:Label>
	</form>
</body>
</html>

Open in new window

using BaBUtilityCSharp;
using System;

public partial class TestIt : System.Web.UI.Page
{
	protected StringMassage sm = new StringMassage();

	protected void Page_Load(object sender, EventArgs e)
	{
		lblString.Text = sm.HtmlLineBreak(lblString.Text) +
						 " More string";
	}

}

Open in new window

		public string HtmlLineBreak(ref string strData)
		{
			if (strData.Length > 0)
				return strData + "<br />";

			return strData;
		}

Open in new window

I'm receiving a syntax error within Visual Studio Pro 2019 as follows.
 Error passing the text property of a web control to a class method
What is the correct c# syntax to pass a web control's text property to a class method please?

Much thanks,
David Bach
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Engineer
Top Expert 2011
Commented:
I think the syntax error is because you are passing in  reference to the Text property of the web control rather than the value of the text property.
There is no reason I can see why you need to pass the value by ref, and you also wouldn't use the return keyword if passing by ref as the original would be updated.

Change the class method to remove the ref keyword from the parameters list, by doing this the class method is just expecting a string value in which is then modified by the code appropriately and then returned to the calling code using the Return keyword.

public string HtmlLineBreak(string strData)
		{
			if (strData.Length > 0)
				return strData + "<br />";

			return strData;
		}

Open in new window



When calling the class method use the .ToString() method to pass in the value of the text property of the web control, this ensures that a string value os passed into the Class method rather than an object reference:

using BaBUtilityCSharp;
using System;

public partial class TestIt : System.Web.UI.Page
{
	protected StringMassage sm = new StringMassage();

	protected void Page_Load(object sender, EventArgs e)
	{
		lblString.Text = sm.HtmlLineBreak(lblString.Text.ToString()) +
						 " More string";
	}

}

Open in new window

David BachPartner

Author

Commented:
Paul:

THANK YOU!!!

The VB code has ByRef so naturally I wrote the c# counterpart using ref.

Slowly buy surely I will be become accustomed to the little nuances of c# vs VB.

Thank you again, Paul!!!
Paul JacksonSoftware Engineer
Top Expert 2011

Commented:
No problem glad I could help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial