MonoDroid GetSpans last parameter - c#

I'm trying to use ISpannable in a Monodroid project but am having problems with GetSpans. What I'm ultimately after is a Rich Text editor such as at:
https://code.google.com/p/android-richtexteditor/source/browse/?r=4#svn/trunk/src/net/sgoliver
However, the Xamarin documentation for GetSpans isn't particularly helpful. The line I'm trying to convert from Java to C# is:
StyleSpan[] ss = s.getSpans(styleStart, position, StyleSpan.class);
However, I don't know what to pass for the last parameter as writing StyleSpan.class in C# gives a compile error of "} expected". What can I pass in to the last parameter to get all spans, or all spans of a particular type?

The C# equivalent should be Java.Lang.Class.FromType(typeof(StyleSpan)).

Related

diff_match_patch google library is not working as expected in C#

I am writing the text change tracking add-in and my code is:
mDiffMatchPatch = new diff_match_patch()
List<Diff> diffList = mDiffMatchPatch.diff_main(OriginalText, ModifiedText);
mDiffMatchPatch.diff_cleanupSemantic(diffList);
Where Diff is an object with two properties, 'text' and 'operation'
My original text is :
When I just insert the word 'ice' after 'installer' is shows up as it should:
But when I highlight 'installer' and type 'ice' on top of it (replacing 'installer' by 'ice' in one operation), it shows incorrectly.
That is, it takes the first letter of original 'installer' and separates 'i' from 'ce' in 'ice'.
I checked and re-rechecked and my text color rendering is correct. It is the diff_match_match breaks the 'i' from 'ice' in order to match it with the 'i' of 'installer'.
Is there any remedy for this? If not, is there alternative libraries in C# for diff_match_patch?
M
I resolved it by following the guidance on project github page. That is, using the word-based diff function of the diff_match_patch object. You create it by copying line-based function and modifying it per article.

Alternative to out variables

I need to include double.TryParse(wordConf, out double wordConfDouble); in a script, but I get a feature out variable declaration is not available in c# 6 error message. Searching for it on Google, I can only see solutions for upgrading to C# 7 (which I am not allowed to do so in this project) so I wonder if someone could help me write an equivalent to this line that would work in any C# compiler.
You don't need to inline declare a type for out-parameters.
Replace:
double.TryParse(wordConf, out double wordConfDouble);
With:
double wordConfDouble;
double.TryParse(wordConf, out wordConfDouble);
It's just the inline declaration which is not supported in < C#7.0. Change your code to
double wordConfDouble;
double.TryParse(wordConf, out wordConfDouble);
Reference: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7#out-variables

Calling functions in razorengine

I am using http://antaris.github.io/RazorEngine/ to generate strings from a template and am having trouble calling functions within the template. The documentation on the site is a bit lacking with examples, but I am trying to call the double ToString function with a format. The format string is:
"The value of sensor #Model.Measurement.Sensor.Description is out of spec with a value of ((double)#Model.AdjustedValue).ToString(\"#.##\")"
The last part of the string I would like the output to be rounded to the nearest hundredth, but instead of I am getting the following string:
"The value of sensor Test_sensor_1 is out of spec with a value of ((double)78.14215625).ToString("#.##")"
Could someone point out the correct syntax for what I am trying to achieve.
Your # is in the wrong place, I think:
"The value of sensor #Model.Measurement.Sensor.Description is out of spec with a value of #(((double)Model.AdjustedValue).ToString(\"#.##\"))"

Tried to convert VB code to C# but error occurred

I did convert the VB code to below C# code, but error occurred. I am getting error in Strings.InStr and Strings.Mid.
sHeadingNm = ActiveDocument.Styles(wdStyleHeading1).NameLocal;
if (!string.IsNullOrEmpty(sHeadingNm)) {
nPos = Strings.InStr(1, sHeadingNm, "1");
if (nPos > 0)
sHeadingNm = Strings.Mid(sHeadingNm, 1, nPos - 1);
}
//=======================================================
//Service provided by Telerik (www.telerik.com)
//Conversion powered by NRefactory.
//Twitter: #telerik
//Facebook: facebook.com/telerik
//=======================================================
Please help me...
C# equivalents of method you've used:
Strings.InStr has equivalent of String.IndexOf
Strings.Mid has equivalent of String.Substring
You need to use the C# equivalent functions something like this:
nPos = sHeadingNm.IndexOf('1');
sHeadingNm = sHeadingNm.Substring( 1, nPos - 1);
Your problem must be that Strings.InStr and Strings.Mid are not standard methods in C#, but in VB.net. You should add probably using Microsoft.VisualBasic in order to use them, although i'd recommend to use C# equivalent methods.
Better stated, you let the Telerik converter convert the code. Code converters can't safely assume that library and function calls which exist in one language do not exist in the other; for all they know, your destination code has a custom library that mimics the behavior of functions only present in the source language. Additionally, most functions in VB that are VB-only are 1-based, not 0-based as in the rest of .Net.
For this reason, you don't get an automatic conversion of Strings.InStr to String.IndexOf. You also won't see Strings.Mid to String.Substring. Code looking for a "0" to return from Strings.Instr or Strings.Mid because nothing was found will break, as "0" is now the first index in a successful search. You actually need the ensuing errors to determine where you need to adjust your code to look for the proper response (i.e., -1 on a search with no results).

Getting the Windows System Error Code title/description from its hex number

I'm messing around with some windows functions using p/invoke. Occasionally, I get an error code that is not ERROR_SUCCESS (such an odd name).
Is there a way to look these up within the program? Forexample, if I get error 1017. Can I tell the user
The system has attempted to load or
restore a file into the registry, but
the specified file is not in a
registry file format.
(ERROR_NOT_REGISTRY_FILE: 0x3F9)
Instead of
Error Code: 1017
I'm not sure if there's a niifty .NET wrapper, but you could call the FormatMessage API using P/Invoke.
See this answer for how it would normally be called from native code. Though the question refers to grabbing error codes from HRESULTs, the answer also applies for retreiving codes from the regular OS error codes coming from GetLastError/GetLastWin32Error).
EDIT: Thanks Malfist for pointing me to pinvoke.net, which includes alternative, managed API:
using System.ComponentModel;
string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
Console.WriteLine(errorMessage);
You could take the defines from winerror.h at Rensselaer Polytechnic Institute, and put them into an Enum:
public enum Win32ErrorCode : long
{
ERROR_SUCCESS = 0L,
NO_ERROR = 0L,
ERROR_INVALID_FUNCTION = 1L,
ERROR_FILE_NOT_FOUND = 2L,
ERROR_PATH_NOT_FOUND = 3L,
ERROR_TOO_MANY_OPEN_FILES = 4L,
ERROR_ACCESS_DENIED = 5L,
etc.
}
Then if your error code is in a variable error_code you would use :
Enum.GetName(typeof(Win32ErrorCode), error_code);
I landed on this page while in search of a managed alternative to calling FormatMessage through P/Invoke.
As others have said, there is no way to get those capitalized, underscored names, short of looking them up in winerror.h, which I have seen reproduced online in various places where I landed in the course of searching for information about resolving specific status codes. A quick Google search, for winerror.h, itself, uncovered a page, at Rensselaer Polytechnic Instutute, where someone has helpfully extracted the #define statements from it.
Looking at it gave me an idea; I think there may be a way to get there, working from the source code of winerror.h, which I have, as part of the Windows Platform SDK that ships with every recent version of Microsoft Visual Studio.
Right now, I am in the middle of sorting out a pressing issue in the .NET assembly that brought me to this page. Then, I'll see what I can cobble together; this kind of challenge is right up my alley, and somebody threw down a gauntlet.
Yes there's a function that does that but I don't remember what it is. In the mean time, you can use the error lookup tool (Tools->Error Lookup) to see what a particular code means from within Visual Studio.

Categories

Resources