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).
Related
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
I'm a beginner in programming and i would like to know how can i translate the following code into C#
Dim arrayAlumnos(ds.Tables(0).Rows.Count - 1) As Registro
To preserve the array idea I'd probably write it like:
var arrayAlumnos = new Registro[ds.Tables[0].Rows.Count];
But you could say this too:
Registro[] arrayAlumnos = new Registro[ds.Tables[0].Rows.Count];
But ChaosPandion is right... a List is what you'd want to use most likely.
You should be using a list so I think this is the proper translation.
List<Registro> students = new List<Registro>();
you'll probably have a lot of one off questions like this going through your travels of conversion, might want to take a look at:
http://msdn.microsoft.com/en-us/library/aa288436(v=vs.71).aspx
The basic idea behind the creation in C# is...
~ObjectType~ ~varName~ = new ~type~(~implement a type constructor here~);
anything in between ~~s you'll need to plug in the appropriate information. For your case it would likely be:
Registro[] arrayAlumnos = new Registro[ds.Tables[0].Rows.Count - 1];
Kind of hard since it's a different spoken language but based on your variable name i'm guessing it's an array, though as others pointed out it could easily be created as a list.
FYI, if you want to convert a lot of code from VB to C#, you can use the ILSpy disassebler. You can do this even if you don't have the original VB code. Do this:
Compile the VB code into a *.exe or a *.dll.
Open the *.exe or *.dll file in ILSpy.
In the language dropdown, select VB. (It's values are C#, VB, and MSIL).
I was on a task of porting C++ code to C#. There was a bug reported by client in my code. When I did debugging, here is what I got...
C++ Code:
double d = -0.0000000000018736038338473693;
String temp = String(SimpleRoundTo(d, -12))); // SimpleRoundTo() is from delphi
// temp is having -1E-12
C# Code:
double d = -0.0000000000018736038338473693;
string temp = (Math.Round(d, 12)).ToString();
// temp is having -2E-12
I did check the overloads by Math.Round(), not getting how to get the same result.
The answer that the C++ version is giving is just plain wrong. That should be obvious. It's clear that 2E-12 is the correct answer since SimpleRoundTo, just like Math.Round, rounds to nearest.
So, if you wish to reproduce the erroneous output of the C++ version in C# you'll need to translate the Delphi RTL code to C#. That would involve working out exactly what SimpleRoundTo and the String() conversion does. Or p/invoke to a C++ DLL.
I would tackle this by trying to work out what the original code is intended to do. Express this in a mathematical way rather than in terms of the RTL functions. Once you have a clear specification of what you intend the code to do, code it in C# using the natural C# mechanisms. In other words, don't attempt a literal translation because one probably does not exist. Instead understand the intent of the original code and implement it from scratch in C#.
Looking at your code you perform rounding, and then convert to text. I would be looking for an approach that performed the rounding as part of the conversion to text.
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)).
So as an excercise in utility i've taken it upon myself to convert one of our poor old vb .net 1.1 apps to C# .net 4.0.
I used telerik code conversion for a starting point and ended up with ~150 errors (not too bad considering its over 20k of code and rarely can i get it to run without an error using the production source) many of which deal with time/date in vb versus c#.
my question is this how would you represent the following statement in VB
If oStruct.AH_DATE <> #1/1/1900# Then
in C#? The converter gave me
if (oStruct.AH_DATE != 1/1/1900 12:00:00 AM) {
which is of course not correct but I cannot seem to work out how to make it correct.
if (oStruct.AH_DATE != new DateTime(1900, 1, 1){
You may try this construct :
if ( DateTime.Compare(oStruct.AH_DATE, new DateTime(1900, 1, 1)) == 0 )
{
// your code here
}
Here is a reference how to compare DateTimes in .NET - DateTime.Compare Method
Note: Comparing two dates like integers may lead you to wrong results. Because, date-time structure in .NET has its specific properties. I would always try to be attentive when comparing two dates, and always choose DateTime.Compare to be on a safer side.
This should work:
if (oStruct.AH_DATE != DateTime.Parse("1/1/1900"))