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!
Related
Hello everybody,
around two or three month ago I started to learn Dynamo for Revit... finally :)
After learning and testing a lot, I got a few own scripts working. Then I learned Python, because I couldn't create the next script only with Dynamo-Nodes.
Then I thought "Let's see how difficult it is to get something done as a PlugIn".
I watched some Videos and read a lot of stuff.
Finally I got the Revit-AddIn-Wizard installed and made my first small Test-PlugIn.
Great...
Now I have a few problems which I do not understand... so I thought I will try my luck here... because I got so much information and help, reading through this site.
My goal was/is the following: (I tell you what I have now)
A form with a few buttons, comboboxes and a DataGridView.
I can load an Excelfile, click on "Show" to show it in the DataGridView.
The header of each row will be automatically put into 3 comboboxes.
In the first combobox you select the first search-parameter, in the second you CAN select another search-parameter and in the third combobox you select the parameter you want to set.
I have a checkbox to switch from type- to instance-parameter for the search- and the set-operation.
There is also a button which shows another small form with a list of categories (I won't search for ALL, only nearly all modelcategories).
PlugIn
I took me a lot of "watching Videos, reading through the internet, testing, testing and testing".
Thanks to this site here and a few others... I managed to get this whole PlugIn nearly 100% working.
But now I have a few strange issues and I have absolutely no clue on how to fix them or if it is possible. And I really hope that someone can help me.
First... I just tell you my problems and perhaps someone can say "this really IS an issue!" or that it is possible to get it done. Then I would post some code.
So... what do I do?!
1. I have a FilteredElementCollector which filters ALL elements.
2. Depending on my "Type/Instance-Checkboxes" I do .WhereElementIsElementType OR .WhereElementIsNotElementType.
3. Then it passes a MultiCategoryFilter to get the big list down to only the modelcategories.
4. Next, the collection passes one of ten different "methods" depending on all settings. There I filter this collection depending on the searchlists-comboboxes. When the combobox says "Familie" or "Typ" then it filters for ".BuiltInParameter.SymbolFamilyName" or ".Name" otherwise it just uses ".LookupParameter".
After that I have a collection with only the elements of selected categories which contains the values from the Excellist.
5. Depending on what my search- and set-settings are (e.g. search for type and set instance) I have to get the instances from the collected types or the other way around.
6. Then I pass it down to another method where I finally set the parameter.
So... Excelheader goes into comboboxes, depending on what you select in there it creates lists with the values of the selected rows.
I hope you all understand.
Now... where are my problems?
When I search for type-familynames or instance-parameter and set a typeparameter it works for ALL categories without any error.
1. When I try to set an instanceparameter (doesn't matter what my search-setting are) it works for all "normal" families but not for the systemfamilies (e.g. walls, floors, pipes etc.). No error, just nothing happens WHY? It seems that I cannot set an instance-parameter for system-families.
2. Roofs, Stairs, CurtainPanels and GenericModel make problems when I search for a typeparameter Error is something like "The object reference was not set to an object instance". Only with these 4 categories and it doesn't matter what I want to set... but when I search for family-/typeNAME or Instance-Parameter, then I can set type or instance and it works (except instance for sysfam).
3. When I try to search AND set an instance-parameter it works for ALL categories EXCEPT if one wall does not contain a search value... it really is enough that ONE wall does not have a search-param-value that everything will be cancelled.
I have a few other small problems... but I hope someone can help me with these problems... I would be extremely thankfull
greetings and have a nice day or night :)
Philipp
Tl; dr.
The three problems you describe sound like your own. I have no heard anybody else runAsk three separate questions and provide three separate minimal code snippets describing how they arise,. into those. I suggest that you create three separate independent minimal reproducible cases to demonstrate all three issues. Chances are, when you simplify and minimalise your code, the problem will go away. If it does not, it might just possibly be in a small and manageable enough state for other people to help you take a look at it. Given the long-winded description above, nobody in the world can help you.
Thank you for your answer Jeremy,
as I said, as a first start it is ok for me if you don't say "With theses categories, there are indeed some issues!"
I think I've managed to create 3 small examples of my problems.
For each problem I made a zip-file containing the complete visual-studio folder, a small exampleproject and a readme.txt with (I hope) enough information to understand everything in detail.
Problem1
Problem3
You only need to compile them or copy the .addin and .ddl files into the Revit AddIn folder. Then you get the new ribbons.
Short problem summary = I get problems when searching for parametervalues and setting values to another parameter.
Edit: I just solved the 2. problem when searching for familynames and setting system-families-parameter.
I used:
ElementClassFilter ecf = new ElementClassFilter(typeof(FamilyInstance));
FilteredElementColletor colle2 = new FilteredElementCollector(doc);
colle2.WherePasses(ecf);
I simply deleted the ClassFilter and do it now like in the other cases where I need instances.
FilteredElementCollector colle2 = new FilteredElementCollector(doc);
colle2.WhereElementIsNotElementType();
The 1. and 3. problem still exist :/
I would be thankful for any help someone can provide :)
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.
In C#, I have a 2d array of labels who's image I want to change depending on conditions. More specifically, I'd like to get it toggle between a given image and no image at all (turning it into a transparent label) and back again when conditions are met. Currently, to wipe the label clear again, I'm using this:
someLabelArray[i][j].Image = null;
But it always throws a Null Reference Exception, which makes me suspect this isn't the 'right' way to do things. Or perhaps I'm misunderstanding the error? Is there a better way to do this?
I suspect that would be absolutely fine - but that either someLabelArray is null, or someLabelArray[i] is null for whatever value of i you're using.
Simple way to test this: change your code to:
someLabelArray[i][j].Text = "foo";
which obviously has nothing to do with images, and will definitely work if your array is okay. I suspect this will fail in the same way, in which case you need to look carefully at how you're constructing your array.
If this works - well, I'll have another look then :)
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 : " ";
I'm playing with the SubSonic RESTHandler for the first time and it's awesome... There is one quirk tho, that I'm curious about.
RESTHandler.cs (line 319):
//if this column is a string, by default do a fuzzy search
if(comp == Comparison.Like || column.IsString)
{
comp = Comparison.Like;
paramValue = String.Concat("%", paramValue, "%");
}
This little blurp of code forces all searches on string columns to wildcard searches by default. This seems counter intutive, since you've provided a nice set of comparisons we can add to a parameter (_is, _notequal, etc...). Is there a reason this was done? The EvalComparison uses "Comparison.Equals" as it's default, so unless a like is explicitly needed the " || column.IsString" looks like it should be removed since it breaks the ability to use different types of comparisons.
This was driving me crazy, since you can't do a "WHERE Field = X" without modifiying code...
Just curious if this is more of a feature than a bug...
Thanks!
Zach
It's because this is a LIKE operation which for a DB usually allows string operations. The feeling at the time was that if you wanted equals you could just use that.
It's been a while since I've touched this code - if you'd be kind enough to open a bug I'll take a look at it.
It does indeed look like a feature. It's based on the idea that, if I am searching for a string in a column without the wildcards, I must match the string exactly or I get no hits. I suspect that this was done to make programming search textboxes easier.