help with following code

D

-D-

I'm new to .net and just learning c#.

I have the following code in vb and want to convert this to c#

Public Class PositionData

Private strText As String
Private strUrl As String

Public Sub New(ByVal strDisplayText As String, ByVal strURL As String)
Me.strText = strDisplayText
Me.strUrl = strURL
End Sub

Public ReadOnly Property DisplayText() As String
Get
Return strText
End Get
End Property

Public ReadOnly Property URL() As String
Get
Return strUrl
End Get
End Property
End Class

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim MenuItems As New ArrayList

MenuItems.Add(New PositionData("About Us", "About.aspx"))
MenuItems.Add(New PositionData("Contact Us", "Contact.aspx"))
MenuItems.Add(New PositionData("Customer Support", "Support.aspx"))
MenuItems.Add(New PositionData("Site Map", "sitemap.aspx"))

Repeater1.DataSource = MenuItems
Repeater1.DataBind()
End Sub

Any help is appreciated.
 
M

Mike D Sutton

I'm new to .net and just learning c#.
I have the following code in vb and want to convert this to c#
Any help is appreciated.

The code is pretty similar in either language:

'*** Untested:
public class PositionData {
private string strText;
private string strUrl;

public PositionData(string strDisplayText, string strURL) {
this.strText = strDisplayText;
this.strUrl = strURL;
}

public string DisplayText {
get { return strText; }
}

public string URL {
get { return strUrl; }
}
}

private void Page_Load(object sender, EventArgs e) {
ArrayList MenuItems = new ArrayList();

MenuItems.Add(new PositionData("About Us", "About.aspx"));
MenuItems.Add(new PositionData("Contact Us", "Contact.aspx"));
MenuItems.Add(new PositionData("Customer Support", "Support.aspx"));
MenuItems.Add(new PositionData("Site Map", "sitemap.aspx"));

Repeater1.DataSource = MenuItems;
Repeater1.DataBind();
}
'***

Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: (e-mail address removed)
WWW: Http://EDais.mvps.org/
 
I

Ian Semmel

The first function is

public class PositionData
{
private string strText;
private string strUrl;

public PositionData ( string strDisplayText, string strURL )
{
this.strText = strDisplayText; // this. is not required
strUrl = strURL;
}

public string DisplayText
{
get
{
return strText;
}
}

// ditto other property
}

I'll leave the second function to you or someone else
 
D

-D-

Thanks Mike...I appreciate your help. That got it working.

If I may ask a couple of follow-up questions.

Here is how my code-behind looks:

namespace compass.user_controls

{

using System;

using System.Collections;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

/// <summary>

/// Summary description for TopNavBar.

/// </summary>

public class TopNavBar : System.Web.UI.UserControl

{

protected System.Web.UI.WebControls.Repeater Repeater1;


public class PositionData

{

private string strText;

private string strUrl;

public PositionData(string strDisplayText, string strURL)

{

this.strText = strDisplayText;

this.strUrl = strURL;

}

public string DisplayText

{

get { return strText; }

}

public string URL

{

get { return strUrl; }

}

}



private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here

ArrayList MenuItems = new ArrayList();

MenuItems.Add(new PositionData("About Us", "About.aspx"));

MenuItems.Add(new PositionData("Contact Us", "Contact.aspx"));

MenuItems.Add(new PositionData("Customer Support", "Support.aspx"));

MenuItems.Add(new PositionData("Site Map", "sitemap.aspx"));

Repeater1.DataSource = MenuItems;

Repeater1.DataBind();



}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: This call is required by the ASP.NET Web Form Designer.

//

InitializeComponent();

base.OnInit(e);

}


/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

}

}



forgive my novice quesitons, but first, is there a better way to write this
code? Should I create a class file instead with the property definitions
and method? What that be cleaner or a better way to code this?

why does a new class have to be added for the PositionData method and
properties? I thought I could have used the public class TopNavBar? Or, do
I need to create a new class for every new object that I create?



public class TopNavBar : System.Web.UI.UserControl

{

protected System.Web.UI.WebControls.Repeater Repeater1;


public class PositionData

{

private string strText;

private string strUrl;

public PositionData(string strDisplayText, string strURL)

{

this.strText = strDisplayText;

this.strUrl = strURL;

}

public string DisplayText

{

get { return strText; }

}

public string URL

{

get { return strUrl; }

}



finally, instead of populaing the arraylist with hardcoded values, I want
populate the values from a database. That is my next step. I assume I
should be using the databind in place of the hard-coded values and a
function that will return the records from the database.

Thanks again.

Regards,

-D-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top