Can I decide the number of rectangles in a grid at runtime? - c#

I'm basically having the same problem as this guy. The thing is, I want there to be a certain number of rectangles on the screen, depending on the screen size/resolution. Now the first comment on that question says "creating UI elements in procedural code in XAML-based technologies is discouraged, cumbersome, probably a bad idea, and just plain wrong".
So is there another way in which I can achieve my goal?
One way I can think of would be to have a minimum of 3x3 rectangles, but add UI code for 6x3 and make the last 3 rows visibility collapsed. In app load, I could figure out if I could fit more rows, and selectively make them visible. I'd still like to know if there is a cleaner approach.
I won't paste my code here since it's very similar to the linked question, but I will if asked, and it helps to clarify things a bit.

I understand the general sentiment that a XAML app should declare as much of the UI as possible in the XAML. But I also believe it's possible to be too dogmatic about things. If one genuinely has a scenario where the layout of UI elements one would otherwise select during design-time is unknown until run-time, then why not populate the UI at run-time based on that information?
Barring some clearly-written and compelling argument detailing why run-time creation of UI elements is actually bad, I'm going to keep on doing it when appropriate. I will happily design as much of the UI in advance, but if there are parts that depend on information known only at run-time, I will write code to do that.
A person can wind up with a lot of really awful code and maintenance hassles trying to shoe-horn a specific design goal into some constrictive, prescriptive dogma.
I went ahead and posted an answer to the question you've linked to. I don't know if that's actually something you needed, but if so you can look there. It seems to me that the main question here is "is it really so wrong to configure my UI at run-time?" and my answer to that is, as you can see, "no, not necessarily".

Related

Using FRP (and immutable collections) with many objects, shown on a grid

I've been reading about FRP lately and really liked the idea so far. There are a couple of things I haven't found much information about, and one of them is this: If I use, for example, a WPF grid for displaying my data (with some thousands of rows), how should I wire the updates to the grid?
I should note that I have relatively little experience with WPF too, but as far as I understand, this would usually be solved by binding the grid to an ObservableCollection<T>, which, if I understand correctly, is very good at handling the updates (meaning updating, adding or removing single items is handled with good performance).
So then, if I wanted to use FRP with immutable collections, this isn't applicable. But what other options are there? If I re-fill the whole grid with each and every update, add or delete, I would guess the performance is quite appalling.
I also don't really have example code to show because, frankly, this is such a basic thing that I wouldn't know where to start without knowing how to do this first. And while I'm specifically talking about C# and WPF in here, I'm also interested in the general principles.

Template pattern - not useful for small projects

I'm sorry to ask such a localized question but until I get confirmation I don't feel confident moving on with my project.
I have read lots about the template pattern, Wikipedia has a good example.
It shows that you create the basic virtual methods and then inherit the base class and override where you want. The example on the site is for Monopoly and Chess which both inherit the base class.
So, my question is, if you had an application which was only going to be Chess and never anything else, would there be any benefit in using the template pattern (other than as an education exercise)?
No, I think that falls under the category of "You Ain't Gonna Need It."
To be more specific, design patterns exist to solve a particular problem, and if your code doesn't need to solve that problem, all they do is add lines of code without having any benefit.
No. Expressed in a very simplified and superficial way, the template pattern is just worthwhile starting at a certain relationship between total code size and templated code size. In your example, the chess game is going to be the entire program, so there'll be no need to use the template pattern here.
The template pattern is used in specific situations. It is used when you want to sketch out an algorithm but let the specific steps differ.
This could be useful in a Chess application. However, you should not start developing an application with the idea 'I'm going to use this pattern and that one and..'. Instead, you develop the code and you discover that you need certain patterns.
This is where a Test Driven Development approach is really handy. It allows you to refactor your code each step of the way.
A nice book that explains this is Refactoring To Patterns.
I would suggest writing your chess game and then if in the future coming back and changing things to fit monopoly too. But its something totally different if you want to use the pattern to learn the pattern, in that case its good to start simple so the complex is easier to understand.
It really depends on the parts of the program. The whole idea of Template is to have an algorithm that never changes and to be able to add or edit certain steps of that algorithm.
It may well be that you never change, however, this is the issue with design principles, it IS good practice and you may later wish you'd implemented them. I would say though that if you are 100% sure then you can leave it out as it usually saves time and lines of code. Depends if you want to learn Template usage or not.
Also the GOF principles website is quite good:

Checking-in Designer Generated code into TFS, issues

I just had a conversation with my manager relating to checkin\out policies on a project I'm currently working on. Basically I tried to edit a file that was already checked out by another developer and I couldn't - I asked my manager why we couldn't edit the same class at the same time and he gave this reason for turning that functionality off: We had a lot of problems with developers editing the same Form (or anything visual done in the designer) and then cheking it in. Merging the changes in the designer generated code was a lot of hassle...
As I'm writing this I'm struggling to see what problem they were having - surely they were getting the latest code before trying to check something in??
Have any of you come across problems with editing the same Form (or something in the designer) as another developer and then checking into TFS? If so how did your team get around the problem? Did you also turn off the ability for developers to work on the same class?
EDIT: The following post (found here) is exactly the problem my manager was describing. Anyone know of a simpler way to resolve the issue than the ones in that post?
I would argue that the solution to your problem would be to establish best practices for source code modification.
Discourage people from going into UI code and arbitrarily jiggling the components around in the designer. Any reasonable UI modifications should be easily mergeable. Your best bet is to try and educate people as to the best way to merge in any given source control system. Also, as helpful as the designer is, ignorance of what code is being automatically generated in the background will be significantly detrimental in the long-term.
People who insist on locking checked-out files for the reasons you stated in your post typically wait long periods of time to check their code in. Naturally, the more time passes, the more code gets modified, so it makes merging difficult for these people. Checking in early, often, and incrementally requires people to think about their changes in stages, and for some coders, this is a rather painful cultural/psychological adjustment.
I've just checked back through the histories of some of my .designer.cs files and I can't see any changes that would cause a merge problems. There were no wholesale rearrangements of code for example.
Another thing to consider is to make sure that everyone does a "get latest" at regular intervals then any individual merge/resolution isn't going to be that great thus minimising the chances of anything going wrong.
It might also be worth investigating a 3rd party merge tool. There are plenty around.
Now it could be that the changes I've done are simple compared to the ones you've got so you should take my anecdotal data with a pinch of salt.
It can cause problems (in general) when a lot of people are editing UI concurrently. The merge logic will do a fine job merging things, but in a lot of cases the UI is drawn according to how things are added to the form. Your UI can get messed up quickly.
I don't know if I would use this as an excuse to enforce exclusive checkouts across the board, though. I might go from a (non programmatic) policy standpoint that says shared checkout for business logic, but exclusive for UI changes.
I would couple that with a strong MVP, MVC, or MVVM approach, though, which should limit the number of people that have to touch the UI concurrently.
As others have alluded to, keep one of the seminal rules of SCM in mind: merge early and often, and your problems are reduced. (along with that is "always get latest before you start working on the code).

Custom components vs default components in GUI Design. Which is more practical? (.NET)

Ok, so is it better to create my own components (Either inherit them from existing ones or creating them from scratch) or to use the ones that come by default?
I'm asking this under the umbrella of scalability, overhead and other factors that my contribute on each of the choices.
UPDATE: Sorry if I'm to vague. I don't really know how to explain myself better. Isn't it a good idea to inherit each default component? For example what if I'm to design a rather large application and for example i need to change the display format on a DateTimePicker. Wouldn't it better to think ahead and inherit it and use the inherited component instead of the defaults?
But, wouldn't it add a lot of overhead in small applications?
This is a difficult question to answer, and may get closed as subjective. It depends on what you are doing, and whether the supportability of scaling it over an existing component is less than that of wrapping a whole new component (inherited or not) and all the entailing support work.
For instance, suppose we have a textbox where we always want to have the text be green. We could just set the font property for the textbox and be done, that would be simple and straight forward, and not need a custom component. However, what if we wanted it to always be green, not be changeable and be enforced regardless of what might be put into the textbox? That would require more code and a certain amount of design work in the app, or we could create a custom component to do this work for us, and have it appear as a textbox to everyone who implements it. Simplifying the deployed code in essence (by hiding it inside the control) and making the overall design cleaner. If we only need this textbox in one place, it is probably overkill, however if this textbox is pivotal to our app it might be worth the time.
I don't think your question has a certain answer - it depends on the functionality that you need. If standard components fulfill your needs then I don't think creation of your own components makes sense.

C# penalty for number of lines of code?

Are there limits or performance penalties on the amount of code inside of my home.cs form?
I am writing a database application front-end in C# in Visual Studio 2008. The way things are lining up, I am using a tab-page way of changing the info shown to the end users, instead of using new forms.
Coming from VBA/MS Access, I remember that if you go over a certain number of lines of code, it would produce an error and not compile. Will C# do this in Visual Studio 2008, or will I suffer a performance hit? I know code readability could be a problem because everything would be in one place, but I can also see that as an advantage in some situations.
It's not the lines of code in your .cs files that you need to be worried about with regards to performance - it's the number of controls on your form at runtime that might cause problems. If it's just a few controls on a few tabs, you will have no problems. If it's hundreds of controls on lots of tabs, you may have performance problems (not to mention usability problems - I personally hate tab controls with more than one row of tabs).
Also, I don't think tabs are appropriate if the purpose of the UI is more wizard-like where you want the user to interact with all of the tabs in succession. Tabs are meant for presenting sets of options to the user, without requiring them to see all the options at once.
Finally, if the purpose of each tab is significantly different, I find that it's easier to encapsulate each bit of functionality as a separate form. With tabs, you could at least encapsulate each bit as usercontrols, and then have each tab on your form host one instance of a usercontrol.
The only problem i would foresee is that in the future its going to be very hard to maintain.
Try break the logic of that main form up as much as possible into classes so that when you need add something you can actually do it without having a fit.
If you are using tabs, you can still create custom user controls that will hold the content that goes in the tabs. Make a control per tab, and then you can keep your code for the different tabs separate. There is a walk-through on MSDN here.
In response to your comment above, about not showing the tabs, I would really re-think how you're approaching this. Why not simply have all of your user controls sitting on your main form, in a Panel if necessary, have them all set to Dock = DockStyle.Fill, and then change the Visible and Enabled properties based on which one you want to show? You may be making this harder on yourself than it needs to be.
More responses to comments - You may be looking for something like the CardLayout in Java. The source for the GNU Classpath version can be found here, it might give you some ideas on how to implement this.
"I know code readability could be a problem because everything would be in one place, but I can also see that as an advantage in some situations."
In my experience, this attitude will ultimately leave anyone who has to maintain your code in the future with quite a headache, as it's an accepted practice to modularize your code so that the pieces that may change or the ones that serve distinctly different purposes are separated away from each other.
With that said, I don't think there is a limit imposed by VS on the length of your files, but I think you will run into some seriously frustrating performance degradation as your files become longer, especially while switching between design and code views.
I would urge you to save your future self and his/her sanity and break your code up logically into separate files. You'll thank yourself later!
It shouldn't be a problem.
Just keep good coding practices in mind and modularise your code for readability and maintainability.
On the other hand, if you put too many controls on your form, then it will probably take longer to load. Factor that into your design if you want a snappy interface.
Sounds horrible but I don't see any reason why it would be a problem.
I have a form that in inherited with my new job that had over 30,000 Lines. It is completely cancerous. Please think before you code and modularize!

Categories

Resources