How can this code be written shorter/clearer? - vb.net

newRow("OrderReference") = line.Substring(line.IndexOf("*1003") + 5, line.IndexOf("*", line.IndexOf("*1003") + 5) - line.IndexOf("*1003") - 5)
There you have it. Very long and ugly. I was thinking about this:
Dim indexPlus = line.IndexOf("*1003") + 5
Dim indexMinus = line.IndexOf("*1003") - 5
newRow("OrderReference") = line.Substring(indexPlus, line.IndexOf("*", indexPlus) - indexMinus)
But that introduces new and meaningless vars. Unsatisfying.
Maybe RegEx is the savior here?
Unfortunately I mustn't change the input data :-(
The input data consist of the BWA-format (popular with books). Here you can see the part in question:
All codes in this example set are required. Only corresponding values change.

I don't even think your second code works. It seems more like this.
Dim index = line.IndexOf("*1003") + 5
newRow("OrderReference") = line.Substring(index, line.IndexOf("*", indexPlus) - index)
10 - 5 - 2 isn't the same as 10 - (5 - 2) but instead it's the same as 10 - (5 + 2).
Next time, check out the codereview stack exchange.

Given that your data is always constant, and what you're looking for always begins with "*1003", you don't need to use Regex (Even though you could). Just use what you're already using but with some corrections.
using System;
public class Program
{
public static void Main()
{
string input = "L10113540 VD44444 VD2002100234949 000116161 04201261\r\n";
input += " KN00010000000129000LPEUR003000001*1003A.Muller-Schulz*1017Bastei\r\n";
input += "Lubbe.61204 Laund.Meine Schuld*1019KL*102990300*1030NO*1032EUR*1131KT";
int start = input.IndexOf("*1003");
int end = input.IndexOf("*", start + 1);
string result = input.Substring(start + 5, end - start - 5);
Console.WriteLine(result);
// Your code
start = input.IndexOf("*1003") + 5;
end = input.IndexOf("*1003") - 5;
result = input.Substring(start, input.IndexOf("*", start) - end);
Console.WriteLine(result);
}
}
Result
A.Muller-Schulz
A.Muller-Schulz*1017Baste
You can see that what you posted in your question, doesn't give the results you want. All you're really looking for is just the next asterisk after the first "*1003". You can see the difference between your code and what I've given.
.NET Fiddle Example

Related

How to count time in chisel with iotesters?

Is there a way to get the step value in iotesters ?
Currently I'm using a var counter but I'm sure there is a better way :
class MyTest (dut: MyModule) extends PeekPokeTester(dut) {
var timeCounter = 0
for(i <- 0 to 10) {
step(1)
timeCounter = timeCounter + 1
}
println("Step value is " + timeCounter)
Is there a getStepValue() like function to get that ?
You can get this using the (presently undocumented) method t.
There's an internal var simTime that is tracking time. This is automatically incremented on a step (just like how you're doing it). The method t lets you query the query its value.

Problem with setting indent of bullet items in textframe

I am trying to port some existing VBA code to C#. One routine controls the indentation of bullet items, and is roughly:
indentStep = 13.5
For Each parag In shp.TextRange.Paragraphs()
parag.Parent.Ruler.Levels(parag.IndentLevel).FirstMargin = indentStep * (parag.IndentLevel - 1)
parag.Parent.Ruler.Levels(parag.IndentLevel).LeftMargin = indentStep * (parag.IndentLevel)
Next parag
The code works, but appears to be spooky black magic. In particular, each time a particular ruler's margins are set ALL NINE rulers margins are actually set.
But somehow the appropriate information is being set. Unfortunately, when you do the same thing in C#, the results change. The following code has no visible effect:
const float kIndentStep = 13.5f;
foreach (PowerPoint.TextRange pg in shp.TextFrame.TextRange.Paragraphs())
{
pg.Parent.Ruler.Levels[pg.IndentLevel].FirstMargin = kIndentStep * (pg.IndentLevel - 1);
pg.Parent.Ruler.LevelS[pg.IndentLevel].LeftMargin = kIndentStep * pg.IndentLevel;
}
This appears to be a limitation/bug when automating PowerPoint from C#. I confirm it works with VBA.
I do see an effect after the code runs: it changes the first level with each run so that, at the end, the first level has the settings that should have been assigned to the last level to be processed, but none of the other levels appear to be affected, visibly. I do see a change in the values returned during code execution, but that's all.
If the code changes only one, specific level for the text frame, it works. The problem occurs only when attempting to change multiple levels.
I tried various approaches, including late-binding (PInvoke) and putting the change in a separate procedure, but the result was always the same.
Here's my last iteration
Microsoft.Office.Interop.PowerPoint.Application pptApp = (Microsoft.Office.Interop.PowerPoint.Application) System.Runtime.InteropServices.Marshal.GetActiveObject("Powerpoint.Application"); // new Microsoft.Office.Interop.PowerPoint.Application();
//Change indent level of text
const float kIndentStep = 13.5f;
Microsoft.Office.Interop.PowerPoint.Shape shp = pptApp.ActivePresentation.Slides[2].Shapes[2];
Microsoft.Office.Interop.PowerPoint.TextFrame tf = shp.TextFrame;
object oTf = tf;
int indentLevelLast = 0;
foreach (Microsoft.Office.Interop.PowerPoint.TextRange pg in tf.TextRange.Paragraphs(-1, -1))
{
int indentLevel = pg.IndentLevel;
if (indentLevel > indentLevelLast)
{
Microsoft.Office.Interop.PowerPoint.RulerLevel rl = tf.Ruler.Levels[indentLevel];
object oRl = rl;
System.Diagnostics.Debug.Print(pg.Text + ": " + indentLevel + ", " + rl.FirstMargin.ToString() + ", " + rl.LeftMargin.ToString()) ;
object fm = oRl.GetType().InvokeMember("FirstMargin", BindingFlags.SetProperty, null, oRl, new object[] {kIndentStep * (indentLevel - 1)});
//rl.FirstMargin = kIndentStep * (indentLevel - 1);
object lm = oRl.GetType().InvokeMember("LeftMargin", BindingFlags.SetProperty, null, oRl, new object[] { kIndentStep * (indentLevel) });
//rl.LeftMargin = kIndentStep * indentLevel;
indentLevelLast = indentLevel;
System.Diagnostics.Debug.Print(pg.Text + ": " + indentLevel + ", " + tf.Ruler.Levels[indentLevel].FirstMargin.ToString() + ", " + tf.Ruler.Levels[indentLevel].LeftMargin.ToString()) ;
rl = null;
}
}
FWIW neither code snippet provided in the question compiles. The VBA snippet is missing .TextFrame. The C# snippet doesn't like Parent.Ruler so I had to change it to TextFrame.Ruler.

How do I read the data from a TYPE_MIME_PART item?

It kinda works, but the problem is that it seems that the MIME_PART structure is not initialized ? all it's properties has the same values, even if I try to open a different mime item.
MIME_PART *pMime;
DHANDLE hPart;
char *pText;
WORD textLen;
if (error = NSFMimePartGetPart(bidLinksItem, &hPart)) {
goto exit;
}
pMime = OSLock(MIME_PART, hPart);
textLen = (pMime->wByteCount) - pMime->wHeadersLen - pMime->wBoundaryLen;
pText = (char *)pMime + sizeof(MIME_PART) + wHeadersLen;
char *itemText = (char *)malloc(textLen);
memcpy(itemText, pText, textLen);
itemText[textLen] = '\0';
OSUnlock(hPart);
The itemText string has most of the content, but since the MIME_PART structure is not properly set, the pointer to the text is off...
So how do I properly set the MIME_PART?
Your code should do something like this instead:
DHANDLE hPart;
char *pchPart;
if (error = NSFMimePartGetPart(bidLinksItem, &hPart)) {
goto exit;
}
pchPart = OSLock(char, hPart);
In other words, lock the handle as type char instead of type MIME_PART. At this point, pchPart points to the beginning of the raw part data -- starting with a boundary (if present) and the headers. You can use NSFMimePartGetInfoByBLOCKID to get the length of the boundary and headers.
I realize this contradicts the documentation, but I've confirmed with a subject matter expert: The documentation is wrong.
Wrong answer, but the comments may be useful. My other answer is more correct.
This question could be improved. For example, you could show some sample data and describe the results when you try to read that data with your code.
But I'll try to answer based on the information I have. You calculated the text length like this:
textLen = (pMime->wByteCount) - pMime->wHeadersLen - pMime->wBoundaryLen;
That looks right to me, but then you do this:
pText = (char *)pMime + sizeof(MIME_PART) + wHeadersLen;
Is wHeadersLen guaranteed to be equal to pMime->wHeadersLen? Also, you didn't consider the boundary length. Shouldn't you calculate the address like this instead?
pText = (char *)pMime + sizeof(MIME_PART) + pMime->wHeadersLen + pMime->wBoundaryLen;

LINQ return value conversion failed from string to int

Hello there so I have a DropDownBox in vb.net which is field Type: Varchar in MS SQL Server 2012 and shows like this:
1 - Info
10.1 - Testing
10.2 - Programs
2 - Modes
3 - Qualifications
....
But it shows in the wrong order. This is the code I got:
Public Shared Function GetLineCodeList() As IEnumerable
Dim _Result As IEnumerable
_Result = (From lc As SLBLINECODE In DataManager.SlbContext.SLBLINECODES
Where lc.HIDDEN = 0
Order By lc.LINECODE
Select New With {
.LINECODE = lc.LINECODE,
.LINEDESCRIPTION = lc.LINECODE + " - " + lc.LINEDESCRIPTION
}).ToList()
Return _Result
End Function
So I been trying to do a Order By CInt(lc.LINECODE)
Because I know Oracle works fine, but in T-SQL don't behave the same. Any recommendations ?
Try Convert.ToInt32(lc.LINECODE) or maybe due to 10.1 try Convert.ToDecimal(lc.LINECODE). There are more conversion methods here of the Convert class.
UPDATE #1
What seems a better solution is to forget the order at the DB side.
Instead before calling .ToList() call .OrderBy(item => Decimal.Parse(item.LINECODE)).
In VB.NET: .OrderBy(Function(item) Decimal.Parse(item.LINECODE))
UPDATE #2
#Keyur PATEL is right. I also suggested to forget the ordering at the DB side but missed that an extra call to .ToList() is needed.
So what should work looks like this:
_Result =
(
From lc As SLBLINECODE In DataManager.SlbContext.SLBLINECODES
Where lc.HIDDEN = 0
Order By lc.LINECODE
Select New With {
.LINECODE = lc.LINECODE,
.LINEDESCRIPTION = lc.LINECODE + " - " + lc.LINEDESCRIPTION
}
) _
.ToList() _
.OrderBy(Function(item) Decimal.Parse(item.LINECODE)) _
.ToList()

Make line chart with values and dates

In my app i use ios-charts library (swift alternative of MPAndroidChart).
All i need is to display line chart with dates and values.
Right now i use this function to display chart
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let lineChartDataSet = LineChartDataSet(yVals: dataEntries, label: "Items count")
let lineChartData = LineChartData(xVals: dataPoints, dataSet: lineChartDataSet)
dateChartView.data = lineChartData
}
And this is my data:
xItems = ["27.05", "03.06", "17.07", "19.09", "20.09"] //String
let unitsSold = [25.0, 30.0, 45.0, 60.0, 20.0] //Double
But as you can see - xItems are dates in "dd.mm" format. As they are strings they have same paddings between each other. I want them to be more accurate with real dates. For example 19.09 and 20.09 should be very close. I know that i should match each day with some number in order to accomplish it. But i don't know what to do next - how i can adjust x labels margins?
UPDATE
After small research where i found out that many developers had asked about this feature but nothing happened - for my case i found very interesting alternative to this library in Swift - PNChart. It is easy to use, it solves my problem.
The easiest solution will be to loop through your data and add a ChartDataEntry with a value of 0 and a corresponding label for each missing date.
In response to the question in the comments here is a screenshot from one of my applications where I am filling in date gaps with 0 values:
In my case I wanted the 0 values rather than an averaged line from data point to data point as it clearly indicates there is no data on the days skipped (8/11 for instance).
From #Philipp Jahoda's comments it sounds like you could skip the 0 value entries and just index the data you have to the correct labels.
I modified the MPAndroidChart example program to skip a few data points and this is the result:
As #Philipp Jahoda mentioned in the comments the chart handles missing Entry by just connecting to the next data point. From the code below you can see that I am generating x values (labels) for the entire data set but skipping y values (data points) for index 11 - 29 which is what you want. The only thing remaining would be to handle the x labels as it sounds like you don't want 15, 20, and 25 in my example to show up.
ArrayList<String> xVals = new ArrayList<String>();
for (int i = 0; i < count; i++) {
xVals.add((i) + "");
}
ArrayList<Entry> yVals = new ArrayList<Entry>();
for (int i = 0; i < count; i++) {
if (i > 10 && i < 30) {
continue;
}
float mult = (range + 1);
float val = (float) (Math.random() * mult) + 3;// + (float)
// ((mult *
// 0.1) / 10);
yVals.add(new Entry(val, i));
}
What I did is fully feed the dates for x data even no y data for it, and just not add the data entry for the specific xIndex, then it will not draw the y value for the xIndex to achieve what you want, this is the easiest way since you just write a for loop and continue if you detect no y value there.
I don't suggest use 0 or nan, since if it is a line chart, it will connect the 0 data or bad things will happen for nan. You might want to break the lines, but again ios-charts does not support it yet (I also asked a feature for this), you need to write your own code to break the line, or you can live with connecting the 0 data or just connect to the next valid data.
The down side is it may has performance drop since many xIndex there, but I tried ~1000 and it is acceptable. I already asked for such feature a long time ago, but it took lot of time to think about it.
Here's a function I wrote based on Wingzero's answer (I pass NaNs for the entries in the values array that are empty) :
func populateLineChartView(lineChartView: LineChartView, labels: [String], values: [Float]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<labels.count {
if !values[i].isNaN {
let dataEntry = ChartDataEntry(value: Double(values[i]), xIndex: i)
dataEntries.append(dataEntry)
}
}
let lineChartDataSet = LineChartDataSet(yVals: dataEntries, label: "Label")
let lineChartData = LineChartData(xVals: labels, dataSet: lineChartDataSet)
lineChartView.data = lineChartData
}
The solution which worked for me is splitting Linedataset into 2 Linedatasets. First would hold yvals till empty space and second after emptyspace.
//create 2 LineDataSets. set1- till empty space set2 after empty space
set1 = new LineDataSet(yVals1, "DataSet 1");
set2= new LineDataSet(yVals2,"DataSet 1");
//load datasets into datasets array
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(set1);
dataSets.add(set2);
//create a data object with the datasets
LineData data = new LineData(xVals, dataSets);
// set data
mChart.setData(data);