|
PIEBALDconsult wrote: why then doesn't VS automagically add using directives as you type?
It pops up a smart tag and asks you what you want to do. There may be situations where you need the full name to disambiguate. It may not know which namespace to use if the class name is used in two separate namespaces, one of which you may not be interested in.
PIEBALDconsult wrote: Such that if I type System.Diagnostics.StackFrame it could remove the System.Diagnostics. and add using System.Diagnostics at the top.
Because it doesn't know if that's what you really want. You may be planning to use another class called StackFrame elsewhere in your code but from a different namespace.
-- Always write code as if the maintenance programmer were an axe murderer who knows where you live.
Upcoming FREE developer events:
* Glasgow: Agile in the Enterprise Vs. ISVs, Mock Objects, SQL Server CLR Integration, Reporting Services, db4o ...
* Reading: SQL Bits
My website
|
|
|
|
|
Colin Angus Mackay wrote: Always write code as if the maintenance programmer were an axe murderer who knows where you live.
Indeed, so don't use the using directive.
|
|
|
|
|
PIEBALDconsult wrote: Indeed, so don't use the using directive.
You're like a record stuck in its groove.
-- Always write code as if the maintenance programmer were an axe murderer who knows where you live.
Upcoming FREE developer events:
* Glasgow: Agile in the Enterprise Vs. ISVs, Mock Objects, SQL Server CLR Integration, Reporting Services, db4o ...
* Reading: SQL Bits
My website
|
|
|
|
|
I always use using because it makes it easier to read the code and better for printing out code. I find having the fully qualified route as too much typing even though it is pretty descriptive.
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
If Microsoft didn't intend for us to use using , the templates in Visual Studio wouldn't contain a dozen of them.
I usually remove the using statements for namespaces that I don't use at all, but that's only because I don't want them there. A using statement doesn't add anything at all to the compiled code, so it doesn't hurt anything at all to have them there.
To always specify the complete namespace only makes the code harder to read. (It's also more to type, but that's not really a good argument, as the actual typing is a rather small part of the programming.)
---
single minded; short sighted; long gone;
|
|
|
|
|
sure, try removing the first line "#using System;" that will be interesting!
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
That's the first thing I do when VS creates a new file for me.
I've even made some effort to alter the templates to save myself from having to do that.
|
|
|
|
|
Hi,
I was under the impression things would not work at all without "using System;"
i.e. I thought the compiler would then be unable to map "int" to "System.Int32"
but clearly it can and does, without any "using System;"
Thanks for bringing that to my attention.
I have the habbit of removing usings I don't need, such as
System.Collections.Generic when I don't need any generic stuff.
but I use using statements all the time, and I tend to add a comment mentioning
the first Class that caused me to include the using statement.
For me the shorter form is the better one, for it improves readability.
But I can imagine situations where lots of namespaces would be required,
and then things could become hard to understand without full qualification.
The only such situation I encountered so far is about timers, having at least three
different classes called Timer in the Framework.
Greetings
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Luc Pattyn wrote: I have the habbit of removing usings I don't need
(That would be all of them.)
The using directive adds nothing to the language; it only enables the programmer to type less.
It's kinda like goto , except goto may actually be needed in some very rare cases. (Keeping in mind that C# needs goto to simulate fall-through in switch statements.)
|
|
|
|
|
The using directive is merely a crutch for inexperienced programmers (hence its use in templates and other pieces of bad code). The only time I use a using directive is to make an alias of a very long namespace (and then very rarely).
I very much dislike seeing a snippet of code (perhaps posted on here) that refers to some Widget, and I have no idea where that type of Widget came from.
Or someone answering a question by saying, "You should use a Widget." -- without saying where the blasted thing resides!
Always use a fully-qualified name and you'll never be misunderstood.
Were I in charge, the using directive would be the first against the wall.
|
|
|
|
|
PIEBALDconsult wrote: The using directive is merely a crutch for inexperienced programmers (hence its use in templates and other pieces of bad code).
That is utter crap!
PIEBALDconsult wrote: I very much dislike seeing a snippet of code (perhaps posted on here) that refers to some Widget, and I have no idea where that type of Widget came from.
Perhaps you might want to turn your gaze upon yourself. Perhaps you are the inexperienced one?
PIEBALDconsult wrote: Were I in charge, the using directive would be the first against the wall.
Just as well you are not.
-- Always write code as if the maintenance programmer were an axe murderer who knows where you live.
Upcoming FREE developer events:
* Glasgow: Agile in the Enterprise Vs. ISVs, Mock Objects, SQL Server CLR Integration, Reporting Services, db4o ...
* Reading: SQL Bits
My website
|
|
|
|
|
Good code is readable code. Full qualified names are anything but readable.
If anything fully qualified names are the crutch for inexperienced programmers who don't know where things are, code by intellisense, aren't able to hit 'find' in the object browser, or don't have the skill to notice when a conflict occurs.
Programming environments are continually evolving to provide more and more abstraction, which helps us make sense of complex situations. Why anyone would want to make their code more complex and difficult to understand is beyond me.
Regardless, your refactoring tools (even the built in ones) make dealing with your usings a total no-brainer.
|
|
|
|
|
Mark Churchill wrote: Full qualified names are anything but readable.
Not at all, I know when I read "System.Windows.Forms.Button" that it means "System.Windows.Forms.Button", and I don't have to guess that some "Button" may or may not be some other type of "Button" whose actual heritage is concealed (well, that's too strong a word actually) in a line waaay the ffff up at the top of the file.
Don't think of fully-qualified names as being for you, think of them as for they who come after you.
A case in point:
In yesterday's "Friday Programming Quiz" there is a snippet of code that refers to, among other things, mshtml.IHTMLInputElement , that's a fully-qualified name, so even though I've never heard of such a beast, I had little trouble tracking it down. Had the poster not fully-qualified the name (he didn't include a using directive, remember) I would have had a more difficult time.
|
|
|
|
|
You can (and I do) write C# programs with absolutely no using directives.
I feel it improves readability, while most others think otherwise.
Does the using directive solve an actual programming problem?
(Not just reducing keystrokes, footprint on disk, whatever)
|
|
|
|
|
private void txtBody_TextChanged(object sender, eventArgs e)<br />
{<br />
if(tbWordwrap.Pushed)<br />
{<br />
string[] tempArray = new string(txtBody.Lines.length);<br />
tempArray = txtBody.Lines;<br />
<br />
int Prevsel = txtBody.SelectionStart;<br />
string Op = "";<br />
int maxLength = cINI.dbUtils.Pref.DefaultEditorWidth;<br />
bool change= false;<br />
<br />
for(int cou = 0; cou < tempArray.Length; cou++)<br />
{<br />
if (tempArray[cou].length > maxLength)<br />
{<br />
string words = tempArray[cou].split(' ');<br />
int currentLine Length = 0;<br />
string currentLine = "";<br />
foreach(string currentWords in words)<br />
{<br />
if(currentWord.Length > 0)<br />
{ <br />
if(currentword.Length >= maxLength)<br />
{<br />
Op +=currentword.Insert(maxLength, "\r\n");<br />
break;<br />
}<br />
if(currentLineLength + currentword.Length + 1 < maxLength)<br />
{<br />
currentLine += currentword + " ";<br />
currentLineLength += currentword.Length +1;<br />
}<br />
else<br />
{<br />
Op +=currentLine.Insert(currentLineLength, "\r\n");<br />
currentLine = currentWord;<br />
currentLineLength = currentWord.Length;<br />
}<br />
}<br />
}<br />
if(currentLine !="")<br />
Op += currentLine;<br />
<br />
<br />
PrevSel++;<br />
change = true;<br />
}<br />
else<br />
{<br />
Op += tempArray[cou] + "\r\n";<br />
}<br />
if(change)<br />
{<br />
txtBody.Text = Op;<br />
txtBody.SelectionStart = prevSel;<br />
}<br />
}<br />
<br />
Op += tempArray[cou].Insert(maxLength, "\r\n" );<br />
Prevsel++;<br />
change= true;<br />
}<br />
else<br />
{<br />
Op += tempArray(cou) + "\r\n";<br />
}<br />
}<br />
<br />
if(change)<br />
{<br />
txtBody.Text = Op;<br />
txtbody.SelectionStart = Prevsel;<br />
}<br />
}<br />
}<br />
<br />
<br />
The above code is for text_change event of RichText Box in C# vs.net 2003.
Its working fine, But there are some problem which i am i m unable to understand why r coming,
1. In rare cases there occurs a problem like it automatically gives spaces between two letters, like "Him". It gives "H i m",
2. Also dont know why its cuts the word at end of line, like Accept is word,
then Acc to upper line ept to next line, This also comes rarely.
I m unableto short out the problem plz, help.
Here maxLength is variable where user can put no. of chracters he wants in a line, like 65, 68, etc.
|
|
|
|
|
humdumof wrote: = new string(txtBody.Lines.length);
This is a waste of time, you assign the variable to a different array in the next line
Your code is very messy. It should use stringbuilders instead of concatenating strings.
You should experiment with known strings until you find one that shows your bug, then step through the debugger to see what's going wrong.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Hi,
some suggestions:
1.
when showing code, put it in PRE tags so it preserves indentation; as it is,
it is very hard to read.
2.
your code does not compile; it has:
- a String.Split with lower-case
- the line int currentLine Length = 0; is invalid
- a mismatch in { and }
etc
3.
you are trying to do word wrap yourself; why don't you use LastIndexOf(' ',maxLength)
where maxLength is your linewidth limit, to find where the offending word begins?
4.
you have a lot of \r\n; I believe in RTB a single \n is what is required for
line breaks. It is my guess those \r will be turned somehow into spaces,
resulting in some of the phenomena you are experiencing.
The above list is not complete.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Hello Everyone...
I have a question about two form want to access global variable
like below, i create three classes, main class and two inner class,
but i don't why couldn't compile successful ?
If I want do this how should i to do?
<br />
namespace Innerclass<br />
{<br />
class Program<br />
{<br />
public string Share1 = "Share_Temp1";<br />
public string Share2 = "Share_Temp2";<br />
static void Main(string[] args)<br />
{<br />
object obj = new object();<br />
obj = Program.<br />
}<br />
class ClassA<br />
{<br />
public ClassA()<br />
{<br />
<br />
System.Console.WriteLine("Class A Constructor init.");<br />
System.Console.WriteLine("Class A Constructor init.--" + Share1);<br />
System.Console.WriteLine("Class A Constructor init.--" + Share2);<br />
}<br />
<br />
}<br />
class ClassB<br />
{<br />
public ClassB()<br />
{<br />
System.Console.WriteLine("Class B Constructor init.");<br />
System.Console.WriteLine("Class B Constructor init.--" + Share1);<br />
System.Console.WriteLine("Class B Constructor init.--" + Share2);<br />
}<br />
<br />
}<br />
}<br />
}<br />
By AYR
itsayr@gmail.com
|
|
|
|
|
 Hi
in the code you sent,you must declare Share1 and Share2 variables as static members to work ,or pass an instance of Program class to ClassA and ClassB (is not a good choice for program class).
the only difference for inner classes in this usage is they can access the private members too.
here is the example from yours
namespace Innerclass
{
class Program
{
public static string Share1 = "Share_Temp1";
private static string Share2 = "Share_Temp2";
private string Share3 = "Share_Temp3";
public string Share4 = "Share_Temp4";
static void Main(string[] args)
{
Program prg = new Program();
ClassA clsA = new ClassA(prg);
ClassB clsB = new ClassB(prg);
}
class ClassA
{
public ClassA(Program prg)
{
System.Console.WriteLine("Class A Constructor init.");
System.Console.WriteLine("Class A Constructor init.--" + Share1 );
System.Console.WriteLine("Class A Constructor init.--" + Share2);
System.Console.WriteLine("Class A Constructor init.--" + prg.Share3);
System.Console.WriteLine("Class A Constructor init.--" + prg.Share4);
}
}
class ClassB
{
public ClassB(Program prg)
{
System.Console.WriteLine("Class B Constructor init.");
System.Console.WriteLine("Class B Constructor init.--" + Share1);
System.Console.WriteLine("Class B Constructor init.--" + Share2);
System.Console.WriteLine("Class B Constructor init.--" + prg.Share3);
System.Console.WriteLine("Class B Constructor init.--" + prg.Share4);
}
}
}
}
good luck
|
|
|
|
|
AYR Chen wrote: object obj = new object();
This is a waste of time, you reassign obj in the next line
AYR Chen wrote: obj = Program.
This obviously won't compile. What are you trying to do ?
AYR Chen wrote: I have a question about two form want to access global variable
like below, i create three classes, main class and two inner class,
Instead, your two forms should use delegates to communicate the variable value to each other as it changes, and get rid of the global.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Please show code using PRE tags (not CODE tags);
I can't understand it without proper indentation.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Hi All ,
I have got a problem in setting tool tip in maximize, minimise and close boxes of a form which r on the top right corner of a form . I don't know ho to do it .
Please tell me, how i can do it?
Thanx.
Praveen Sharma
|
|
|
|
|
dear sir,
i want read an excel file and open it to listview but now two exception throws would u help me.....its very urgent.
exceptions:
==============
1. No overload for method 'Open' takes '13' arguments
2. Property, indexer, or event 'Value' is not supported by the language; try directly calling accessor methods 'Excel.Range.get_Value(object)' or 'Excel.Range.set_Value(object, object)'
source code:
===============
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//using System.Office.Interop.Excel;
//private Excel.Application ExcelObj = null;
namespace Project_test1
{
public partial class Form1 : Form
{
private Excel.Application ExcelObj = null;
public Form1()
{
// Initialize the Windows Components
InitializeComponent();
ExcelObj = new Excel.Application();
// See if the Excel Application Object was successfully constructed
if (ExcelObj == null)
{
MessageBox.Show("ERROR: EXCEL couldn't be started!");
System.Windows.Forms.Application.Exit();
}
// Make the Application Visible
ExcelObj.Visible = true;
}
string[] ConvertToStringArray(System.Array values)
{
// create a new string array
string[] theArray = new string[values.Length];
// loop through the 2-D System.Array and populate the 1-D String Array
for (int i = 1; i <= values.Length; i++)
{
if (values.GetValue(1, i) == null)
theArray[i - 1] = "";
else
theArray[i - 1] = (string)values.GetValue(1, i).ToString();
}
return theArray;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btn_Click(object sender, EventArgs e)
{
// prepare open file dialog to only search for excel files (had trouble setting this in design view)
this.openFileDialog1.FileName = "*.xls";
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
// Here is the call to Open a Workbook in Excel
// It uses most of the default values (except for the read-only which we set to true)
Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(openFileDialog1.FileName, 0, true, 5,
"", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true);
// get the collection of sheets in the workbook
Excel.Sheets sheets = theWorkbook.Worksheets;
// get the first and only worksheet from the collection of worksheets
Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
// loop through 10 rows of the spreadsheet and place each row in the list view
for (int i = 1; i <= 3; i++)
{
Excel.Range range = worksheet.get_Range("A" + i.ToString(), "J" + i.ToString());
System.Array myvalues = (System.Array)range.Cells.Value;
string[] strArray = ConvertToStringArray(myvalues);
listView1.Items.Add(new ListViewItem(strArray));
}
}
}
i add :
=========
Microsoft Excel 9.0 Objects Library.
|
|
|
|
|
shafikshafik wrote: No overload for method 'Open' takes '13' arguments
1 - use intellisense to find out how many arguments you should be providing, and fix your code
2 - call the get/set methods, as it is telling you to do
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Please show code using PRE tags; I can't understand it without proper indentation.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|