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
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.
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.
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.
@ 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.
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?
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.
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".
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);
}
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);
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With