Equivalent to string * 10 from VB6 in C# - c#

I have import an unmanageable .dll to my project. It's no any document left
and the original working code is in VB6. so I try to make C# code equivalent to VB6 as same as possible.
PROBLEM
I don't know how to convert following code to C#...
Dim ATQ As String * 10
Dim Uid As String * 10
Dim MultiTag As String * 10
NOTE
Q: some users ask me that do you really need string fixed length?
A: I already try string in c# but there are no result update to these variable. So, I think input signature for the dllImport function might be wrong. So, I want to make it as same as VB6 did because I didn't know exactly what should be the right signature.
TRIAL & ERROR
I tried all of this but it's not working (still no result update to these variable)
Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString ATQ = new Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(10)
Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString Uid = new Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(10)
Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString MultiTag = new Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(10)

You can use Microsoft.VisualBasic.Compatibility:
using Microsoft.VisualBasic.Compatibility;
var ATQ = new VB6.FixedLengthString(10);
var Uid = new VB6.FixedLengthString(10);
var MultiTag = new VB6.FixedLengthString(10);
But it's marked as obsolete and specifically not supported for 64-bit processes, so write your own that replicates the functionality, which is to truncate on setting long values and padding right with spaces for short values. It also sets an "uninitialised" value, like above, to nulls.
Sample code from LinqPad (which I can't get to allow using Microsoft.VisualBasic.Compatibility I think because it is marked obsolete, but I have no proof of that):
var U = new Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(5);
var S = new Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(5,"Test");
var L = new Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(5,"Testing");
Func<string,string> p0=(s)=>"\""+s.Replace("\0","\\0")+"\"";
p0(U.Value).Dump();
p0(S.Value).Dump();
p0(L.Value).Dump();
U.Value="Test";
p0(U.Value).Dump();
U.Value="Testing";
p0(U.Value).Dump();
which has this output:
"\0\0\0\0\0"
"Test "
"Testi"
"Test "
"Testi"

string ATQ;
string Uid;
string MultiTag;
One difference is that, in VB6, I believe the String * 10 syntax may create fixed-size strings. If that's the case, then the padding behavior may be different.

Related

Passing a vector from C# to Python

I use Python.Net for C# interaction with Python libraries. I solve the problem of text classification. I use FastText to index and get the vector, as well as Sklearn to train the classifier (Knn).During the implementation, I encountered a lot of problems, but all were solved, with the exception of one.
After receiving the vectors of the texts on which I train Knn, I save them to a separate text file and then, if necessary, use it.
string loadKnowVec = File.ReadAllText("vectorKnowClass.txt", Encoding.Default);
string[] splitKnowVec = loadKnowVec.Split('\r');
splitKnowVec = splitKnowVec.Where(x => x != "").ToArray();
for()
{
keyValues_vector.Add(float.Parse(splitKnowVec[i], NumberFormatInfo.InvariantInfo), 1);
}
dynamic X_vec = np.array(keyValues_vector.Keys.ToArray()).reshape(-1, 1);
dynamic y_tag = np.array(keyValues_vector.Values.ToArray());
dynamic neigh = KNN(n_neighbors: 3);
dynamic KnnFit = neigh.fit(X_vec, y_tag);
string predict = neigh.predict("0.00889");
MessageBox.Show("Скорее всего это: "+predict);
During the training of the classifier, I encountered such a problem that from c# to python, it is not values with the float type, but the value of System.Single[].
Python.Runtime.PythonException: "TypeError : float() argument must be a string or a number,
not 'Single[]'
The stored value, at this point, of dynamic X_vec is "System.Single[]".(I think that's exactly the problem)
2.At first I tried to manually set the values of X_vec, but the error and its values were the same.
The first idea was to change the array type using the numpy library, but it didn't help, it also gave out "".
dynamic Xx = np.array(X_vec, dtype: "float");
dynamic yY = np.array(y_tag, dtype: "int");
Next, it was tried to create an empty array in advance and load specific values into it before changing the data type, but this also did not work.
Perhaps I do not understand the principle of the formation and interaction of the MSVS19 IDE and the python interpreter.
I solved this issue for a couple of days and each time I thought it was worth reading the documentation on python.net .
As a result, I found a solution and it turned out to be quite banal, it is necessary to represent X_vec not as a float[] , but as a List<float>
List<float> vectors = keyValues_vector.Keys.ToList();
List<int> classTag = keyValues_vector.Values.ToList();
dynamic a = np.array(vectors);
dynamic X_vec = a.reshape(-1, 1);
dynamic y_tag = np.array(classTag);

C# Interop Non-invocable member 'Microsoft.Office.Interop.Excel.Range.End' cannot be used like a method

I'm using C# Interop to get some values from a Worksheet and I get the following error:
Non-invocable member 'Microsoft.Office.Interop.Excel.Range.End' cannot be used like a method.
This is my code:
var wb = (Excel.Workbook)Globals.ThisAddIn.Application.ActiveWorkbook;
var wsEvars = wb.Sheets["Evars"];
var wsProps = wb.Sheets["Props"];
var wsEvents = wb.Sheets["Events"];
var wsListVars = wb.Sheets["List Vars"];
var sheetList = new Excel.Worksheet[] { wsEvars, wsProps, wsEvents, wsListVars };
for (var i = 0; i < sheetList.Length; i++)
{
// I get the error on the line below
var rowLast = sheetList[i].Range["I" + sheetList[i].Rows.Count].End(Excel.XlDirection.xlUp).Row;
}
The thing is that is works if I try as follows:
for (var i = 0; i < sheetList.Length; i++)
{
var rowLast = wsEvars .Range["I" + wsEvars .Rows.Count].End(Excel.XlDirection.xlUp).Row;
}
Am I missing something?
Looks like you found a bug in the C# compiler. The bug is actually present in the workaround, it ought to not compile for the same reasons the first snippet did not. Albeit that it is difficult to definitely claim that this is a bug, the C# language spec does not describe what is acceptable in this case.
The Range.End property is an indexed property. Such properties are not formally supported in C#, the language permits only the class indexer (aka this[]) to be the one-and-only indexed property of a class. But that restriction was lifted in C# version 4, specifically to make interop with COM servers easier. Like Excel, indexed properties are very common in COM object models.
Like the normal indexer, you have to use square brackets. Fix:
var rowLast = sheetList[i].Range["I" + sheetList[i].Rows.Count]
.End[Excel.XlDirection.xlUp].Row;
And the workaround you had to use in older C# versions is still available:
var rowLast = sheetList[i].Range["I" + sheetList[i].Rows.Count]
.get_End(Excel.XlDirection.xlUp).Row;
Hard to guess why it finds () parentheses acceptable in the second snippet. It looks like a bug, swims like a bug and quacks like a bug, so it is probably a bug. Let them know about it by clicking the New Issue button. I doubt they'll fix it but there might be more wrong than meets the eye.

Need to ensure C# HDF5 output (of attributes) is same as Visual C++. Or what is equivalent HDF5 formatting in C# as C++

I wrote another question (https://stackoverflow.com/questions/26812721/hdf5-c-sharp-examples-to-solve-3-specific-questions-i-demonstrate-what-has-been) that was flagged as too general, so I'm rewriting to be clearer, more succinct, and as specific as possible. I have used Visual C++ and HDF5 (see: www.hdfgroup.org) to output some datasets. All works well. To format two of the attributes in C++, I use
DataSpace attr_dataspace = DataSpace(H5S_SCALAR);
Attribute attribute_cardNum = dataSet.createAttribute(
attrCardNumber, PredType::STD_I32BE, attr_dataspace, PropList::DEFAULT);
attribute_cardNum.write(PredType::NATIVE_INT, &cardNumber); // write out the card number
and
StrType strdatatype(PredType::C_S1, 256); // of length 256 characters
// Create attribute and write to it
Attribute attribute_boardName = dataSet.createAttribute(
attrBoardName, strdatatype, attr_dataspace);
attribute_boardName.write(strdatatype, asciiBoardName);
Using the HDF5 Java viewer for example, I get
Name Value Type Array Size
Board Name UltraMaster String,length=256 Scalar
Card number 0 32-bit integer Scalar
When I use C# to export the HDF5 (using libraries at hdf5.net), I get:
Name Value Type Array Size
Board Name ˜„  String, length=256 1
Card Number 0 32-bit integer 1
Note that the Array Size is now "1" instead of scalar, and the Value for Board Name is totally hosed up.
My C# code is different (obviously :)). I have:
// Create card Number attribute
H5AttributeId attrCardId = H5A.create(dataSetId, "Card Number", typeId,
H5S.create_simple(1, new long[1] { 1 }));
H5A.write(attrCardId, new H5DataTypeId(H5T.H5Type.NATIVE_INT),
new H5Array<int> (new int[]{cardNumber}));
// Create Board Name attribute
byte[] asciiStr = ASCIIEncoding.ASCII.GetBytes("Board Name");
H5AttributeId attrBoardNameId = H5A.create(dataSetId, "Board Name",
H5T.create(H5T.CreateClass.STRING, 256), H5S.create_simple(1, new long[1] { 1 }));
H5A.write(attrBoardNameId, H5T.create(H5T.CreateClass.STRING,256),
new H5Array<string>(new string[] { GetBoardNameFromCardNum(cardNumber) }));
As I said, the results are different. It would be desirable to have the C# mimic the C++ output (although I suppose I could change the C++ code). So specifically,
How do I get the array size to be "Scalar"
How to I output the board Name? I assume this has something to do with the fact that I'm dealing with C# strings (unicode) and not C++ ascii strings.
In case anyone needs this at some later point, the relevant code that worked for me was:
// Card Number Attribute
H5AttributeId attrCardId = H5A.create(dataSetId, "Card Number", typeId, H5S.create(H5S.H5SClass.SCALAR));
H5A.write(attrCardId, new H5DataTypeId(H5T.H5Type.NATIVE_INT), new H5Array<int> (new int[]{cardNumber}));
Notice the H5A.create instead of H5A.create_simple
// Create Board Name attribute
byte[] asciiStr = ASCIIEncoding.ASCII.GetBytes(GetBoardNameFromCardNum(cardNumber));
H5AttributeId attrBoardNameId = H5A.create(dataSetId, "Board Name", H5T.create(H5T.CreateClass.STRING, 256), H5S.create(H5S.H5SClass.SCALAR));
H5A.write(attrBoardNameId, H5T.create(H5T.CreateClass.STRING,256), new H5Array<byte>(asciiStr));
Additionally, I'd like to point out that there are some HDF5.net examples at:
HDF5.net. Look for •HDF5DotNet source and examples
Once you get that, look under the "tests" folder as well as the "example" folder.
Cheers,
Dave

Word Interop Delete results in Wrong Parameter

I have the pleasure to write some code that moves around stuff in an Office XP environment. I've referenced the OfficeXP Interop Assemblies and written code to Search/Replace stuff. That works fine. Now I need to delete Text around a Bookmark and i keep getting Exceptions thrown at me.
Here is some of the code:
object units = WdUnits.wdLine;
object lines = 2;
object extend = WdMovementType.wdExtend;
object bookmarkName = "Bank1";
var bm = doc.Bookmarks;
var bm1 = doc.Bookmarks.get_Item(bookmarkName);
var ra = bm1.Range;
ra.Delete(ref units, ref lines);
The last line is where i get a "Wrong Parameter" Exception. Looking at the Definition in the MSDN I kind of think I'm right. But obviously I'm not. Hope you guys can help me out here.
Update: ok, i see. Using the Delete method on the Range object i can only use wdWord as a Parameter. I'd like to change my question now: what i do want to do is delete two lines starting from the bookmark. How would i do this?
Range objects in Word are not "line oriented", they don't allow line operations, only paragraph operations. However, selections allow line operations. The current selection is not a property of the word document, but of the word application object. Here is some VBA code which does essentially what you try, I think you can easily port this to C#:
Dim rng As Range
Dim doc As Document
Set doc = ActiveDocument
Set rng = doc.Bookmarks("BM").Range
Dim s As Long, e As Long
rng.Select
s = Application.Selection.Start
e = Application.Selection.Next(wdLine, 1).End
Application.Selection.SetRange s, e
Application.Selection.Delete
Ok, i found a way to do what i had to do. Here is the Code:
if (doc.Bookmarks.Exists("Bank1"))
{
object bookmarkName = "Bank1";
object units = WdUnits.wdLine;
object lines = 2;
object extend = WdMovementType.wdExtend;
doc.Bookmarks.get_Item(bookmarkName).Select();
app.Selection.MoveDown(units, lines, extend);
app.Selection.Delete();
}

Using a large static array in C# (Silverlight on Windows Phone 7)

I have a question that's so simple I cannot believe I can't answer it myself. But, there you go.
I have a large-ish static list (of cities, latitudes and longitudes) that I want to use in my Windows Phone 7 Silverlight application. There are around 10,000 of them. I'd like to embed this data statically in my application and access it in an array (I need to cycle through the whole list in code pretty regularly).
What is going to be my most effective means of storing this? I'm a bit of an old school sort, so I reckoned the fastest way to do it would be:
public struct City
{
public string name;
public double lat;
public double lon;
};
and then...
private City[] cc = new City[10000];
public CityDists()
{
cc[2].name = "Lae, Papua New Guinea"; cc[2].lat = 123; cc[2].lon = 123;
cc[3].name = "Rabaul, Papua New Guinea"; cc[3].lat = 123; cc[3].lon = 123;
cc[4].name = "Angmagssalik, Greenland"; cc[4].lat = 123; cc[4].lon = 123;
cc[5].name = "Angissoq, Greenland"; cc[5].lat = 123; cc[5].lon = 123;
...
However, this bums out with an "out of memory" error before the code actually gets to run (I'm assuming the code itself ended up being too much to load into memory).
Everything I read online tells me to use an XML resource or file and then to deserialise that into instances of a class. But can that really be as fast as using a struct? Won't the XML take ages to parse?
I think I'm capable of writing the code here - I'm just not sure what the best approach is to start with. I'm interested in speed of load and (more importantly) run time access more than anything.
Any help very much appreciated - first question here so I hope I haven't done anything boneheaded.
Chris
10,000 structs shouldn't run out of memory, but just to make sure, I would first try turning your struct into a class such that it uses the heap instead of the stack. There is a strong possibility that doing that will fix your out of memory errors.
An XML file stored in isolated storage might be a good way to go if your data is going to be updated even every once in a while. You could pull the cities from a web service and serialize those classes to the Application Store in isolated storage whenever they get updated.
Also, I notice in the code samples that the cc array is not declared static. If you have a few instances of CityDists, then that could also be bogging down memory as the array is getting re-created every time a new CityDists class is created. Try declaring your array as static and initializing it in the static constructor:
private static City[] cc = new City[10000];
static CityDists()
{
cc[2].name = "Lae, Papua New Guinea"; cc[2].lat = 123; cc[2].lon = 123;
cc[3].name = "Rabaul, Papua New Guinea"; cc[3].lat = 123; cc[3].lon = 123;
cc[4].name = "Angmagssalik, Greenland"; cc[4].lat = 123; cc[4].lon = 123;
cc[5].name = "Angissoq, Greenland"; cc[5].lat = 123; cc[5].lon = 123;
...
If loading an xml doc from the xap works for you..
Here's a project I posted demonstrating loading of an xml doc from the XAP via XDocument/LINQ and databinding to a listbox for reference.
binding a Linq datasource to a listbox
If you want to avoid the XML parsing and memory overhead, you could use a plain text file for storing your data and use the .Net string tokenizer functions to parse the entries e.g. use String.Split()
You could also load the file partially to keep memory consumption low. For example, you load only k out of n lines of the file. In case you need to access a record that is outside the currently loaded k segments, load the appropriate k segments. You could either do it the old school way or even use the fancy Serialization stuff from .Net
Using a file such as XML or a simple delimited file would be a better approach as others have pointed out. However can I also suggest another change to improve the use of memory.
Something like this (although the actual loading should be done using an external file):-
public struct City
{
public string name;
public string country;
public double lat;
public double lon;
}
private static City[] cc = new City[10000];
static CityDists()
{
string[] countries = new string[500];
// Replace following with loading from a "countries" file.
countries[0] = "Papua New Guinea";
countries[1] = "Greenland";
// Replace following with loading from a "cities" file.
cc[2].name = "Lae"; cc[2].country = contries[0]; cc[2].lat = 123; cc[2].lon = 123;
cc[3].name = "Rabaul"; cc[3].country = countries[0]; cc[3].lat = 123; cc[3].lon = 123;
cc[4].name = "Angmagssalik"; cc[4].country = countries[1]; cc[4].lat = 123; cc[4].lon = 123;
cc[5].name = "Angissoq"; cc[5].country= countries[1]; cc[5].lat = 123; cc[5].lon = 123;
}
This increases the size of the structure slightly but reduces the memory used by duplicate country names signficantly.
I hear your frustration. Run your code without the debugger, it should work fine. I'm loading 2 arrays in under 3 seconds, each with over 100,000 elements. Debugger reports "Out of Memory", which is simply not the case.
Oh and you are correct about the efficiency. Loading the same information from an XML file was taking over 30 seconds on the phone.
I don't know who was responding to your question but they really should stick to marketing.

Categories