locked
i tried to convert this VB to C# but some prob ? RRS feed

  • Question

  • User346727691 posted

    hi there , yesterday i spend alot of time to understand and convert this article from vb to c#

    but some problem face me in C# code for the asp.net page code

    like

     

    //Determine which control fired the postback event. 
            Control c = GetPostBackControl(Page);
    
            if ((c != null))
            {
                //If the add button was clicked, increase 
                //the count to let the page know we want
                //to display an additional user control
                if (c.ID.ToString == "btnAdd")
                {
                    ltlCount.Text = Convert.ToInt16(ltlCount.Text) + 1;
                }
            }

     

    this part give me message said GetPostBackControl , doesn't exist  , what is GetPostBackControl should be in C# , or i miss something

     

    second problem

     

    WebUserControl DynamicUserControl = LoadControl("WebUserControl.ascx");

     i can't find the namespace for webusercontrol , i tried to see which namespace this class belong to , but its frustrated me so badly

     

     

    the third problem

     

    string[] DeletedList = ltlRemoved.Text.Split("|");
            for (int i = 0; i <= DeletedList.GetLength(0) - 1; i++)
            {
                if (ControlID.ToLower == DeletedList(i).ToLower)
                {
                    return true;
                }
            }
            return false;

     

    something wrong at the first line , is it should be like that with C#

     

    sorry for my long question my helpers , but am try to understand

    by the way here is the original code in VB at this lesson

     

    http://www.codeproject.com/KB/user-controls/DynamicUC.aspx

    Tuesday, May 10, 2011 7:39 AM

All replies

  • User-2139489267 posted

    First of all I would suggest that you download the code and use that vb code and convert it using this and if you still encounter problem do let me know.

    Tuesday, May 10, 2011 7:48 AM
  • User346727691 posted

    i already converted it , but i feel some objectives not declared right and i sent you the above code to make sure if this is right or wrong , what you suggest me to do , just fix for me this lines i sent and maybe i will look more information about each object is used in this lesson ok . thank you nilsan i hope we always be in connecting through this forum because am junior in this world of codes ok

     

    Tuesday, May 10, 2011 8:59 AM
  • User-1313990200 posted

    Convert using the tool and check once

    http://www.developerfusion.com/tools/convert/vb-to-csharp/

    Tuesday, May 10, 2011 9:09 AM
  • User397347636 posted

    I can see a few problems that the online converter should have caught for you:

    1. "c.ID.ToString" needs to change to "c.ID.ToString()"

    2. "ltlRemoved.Text.Split("|")" needs to change to "ltlRemoved.Text.Split('|')"

    3. "if (ControlID.ToLower == DeletedList(i).ToLower)" needs to change to "if (ControlID.ToLower() == DeletedList(i).ToLower())"

     

    Try a better converter - most of the online converters are not actively maintained - the problems I outlined above are some of the most trivial issues in VB to C# conversion.

     

    Tuesday, May 10, 2011 11:23 AM
  • User346727691 posted

    thank you for fixing but i correct most of them through searching for each object , but i really get lost with this few lines , i need extra help because i have no patient to search more by my self , i need experts to help me

    here is this line is have some problem when convert it from VB to C#

     

    //Add an event handler to this control to raise an event when the delete button is clicked
    //on the user control
    
    DynamicUserControl.RemoveUserControl +=this.HandleRemoveUserControl;
    
    	//Finally, add the user control to the panel
    	ph1.Controls.Add(DynamicUserControl);
    
    

    so i found on msdn that UserControl class is contain method called RemovedControl , but i really find it not exist in my visual studio 2008 , and here is the starting of instructing this object i called it DynamicUserControl

    

    UserControl DynamicUserControl =(UserControl)LoadControl("WebUserControl.ascx");
    

    i really tried to know why in visual basic we can instruct it as  WebUserControl , but in C# i can't find class called WebUserControl , but this work too i changed in visual basic from webusercontrol class to usercontrol and its work the same , but i can't use any method like RemovedUsercontrol

    i'm looking for idea about what i have face now , its messy codes

     

    Tuesday, May 10, 2011 6:44 PM