|
With design patterns, first you need to answer few questions by yourself. Questions like:
1. When to use which pattern?
2. Do I need to use XYZ pattern in my application?
Once you have identified an appropriate pattern, look for code samples on CP or anywhere else you prefer.
|
|
|
|
|
That's a lot like asking "I have 23 different tools in my shed, what should I build so that I get to use some tools? I don't know how to use tools in a project."
It's a little silly, and remember, design patterns are means, not ends. They're not magic faerie dust that you can sprinkle on your project to make it cool, they're pre-made ways to solve problems that show up from time to time (some more often than others).
|
|
|
|
|
Patterns describe problems and a way to solve them.
If you don´t see a problem in your project, you don't have to look up the patterns.
Critical thing is to see/smell the problems (same as with refactoring) : browse over the problem descriptions, keep your eyes open and at some point in time you find a match.
|
|
|
|
|
It is not important to use design pattern.Design patterns are used for a specific problem domains.May be you already used one of them but you never know.First you should understand the design pattern then implement it on your code.
|
|
|
|
|
You've got this all backwards. First you need to work out what problems you are trying to solve in your project, and then you can look at design patterns to see if someone has already done a partial solution to some or all of them.
The most common in my experience are simple ones like observer (though not really in C# as delegates and events do all the work for you) and boundary interfaces, which are so simple as to barely count as patterns at all. An MVP or MVVM variant is also very common.
Are those in the seminal 'Gang of Four' design patterns book? I don't know ... and that's the point, a pattern is not part of a gospel truth handed down by the gods of programming, it is advice and a worked example that you can modify to your needs. It doesn't matter if it comes from That Book, or a blog, or a website or a colleague's brain, if it fits the purpose.
|
|
|
|
|
Hi, I have the following issue with my WinForms C# project and wonder if someone could point me to a solution...
I have a reference to a 3rd party DLL (which I am charged for and is licensed on a per end-user basis). The dll allows me to run certain functionality within my project. Ideally I'd like to to check if the Licence file \ DLL exists (I can do this) then if it exists, proceed as normal, but if it doesn't exists, then remove access to the functionality the Dll provides. The issue I have is that I have references in the project to the DLL, so if the licence file is not there or the DLL file is not present, the app explodes on startup. Is there any way I can get around this issue?
Many thanks.
|
|
|
|
|
Good question. I found this article: Loading an Assembly[^]
If that doesn't solve the problem, you can dynamically load the assembly with the Assembly.Load[^] method, and then create objects from it with the Activator.CreateInstance[^] method.
This will allow you to remove the static reference from your project.
Also, see this article about early and late binding: Early and Late Binding[^]
EDIT: I apologize, that article is about VB. But if you search on "Late Binding C#" you'll find good information.
The difficult we do right away...
...the impossible takes slightly longer.
modified 3-Mar-14 15:03pm.
|
|
|
|
|
Richard, many thanks for taking the time to reply. I'll check out the links you kindly posted.
|
|
|
|
|
What I would do is have the functionality declared in an external assembly - the classes I wanted would implement interfaces stored in a DLL or the Exe and I would then use MEF to wire things together. This would allow you to use the interface as your type in the main program.
|
|
|
|
|
MEF?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
|
You can actually catch the exception that gets thrown when a class can't be loaded. The trick is that the class must not be used in your Main method, because entering a method which uses a class the point that the class binder tries to load it, and you can't catch it.
So you can do something like
static void Main() {
try { EntryPoint(); }
catch(FileNotFoundException) {
}
}
static void EntryPoint() {
var x = new DependencyClass();
}
The 'correct' way to do it is to handle Application.AssemblyResolve but what I've described here can be useful if all you want to do is tell the user they need to install something which is missing in a more graceful way.
|
|
|
|
|
Thanks BobJanova, this might be all I need.
|
|
|
|
|
how to divided cell in Datagrid view. My code is don't work in fraction number. Someone advise me. Thanks you all.
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
int R1 = row.Index;
for (b = 0; b < dataGridView2.ColumnCount; b++)
{
a = 0;
for (c = 0; c < dataGridView2.RowCount; c++)
{
Ans_Grid.Rows[R1].Cells[b].Value =
(((Double.Parse(dataGridView1.Rows[R1].Cells[c].Value.ToString()))
* (Double.Parse(dataGridView2.Rows[c].Cells[b].Value.ToString())))).ToString()
;
Ans_Grid.Rows[R1].Cells[b].Value = a + Convert.ToInt16(Ans_Grid.Rows[R1].Cells[b].Value);
a = Convert.ToDouble(Ans_Grid.Rows[R1].Cells[b].Value);
|
|
|
|
|
may be decimal will help you in getting your issue fixed
Mouli
|
|
|
|
|
<pre>
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Boolean;
namespace Applica
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo da = new DirectoryInfo("C:\\Folder9");
FileInfo[] Arr = da.GetFiles();
FileInfo ap = Arr[Arr.Length - 1];
long Totbyte = ap.Length;
string filePath = ap.FullName;
string temPath = Path.GetTempFileName();
byte[] data = File.ReadAllBytes(filePath);
File.WriteAllBytes(temPath, data);
byte[] dataB = new byte[1];
BitArray bits = new BitArray(dataB);
for (uint counter = 0; counter < Totbyte; counter++)
{
dataB[0] = data[counter];
for (uint count = 0; count < bits.Length; count++)
{
if (bits[count] == 0)
Console.Write(bits[count] ? "1" : "0");
}
}
}
}
}
|
|
|
|
|
Totbyte is a long. Your loop is a uint.
|
|
|
|
|
Even if I change that variable to a constant, the error still exist.
|
|
|
|
|
My error is in the last 2 lines of the code:
bits[count]
|
|
|
|
|
|
I changed it to an int and I still get an error.
|
|
|
|
|
The same one or a new one?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
|
He did not insult you, he asked you a perfectly reasonable question. You have stated that you get an error, but you have given no details of what that error is. Is it "invalid argument", and if so on what line of code. If it is something different then show the details. But in either case please be clear what the problem is and where it occurs, and people will try to help you. But if you resort to insults and rudeness you are likely to get no help at all.
|
|
|
|
|
Can you comment all your code?
Then I will fix it for you.
PS: always check variable is null or not, if you are not sure before calling it
|
|
|
|