Is there an alternative to using List<string[]>? - c#

I'm using:
List<string[]>
which works, but just feels so wrong! Does anyone have any correct more up date ways to achieve this or is it not wrong? I have tried the following, which works but also feels wrong:
List<List<string>>
For any moaners out there, my definition of wrong is hacky, out of date, old code, over complicated code, etc
Thanks
Edit:
Sorry guys, I forgot to mention the list length is un-known so it's likely to be large list with a non specified number of items, each array will consist of around 10-20 items

There isn't anything wrong with either of them, in the general case. There are certainly specific cases where one or the other may not be appropriate, but without any details there is no way of knowing whether this is an appropriate usage.
It's certainly not a pattern that you would globally consider bad. There's simply no reason for that to be the case.

I would put
List<string>
inside a class then use
List<Class>

the list length is un-known
If you don't know the length why not use List<List<string>>?
As mentioned before it's neither wrong nor hacky or overcomplicated. It's exactly what you need. A list of unknown length that contains lists of strings.

Related

Accessing grid column values in VS2017

I am writing a chunk of code that takes a grid and does some processing. In this processing, I need to look at a field in the grid that is a DateTime type.
I've seen two different techniques for accessing the field and I was wondering if there was any consensus on which might be better. To be frank, I don't understand the syntax for the second one at all and haven't been able to find much of anything on it, but it's coming from someone who seems to know their onions and I wanted to see if it was much, much better, or if it was basically a draw.
So, what do you all think?
This one
d = (DateTime) row.Cells["DT0"].Value;
or this one?
d = row.Cells["DT0"].GetVal<DateTime>();
Or is there yet another, better, way?
Okay, so it looks like the second one was an in-house thing after all. Not sure why they did it as I have to move on and haven't had a chance to investigate it.
So it looks like the first one will do, providing I first check for
"if ( row.Cells[ "DT0" ].Value != null )"
Thank you all for your help!

Data structure for searching strings

I am looking for the best data structure for the following case:
In my case I will have thousands of strings, however for this example I am gonna use two for obvious reasons. So let's say I have the strings "Water" and "Walter", what I need is when the letter "W" is entered both strings to be found, and when "Wat" is entered "Water" to be the only result. I did a research however I am still not quite sure which is the correct data structure for this case and I don't want to implement it if I am not sure as this will waste time. So basically what I am thinking right now is either "Trie" or "Suffix Tree". It seems that the "Trie" will do the trick but as I said I need to be sure. Additionally the implementation should not be a problem so I just need to know the correct structure. Also feel free to let me know if there is a better choice. As you can guess normal structures such as Dictionary/MultiDictionary would not work as that will be a memory killer. I am also planning to implement cache to limit the memory consumption. I am sorry there is no code but I hope I will get a answer. Thank you in advance.
You should user Trie. Tries are the foundation for one of the fastest known sorting algorithms (burstsort), it is also used for spell checking, and is used in applications that use text completion. You can see details here.
Practically, if you want to do auto suggest, then storing upto 3-4 chars should suffice.
I mean suggest as and when user types "a" or "ab" or "abc" and the moment he types "abcd" or more characters, you can use map.keys starting with "abcd" using c# language support lamda expressions.
Hence, I suggest, create a map like:
Map<char, <Map<char, Map<char, Set<string>>>>> map;
So, if user enters "a", you look for map[a] and finds all children.

Can you dynamically search for sequences within a string in c#?

First time asking a question on here;
I am looking for a way to be able to use a search algorithm, or a built in method to dynamically search for repeating sequences within a string, or other variable.
The reason I say dynamic, is because I want it to be able to search through the string and locate repeating sequences on its own. I am not going to be able to supply a constructor of a sequence to look for.
I am unsure if this is even possible, but if it is, all help would be appreciated!
Here is a basic visual representation of what I am looking for (mind you, this is not code, just a for instance of a string)
This is going to be a long string that will have sequences throughout it. This may have matching characters side by side or it may not, but regardless, this is going to be a long string. If this is going to be a long string, I need it to find these sequences throughout it on its own!
As you can see by the above example, there are 2 sets of matching sequences throughout the single string. If there is any way to identify these programatically, along with being able to be searched through very fast for these different patterns, it would help me significantly!
The matches will most likely be stored in a List / array for later use as well.
Thank you for any help you are able to provide!
Edit:
As this question was asked, case sensitivity will not be an issue.
When I was mentioning there were 2 matches, I meant that 2 particular sequences, had a duplicate. One of which, had 2 duplicates.
#HenkHolterman You are correct that this is going to be a compression algorithm, however, I was not sure where to start for looking for the sequences that I will be matching.
I had been doing multiple searches regarding something similar to this, but was coming up short with the answers I were looking for. That is why my question was posed here the way it was.
Thank you for all the responses I have gotten so far though!
Here's the basic brute force idea
first you find all repeating sequences of size 1(you can change the minimum size to whatever you want).
To do this, you essentially go down the line, and use a regex to find all of the Ts and then all the hs, etc...
Then you find all sequences of size 2, so you'd find all the Ths and the his and the iss
you repeat this until you have found all of the sequences.
The runtime would be
the time complexity to find a particular sequence with regex: O(n)
times the number of different sequences of a particular size: O(n)
times the number of sizes: O(n)
the total time complexity would be O(n3)
Use a suffix tree to do this in O(n) time. I am adding this extraneous sentence to keep this from being converted into a comment.

Slow iteration through elements in WatiN

I'm writing an application with Watin. Its great, but running a performance analysis on my program, over 50% of execution time is spent looping through lists of elements.
For example:
foreach (TextField bT in browser.TextFields)
{
Is very slow.
I seem to remember seeing somewhere there is a faster way of doing this in WatiN, but unfortunately I can't find the page again.
Accessing the number of elements also seems to be slow, eg;
browser.CheckBoxes.Count
Thanks for any tips,
Chris
I think I could answer you better if I had a better idea of what you were trying to do, but I can share some observations on what I've learned with WatiN so far.
The more specific your selectors are, the faster things will go. Avoid using "browser.Elements" as that is really generic. I'm not sure that it saves much, but doing something like browser.Body.Elements throws the header elements out of the scope of things to check and may save a few calculations.
When I say "scope", consider that WatiN always starts with the entire DOM. Can you think of ways to limit the scope of elements perhaps to the text fields within the main div on your page? WatiN returns Elements and ElementCollections, each of which may have its own ElementCollection. That div probably has a specific ID, so you can do something like
var textFields = ie.Div("divId").TextFields;
Look for opportunities to be more specific, and you can use LINQ to describe what you want more clearly. For example, can you write something like:
ie.Body.TextFields.
Where(tf => !string.IsNullOrWhiteSpace(tf.ClassName) && tf.ClassName.Contains("classname")).ToList().
Foreach(tf => tf.Value = "Your Text");
I would refine that further by reducing the number of times I scan the collection by doing something like:
ie.Body.TextFields.ToList().
Foreach(tf => {
if(!string.IsNullOrWhiteSpace(tf.ClassName) && tf.ClassName.Contains("classname")) {
tf => tf.Value = "Your Text"
}
});
The "Find.By*" specifiers also help WatiN operate on the collections you want faster and are a more elegant short-hand for what I wrote above:
ie.Body.TextFields.Filter(Find.ByClass("class")).ToList().ForEach(tf => tf.Value = "Your Text");
And as a last piece of advice, this project lets you find elements using jQuery/CSS style selectors.
So, tl;dr: Narrow down the scope of what you're looking for, and be specific.
Hope that helps. I'm looking for ways to speed up my own tests.
If you really need to iterate through all text fields, there is no other way. As #Xaqron pointed out, it depends on IE. But maybe you just need to iterate through text fields of eg. specified <div/>? Finding it first, and then iterating through it's text fields would be faster.
Thanks Dahv for a really detailed answer. In my case I've sped up my tests by about 10x using a number of tricks, some similar to yours:
Refining scope as you and prostynick (in my case using Form1.TextField etc.)
First checking if browser.html matches my regex before seeing if
fields do
Using the GehSoft.PRCE RegEx wrapper - its native code regex
matching is far faster than .NET's for small haystacks. So to find a TextField I'd do:
Gehtsoft.PCRE.Regex regexString = new Gehtsoft.PCRE.Regex("[Nn]ame");
foreach (TextField bT in browser.TextFields)
{
//Skip if no match
if (!regexString.Execute(bT.Name).Success) continue;
Before I was looping on a list of regexes, then inside that i was looping on TextFields. Making the TextFields loop the top loop improved speed about 3x.

Adding blank space as one of my variable

I want to write some data about the person's in a file. And while adding those data i want to add blank space if a particular data for that person is not available.
ex: I want to have " " in place of SSN if person doesn't have one.
Thanks,
Uhm... sorry for this kind of answer, but I really recommend you read up on file I/O. As Brad said, it's kinda unclear what your problem is.
I, for one, think your problem is not actually a problem: if you know how to print to the file all the other information, you surely must know how to add some blanks.
Again, sorry I'm not giving a straight answer, but trust me: I could tell you ten different ways of handling record-keeping when certain data is undefined for a record, and you would still learn much more (and much more solidly) reading a tutorial or two.
If all you're doing is writing out variables from an object, you could just use the String.IsNullOrEmpty method before you return the value you want to write.
Something like
return !String.IsNullOrEmpty(SSN) ? SSN : " ";

Categories