Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make this VB code work in C# using the same variable?

I'm trying to figure out how to convert the following sample code to C# using the same implicit define as VB. I know I can define the button and generic control as two objects and make it work, but I would like to use the same variable "ctlHTML" in C# just like VB works. Can someone help on this?

Sub MySub(varInput As String, pnl As Panel)
    Dim ctlHTML = Nothing
    Select Case varInput
        Case "btn"
            ctlHTML = New HtmlButton
        Case "lbl"
            ctlHTML = New HtmlGenericControl()
    End Select
    With ctlHTML
        .Style.Add("font-size", "14px")
    End With
    pnl.Controls.Add(ctlHTML)
End Sub
like image 550
user2360313 Avatar asked May 07 '13 23:05

user2360313


People also ask

Can I use Visual Studio code for C?

C/C++ support for Visual Studio Code is provided by a Microsoft C/C++ extension to enable cross-platform C and C++ development on Windows, Linux, and macOS.

How do I run a Visual Studio program in C?

After writing the code, right-click on the program, as shown below. Click on the Run Code option or press Ctrl + Alt + N from the button.

Can I use VB.NET and C# in same project?

Answers. Hi, you cannot use C# and VB in the same project, but you can use C# projects and VB projects in the same solution. I would suggest breaking up the work into seperate projects, but ideally you would both use the same language which would make working on eachothers code a lot easier.

Can I convert from VB to C?

@ cpallini. Can you kindly share me the link for converting from VB to C. I can also resolve the bugs after conversion. There is no link to share. Converting VB to C is feasible, however you must know both the languages.

How do I write whatisyourname code in Visual Basic?

In the WhatIsYourName project, enter the following Visual Basic code in the Program.vb file immediately after the opening bracket that follows the Sub Main (args As String ()) line and before the End Sub line: Console.WriteLine (vbCrLf + "What is your name?

How do I create a Visual Basic project in Visual Studio?

Open Visual Studio. On the start window, choose Create a new project. In the Create a new project window, choose Visual Basic from the Language list. Next, choose Windows from the Platform list and Console from the project types list.

What is Visual Basic?

Visual Basic is a type-safe programming language that's designed to be easy to learn. It is derived from BASIC, which means "Beginner's All-purpose Symbolic Instruction Code".


Video Answer


3 Answers

You won't be able to directly convert the code. You'll have to give your variable a type. The class highest in the inheritance chain that supports all of your members would be HtmlControl:

HtmlControl control = null;
switch(varInput)
{
    case "btn":
        control = new HtmlButton();
        break;
    case "lbl":
        control = new HtmlGenericControl();
        break;
}

if(control != null)
{
    control.Style.Add("font-size", "14px");
    pnl.Controls.Add(control);
}
like image 191
Justin Niessner Avatar answered Nov 03 '22 01:11

Justin Niessner


In order to modify the Style property, the control at minimum needs to be an HtmlControl. So you need to declare the variable ctlHtml as that type.

You should also check to make sure the ctlHtml is properly initialized.

I believe your code should look something like this:

public void MySub(string varInput, Panel pnl)
{
    HtmlControl ctlHtml;
    switch(varInput)
    {
        case "btn":
            ctlHtml = new HtmlButton();
            break;
        case "lbl":
            ctlHtml = new HtmlGenericControl();
            break;
        default:
            ctlHtml = null;
            break;
    }
    if (ctlHtml != null)
    {
        ctlHtml.Style.Add("font-size", "14px");
        pnl.Controls.Add(ctlHtml);
    }
}
like image 21
p.s.w.g Avatar answered Nov 03 '22 00:11

p.s.w.g


Since HtmlGenericControl and HtmlButton both inherit from HtmlControl, you can declare ctlHTML as that type (HtmlControl) and it will work.

See here: http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlcontrol.aspx

like image 24
canhazbits Avatar answered Nov 03 '22 01:11

canhazbits