How can i get the between cell addresses - c#

I have a function which accepts fromRange and ToRange of an Excel cell. basically i want to read cell by cell values from the range.
suppose if i pass E2 and E9 i want to read in a loop something like Range(E2).value, Range(E3).value and so on till E9
How can i get the between cell addresses. Please help

Option Explicit
Private Sub calculateRangeOneByOne()
Dim rangeIterator As Range
Dim rangeToIterate As Range
Dim sum As Double
Set rangeToIterate = Range("A8", "E8")
sum = 0#
For Each rangeIterator In rangeToIterate
sum = sum + rangeIterator
Next
End Sub
You usually does not want to iterate over ranges one-by-one. there are tons of functions which work on ranges and so this example is definitly a poor one. You'd better use e.g Sum here but just to give you an idea. A range is a collection and you can iteratee over it with for each, You can also use for with index access. But this is at least a bit less "pain"

Related

getting unstructured CSV-madness into order: does this make sense?

I have logfiles in the file-format of csv.
The vendor of the app it comes from wants to stick with it.
MY goal is to build a logreader that does some highlighting and filtering.
The CSV looks ugly as hell, because it is not at all tidy, even if you put some work into it in Excel. A sample of that structure is attached.
Now, i've been pondering about the solution of this a while and came up with a kinda complex solution. But i don't like that solution. It feels too "special"
But Let's look at what we have:
- The columns and their order are defined by a customizable view in the app.
So sometimes e.g. the date-column is first, could also be last, could be missing even. The output is not in the correct order. Sometimes the DAte-coulmn contains the Details-Text. Sometimes a MAC. It's madness.
Edit: Here is how it can look when it's raw data:
http://s000.tinyupload.com/?file_id=79649596476923658435
So what i came up with is this:
- read line1 of your csv, then you know the columns of that file.
- read a config-file that defines "detection-rules" for Columns with set names. So for instance you have a filter for a Colunm for Mac-addresses. One for IP-addresses and vice versa. The Columnns themself are predefined so this is at least possible.
so you read the CSV-line2 and spilt that string to an array, loop through and check for each string if that string matches one of your filters.
Then - if you have a match - you loop through the array you created with the Column-titles (you read line1 of the CSV)
The looping-instance increments a number up.
Then you have another array with the same size as the array containing the titles. You put the found value in there (the incremented number represents the index you use in the new array)
This all probably sounds hard to understand so i wrote a bit of code to illustrate how i meant it.
Am i missing something or is this the way to go with what i have ?
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Line1csv() As String = {"Date", "MAC"}
Dim csvdata As String() = System.IO.File.ReadAllLines("tst.csv")
For Each csvline In csvdata
'lets split the line into pot. cell-values
Dim Csvlinestr() As String = csvline.Split(",")
'now lets loop through what we created and find out what it is
For Each csvfielstr In Csvlinestr
'ok lets ask what this field is
Dim Csvlinetype As String = AskmeWhatitis(csvfielstr)
'good lets say it told us that the result is title2
'so where do we put it ? lets loop through the line1csv to find out the indx
Dim found As Boolean = False
Dim Indx As Integer = 0
For Each l1csvstr In Line1csv
If l1csvstr = Csvlinetype Then
found = True
End If
Indx += 1
Next
If found = True Then
' here we'd put it into our "sorted array that has the same size as the line1csv-array()
' the index used is sthe indx-variable we used above
End If
Next
Next
End Sub
Public Function AskmeWhatitis(ByVal instr As String) As String
' this sub has a config-file that tells it what to search how and what the match will mean: does it mean its a MAC or date or whaterver
Return "truth"
End Function
End Class

given a random set of boxes and a set of spaces, how can you find the optimal combination?

I have kind of an interesting assignment and I'm not sure where to begin.
Imagine you have a set of spaces (all same size) and a set of boxes (variable integer size, 1 or 2 or 3 units of volume). Each space can hold 4 units of volume. Given a random set of boxes, what would be the optimal combination(s) that would leave the most open space (i.e. pack in the densest).
I don't expect anyone to write my code for me, but i could really use some advice on where to start for such a problem. Can anyone help?
I tried listing all the permutations and then choosing the ones with the most "open space" but it was computationally very expensive. I'm using C# if that makes a difference.
the code is kind of longwinded so here is the pseudo code
int [] boxes = new int[20] // I will be given an array of length 20 representing 20 boxes.
// the values of the boxes[] array will be random integers of either 1, 2 or 3
foreach (var v in boxes[]){boxes[v] = random(1 or 2 or 3)
//now i take the set of boxes and come up with all the permutations with a permutation function
int [,] permutearray = new int[20!,20] // each row is the index of a permutation, with the y value as the size.
//permutearray will be a set of numbers representing a possible combination of boxes like
{{1,3,2,2,1,2,3,4 ...},{3,1,2,3,2,1,1, ...}...}
//now check each row to see how many spaces it would take to contain the boxes in that order
// do this using another function i call "fitcheck" on each row/column of permutearray
//fitcheck reports an array of numbers which represent the permutation index
int [] solutions = new [max_solutions]
solutions = fitcheck(permutearray)

How to pass non-contiguous cells to Excel UDF

in myUDF, I can reference a range of cells like "A1:A12", but how can I reference non-contiguous cells like "A1,B4,H3" etc.
I use ExcelDNA, the parameter type is object
but it seems it will take string, and a range of cells, not non-contiguous cells
[ExcelArgument(AllowReference = true, Name = "Relations", Description = "a set of relations")]object rels
It sounds like you're entering the formula onto the worksheet, along with its parameters, from your code, and you want users to then be able to edit the formula normally in Excel's formula bar. Is that correct?
If so, enclose the parameter in parens. For example, for a UDF like this...
Public Function MyUDF(my_param As Range)
'concatenate all cell values in a non-contiguous range:
Dim rgCell As Range, rgArea As Range
For Each rgArea In my_param.Areas
For Each rgCell In rgArea
MyUDF = MyUDF & CStr(rgCell.Value)
Next rgCell
Next rgArea
End Function
...enter it in the worksheet cell like this:
=MyUDF((A1,A3,A7:A11,C8:E10))
Note the extra set of parens compared to using a built-in function like SUM.
BTW as you may already know, when looping through a non-contiguous range you have to loop through the areas of the range, then loop through the cells in each area; looping through the cells in the range only gives you the cells in the first area.

c# indexer for list of list

hi all
i want to make an indexer to list of list to get items like this myopj[i,j] .
my data structure is like this :
list<list<doubl>>
i try code like this but it not work
public double this[int r, int c]
{
set
{
if (this.list1.Count == 0 )
{
this.list1[r].Add(value);
}
else
this.list1[r][c]=value;
}
}
when i watch it the program don't enter the 'if' and it end the watch .
please is there anyone can help
and thank for all .
You should be checking the count, not the capacity. Capacity is the number of elements the list can have, the Count is the actual number of items in your list.
So, given a list that is not null, the capacity should NEVER be zero, but the count could be.
It seems rather strange that your value is either a list<T> or a T.
Without more information, I suggest you implement this using a single, long list of T.
You will then translate myList<T>[i,j] to the underlying list<T>[ i * numRows + j].
Sprinkle validations as required.
If you really, really must use lists of lists figure out if you want to end up with a rectangular matrix (i.e. the list at row i has the same number of elements as the list at row j), or a staggered array (where each list has an independent number of elements).
If you're in the first case, consider creating and filling the row lists automatically, with the appropriate number of elements( use default<T> to initialize them)
Update:
Then, if what you really need is a rectangular matrix then use a list.
It will have numRows * numColumns elements.
Imagine that, instead of putting one row under the other to make a matrix, you put the elements of a row one after the other.
So, if you have:
11 12 13
21 22 23
31 32 33
in a list form they will be
11 12 13 21 22 23 31 32 33
There is a simple conversion between the X and Y (or i and j) coordinates in the first form and the index in the list in the second form.
Why are you checking the capacity? It will likely never be zero. Did you mean to check the Count?
You want the length property, and not Capactiy.
Use:
if (this.list1.Count < r )
Your algorithm doesn't really make much sense, you might want to describe what you are trying to accomplish. It looks like you are trying to make it grow if its not large enough, but the logic in the two seperate paths(if else) doesn't align.

How to sort numbers

I need some help to be able to handle the following logic. The program gets many integers input like 10,16,3,17,21,29,6.
Logic to be done:
Scenario 1:
First select the biggest 4 numbers of input which is 16,17,21,29. Now assign the values to A,B,C and D:
A = smallest in the selected 4
B = biggest in the selected 4
C =
second smallest in the selected 4
D = third smallest in the selected
4
Result to be Displayed:
A = 16
B = 29
C = 17
D = 21
Scenario: 2
If the user gives 3 inputs like 3,6,10 assign only to A,B,C and should ignore D
Result to be Displayed:
A = 3
B = 10
C = 6
Assuming that you have your input values in an array, you can sort it using the static Array.Sort() method and then pick the top/bottom ones by indexing (eg. values[value.Length - 1] gets the highest value). Do make sure to do some bounds checking to avoid runtime exceptions, and to solve "scenario 2" of the assignment correctly.
If you want something more modern, you could also use some LINQ magic to get the top 4 items:
values = values.OrderByDescending(i => i).Take(4).ToArray();
The four highest values are now in highOnes for you to print in whatever order you please. Once again, make sure to do some bounds checking - there might be less than four numbers in the array.
I would take the input and store them into a List. I would then sort this list.
Once sorted you can then pull out the numbers as required.
Logic:
Sort in descending order
Select top 4 element
Assign value as per your requirement in A,B,C,D
Sounds like this could be an assignment so I'll give you these tips:
If you have all these numbers in an array, you can try partially sorting the array. Write a simple bubble sort, and have it sort the largest numbers to the front. But instead of sorting the whole array, make it stop after it brings the 4 largest elements to the front of the array.
That way, your first element will be your largest, and so forth. From there, you can do the rest of the things you need easily.
Hope that helped.

Categories