I have some C# code to add to an existing VB.net project.
The C# class is designed as a html parser
Have initally used an online converter and was able to get most of the class working, but the piece below is still not functioning. Unfortunately I lack in the knowledge to fix this.
I am posting the whole piece, but if someone can clarify the first couple of lines, I assume that would be enough.
AttributeNameValuePair is a separate class that holds the attribute.
Further down some inline functions are used, would appreciate an example of that aswell. Or would it be easier to make these as separate functions and leave only a reference inside?
Thanks beforehand for any assistance.
private readonly Dictionary<string, Action<DocumentModel, IEnumerable<AttributeNameValuePair>>> commandsDictionary = new Dictionary<string, Action<DocumentModel, IEnumerable<AttributeNameValuePair>>>()
{
{ "b", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => GetLastRun(doc).CharacterFormat.Bold = true) },
{ "i", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => GetLastRun(doc).CharacterFormat.Italic = true) },
{ "u", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => GetLastRun(doc).CharacterFormat.UnderlineStyle = UnderlineType.Single) },
{ "strike", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => GetLastRun(doc).CharacterFormat.Strikethrough = true) },
{ "sub", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => GetLastRun(doc).CharacterFormat.Subscript = true) },
{ "sup", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => GetLastRun(doc).CharacterFormat.Superscript = true) },
{ "div", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) =>
{
foreach(var arg in args)
{
if(arg.AttributeName == "align")
{
HorizontalAlignment align;
switch(arg.AttributeValue)
{
case "center":
align = HorizontalAlignment.Center;
break;
case "right":
align = HorizontalAlignment.Right;
break;
case "justify":
align = HorizontalAlignment.Justify;
break;
default:
align = HorizontalAlignment.Left;
break;
}
}
}
})},
{ "br", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => doc.Sections[0].Blocks.Add(new Paragraph(doc))) },
{ "span", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => {})},
{ "font", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) =>
{
foreach(AttributeNameValuePair arg in args)
{
int? size = null;
string fontName = null;
// Some dummy values.
if (arg.AttributeName == "size")
size = 10 + 3 * int.Parse(arg.AttributeValue);
else if (arg.AttributeName == "face")
fontName = arg.AttributeValue.Split(',').First();
var lastFormat = GetLastRun(doc).CharacterFormat;
if (size.HasValue)
lastFormat.Size = size.Value;
if (fontName != null)
lastFormat.FontName = fontName;
}
})},
};
Which version of vb.net is this? Lambda expressions work a bit differently in each of them. Multiline lambda is only supported in VS2010+. For VS2008 you usually end up having to turn anonymous methods into real methods and reference them via
AddressOf. Automatic translation, I’ve noticed, generally seems to fail when converting anonymous/lambda type expressions from C#->VB.In any case, anonymous method syntax in VB is a bit clumsier and different enough, compared to C#, to be confusing. For the single line methods you would do something like this (I’m using simple types for clarity) :
For multiline (in vs2010) the pattern looks like: