|
I have inherited a load of code and I am seeing things that I find troubling but Im not sure how serious they are. Im coming from years of experience with C++ and java, but Im relatively new to C#.
Some troubling things....
- One class is 5500 lines alone, another 4000. These are STATIC classes! I have never seen so many statics in one codebase before. Classes, methods, variables, etc. Lots of classes like this. My philosophy has been to prefer instantiable singletons over statics unless it is a small lightweight utility class. Is there a good reason to have so many statics?
- I have classes with over 20000 lines of code in ONE class. Not static thank god but still. There are probably about 15 classes over 10000 lines each. How can I refactor or manage this code? Lots of cut and paste obviously.
- There are 180,000 lines of code, and only 2 namespaces encompasing all of it. Shouldn't namespaces be used similar to packages in java? Each package helps to break down the code base into functional groups, right? Or is a namespace in C# different?
These seem like reasons to be concerned, if not run out the door right away... thoughts?
|
|
|
|
|
saxisa wrote: Is there a good reason to have so many statics?
It's just an idiom. I prefer to write static classes, but a class with a static Instance property works just as well. What is the difference ? If a class does not have state, then static makes sense to me.
The big classes are another thing.
saxisa wrote: How can I refactor or manage this code?
If you want to seperate in to smaller files, then you need to use partial classes. If you want to create smaller classes, it's up to you to decide how to break one class in to many.
saxisa wrote: There are 180,000 lines of code, and only 2 namespaces encompasing all of it. Shouldn't namespaces be used similar to packages in java?
Namespaces work in a similar way to C++, but dumbed down a little, like most of C#. I like to break things up in to folders when I create a project, and inherit namespaces from there, but I don't see anything inherently wrong with having less namespaces, so long as nothing clashes.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Christian Graus wrote: It's just an idiom. I prefer to write static classes, but a class with a static Instance property works just as well. What is the difference ? If a class does not have state, then static makes sense to me.
Yes, It's just an idiom. But do you think creating a class (static) is reasonable just for grouping a set of methods? This is where C++'s free (non member) functions takes advantage. It will not force to create classes just for putting some methods. I really wish to see free functions in C#.
|
|
|
|
|
Hi,
I agree with what Christian said. I'll add some in my own words:
1. I don't mind static classes or singleton classes. When there is no state at all I think I'd prefer a static class over a singleton, since there is no real use in an instance then.
2. I don't mind large classes if they make sense. If they can be logically split into smaller ones, I'd go for that. Although it puts more burden on the user (remembering which method is in which class).
3. I would mind large methods very much. When refactoring, I'd go for smaller methods, and probably some internal helper classes. And use many files. I tend to keep files smaller than 400 lines.
4. yes namespaces are like packages (without the relation to folders though); having only a few does not hurt, until you get name clashes.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
In C#, I prefer static classes to classical singletons. I believe static classes were created to provide a more fool-proof way of writing singletons.
Big classes and big methods may be a code smell, but if you can't find ways to reduce them then they're fine. Some times classes and methods just need to be big.
I use only a few namespaces; I may have X.Y.Data , X.Y.Library , X.Y.Forms , etc.
I don't know Java, but I would assume that a .net assembly would be similar to a package.
An assembly may contain many namespaces, and a namespace may span many assemblies.
|
|
|
|
|
is there a best practice for mapping the work flow for the application menu, toolbar and the actual handler for the tasks behind the menu/toolbar items?
----------------------------------------------------------
Lorem ipsum dolor sit amet.
|
|
|
|
|
Hi,
when actions can be commanded in several ways (e.g. menu item, toolbar icon, ...) I tend to create a Command class, which sits in between the GUI and the business model. So the business layer can say enable/disable the Command without knowing any GUI details, allowing easy GUI redesigns (moving menu items around, adding/changing buttons, etc).
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Forgot to mention, there are several patterns that generalize and formalize this.
Google for MVC (model-view-controller) and MVP (model-view-presenter).
I haven't fully understood them yet. IMO my Command classes are a pragmatic subset of MVC.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Hi,
i try to build a user interface like Paint, or a primitive kind of it.
It should be possible to click onto a picturebox and write something to that location.
I don't know where and when to initialise the textbox-object. Before runtime and just "activate" it when text is written, or dynamical if needed?
What is the best appoach to realise that the user can just click onto the picturebox (if the PrintText button was pressed) and get a curser where the text can be written.
Hopefully somebody can help.
Bye,
Michael
|
|
|
|
|
Hi,
just throwing Controls on top of each other won't cut it. If you want to create a paint program, using several controls is not the way to go. You really need:
- a single control that shows it all; I would use a simple Panel;
- a data structure that holds all the different objects that constitute your artwork; you might want to define a DrawableItem class, and derive specific objects from that; then collect them in a List<DrawableItem>
- a Paint handler that paints everything in the list;
- some Mouse handlers to click and selects DrawableItems so you can move, resize, delete them.
- etc.
Have a look at some of the paint articles here on CP, or Google for Paint.NET
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Thanks for your post.
To be more specific: it's a program to transformate and tesselate a picturebox. The design of this picturebox is just the first step.
I already use a class to manage all the different objects. In this class i have a list of GraphicsPath Objects, each connected to a pen which was used for this object.
Painting freehand-curves, regular shapes and other stuff works fine, but i don't get how to deal with the dynamic textbox.
I take a look at the paint articles, but i've already searched google with all keywords -suitable to this issue- i could think of.
|
|
|
|
|
Of course, you might choose to use a TextBox for inputting the text. But once the string becomes part of the artwork, the text is just a drawable item like the rest of them. Whether you use Graphics. DrawLine() or Graphics.DrawString() doesn't make any difference.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Thats fine. I want to save it as a GraphicsPath-textobject anyway and handle it like all the other graphic objects.
But how to put the dynamic text into the pb? You see where my prob is?
|
|
|
|
|
NewJavaBean wrote: You see where my prob is?
Not really. How do you draw a vertical line? how do you draw a rectangle? the only thing a PictureBox helps you to achieve is showing a single, full-sized image. Everything else needs code in the Paint handler, so why not do it all yourself, in the Paint handler of the Control you choose (I choose Panel instead of PictureBox).
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Ok i see
Lets rephrase myself. I'm not sure how to interact with the user.
It should be this way:
User activates the button to draw some text.
User clicks onto the PictureBox and a cursor displays at this point.
User writes a line.
After this point i sould handle it, but how to get there?
Do i actually need a textbox or should i try it another way?
|
|
|
|
|
That is entirely up to you. Here are some possibilities; they all share a "trigger", i.e. something telling your app that text is going to be added (could be a button, menu item, whatever):
1. have the user click on the PB, then hit the trigger; now create a dynamic textbox and add it to PictureBox.Controls giving it the clicked location. Enter the text, upon ENTER read the text, remove the TB from the Controls, add the text to the drawing.
2. without a TB: have the user click on the PB, then hit the trigger; now, let him type text and add it immediately to the drawing until he terminates with ENTER. Here you don't have a cursor, no copy/paste, and you need to support backspace/delete for editing. Is probably more visually pleasing though, as you see the results immediately.
3. with a static TB outside the PB.
4. with a text entry dialog.
1. uses the trigger event, and the TB.KeyDown event to check for ENTER.
2. uses the trigger event, and the PB.KeyPress event (for text) and PB.KeyDown event (for ENTER).
BTW: Once more, I would use a Panel, not a PB.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Thanks Luk thats what i wanted to know.
So i have to create the TexBox by my own, what usually the Designer does for me, and destroy it afterwards.
And i've heard you with the panel
But this is a group project and we've got more than one PB in the program.
The other students would probably kill me if i change such major things in this state of the project.
So now im gonna sleep, its late.
Good night... or day, according to your local timezone
Thanks again!
|
|
|
|
|
NewJavaBean wrote: i try to build a user interface like Paint, or a primitive kind of it.
I did not read all this thread, but at the core, a picturebox is an absolute waste of time here, and can make things harder. Handle your own paint event and go from there.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Given a shape that has the form of an "L", does anyone know how to render a gradient border that follows the shape of the "L"?
|
|
|
|
|
You would do two gradients, or do one that radiates from the bottom right, so that it follows both lines in the L. A more complex letter would be a lot harder, obviously.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Hi,
my experience with PathGradientBrush is limited to a simple case[^], however I would say you should provide intermediate points and colors in any way that suits you.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
You might want to take a look at Rich Design Time Editing with UITypeEditors (VB.NET)[^], as you can see from the title its primary target is TypeEditors, but it demonstrates this by using various fills and paths. It is in VB.Net, hope that is not a problem.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
i want to fetch the characters from file for example :
1st example:
// mehmood// hello ,how r u?
just within comments and replace it to all caps
2nd example:
/* hi
mehmood
ahmed */
i want to fetch the characters within sigle line comment and multi line comments .
just within comments not outside the comment.
--------------------------------------------------------------------------------
i am trying this code, kindly correct my code:
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
StreamReader sr = new StreamReader(fs);
int countComment=0;
while(sr.Peek() >= 0 )
{
str =sr.ReadLine();
strTemp = new char[str.Length];
#region From String method
int count=0;
for (int i = 0; i < str.Length; i++)
{
if (str[i] == '/')
{
++countComment;
count=i;
}
if (char.IsLetterOrDigit(str,count+1) && countComment==2)
{
strTemp2 += str.Substring(count+1).ToUpper();
}
}
#endregion
countComment = 0;
this.textBox2.Text += strTemp2 + Environment.NewLine; ;
this.textBox1.Text += str + Environment.NewLine;
}
sr.Close();
fs.Close();
it generates an exception :
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index
Maifs
modified on Monday, July 13, 2009 5:08 PM
|
|
|
|
|
Hi,
you need to debug your code, and you should do that yourself.
Here are some hints:
- an exception contains a message and a stack traceback indicating class and method names, file names, and line numbers (when available; use debug mode!)
- when you try-catch an exception, make sure to show the ENTIRE exception, use Exception.ToString()
- teach your IDE to always show line numbers in editor windows; for Visual Studio, look under menu Tools/Options/TextEditor/AllLanguages
and if you must show us some code please use PRE tags (that's under the "code block" widget above the text edit box)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Issues
1 - the code is a mess
2 - it won't work
3 - it's hard to read
Do you not know how to use the debugger ? The error message is telling you what is wrong, what is the issue ?
maifs wrote: if (char.IsLetterOrDigit(str,count+1) && countComment==2)
{
strTemp2 += str.Substring(count+1).ToUpper();
}
Why do you use -1 AND +1 ? What if +1 is out of range ( wait, isn't that what the error message says ? )
This needs a lot of work. I'd read a C# book to learn some basics, then rewrite it using regex and the split method of the string class.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|