|
Why didn't they include multiple inheritence: Big question. Best I can do is point you to an in-depth post by one of the CLR guys who explains in detail why they didn't: http://blogs.msdn.com/csharpfaq/archive/2004/03/07/85562.aspx[^]
Why didn't they include typedef's: well, technically they did, they just limited the scope of typedefs to the file level, which is a good thing IMO.
For example, you can write:
using IntList = System.Collections.Generic.List<int>;
...
public void Foo()
{
IntList list = new IntList();
list.Add(5);
...
}
If you don't like the fact that it's limited to the file level, then I'd just write a value type (e.g. a struct) that holds whatever data you need.
|
|
|
|
|
Skippums wrote: the removal of the ever useful typedef
"When Anders closes a door he usually opens a window someplace."
No one showed you the C# way? Won't this work for you?
namespace Test
{
class Test
{
private static MyType
F
(
MyType x
)
{
return ( x + x ) ;
}
[System.STAThreadAttribute]
static void
Main
(
string[] args
)
{
try
{
System.Console.WriteLine ( F ( 2 ) ) ;
}
catch ( System.Exception err )
{
System.Console.WriteLine ( err.Message ) ;
}
return ;
}
}
}
Of course C/C++ header files make using typedefs easier, whereas each C# file would need to have the using directive. On the other hand, you can pass C# code through the C-preprocessor if you want.
|
|
|
|
|
"you can pass C# code through the C-preprocessor if you want"
That is an interesting idea... I will have to try that one at home sometime. As far as the using statement, that works alright, but I will still have to modify the value in a number of places after the fact. What I really wanted was either namespace-scoped typedefs or project-scoped typedefs (solution-scoped would probably be nice in certain situations, but I could get by with just the other two mentioned above). Anyway, thanks for your input!
Jeff
|
|
|
|
|
Skippums wrote: I will still have to modify the value in a number of places after the fact
Wouldn't that be the case anyway?
Skippums wrote: thanks for your input!
Glad to be of service.
|
|
|
|
|
Ok, you got me. I should have said, "I will still have to modify the TYPE in a number of places after the fact".
Jeff
|
|
|
|
|
Never hate any languages,pls.
Cheers
Bino
www.codepal.co.in
|
|
|
|
|
Thanks to everyone who responded to my post! I got a lot of good opinions and dialogs going, and even some reasons why particular language design choices were made (although I still disagree with them). Perhaps the title of the post should have been, "Some of the features I miss from C++", as I still plan on using C# for my projects that I do in my free time, even when someone doesn't force me to. There are many great features of C# to take advantage of, and despite the few drawbacks (in my opinion, pretty significant drawbacks), it really is a great language. Thanks again everyone!
Jeff
|
|
|
|
|
hi,
How can I get all names of files which have extension of mp3 from filesystem (c:\ part of computer for example). I can get only the names of files with extension of mp3 in only one folder by using beloved code. By this code,I can't get, if there are mp3 files in subfolders, but I want to get all mp3 files' names under c:\ part of computer.
private void button1_Click(object sender, EventArgs e)
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\");
System.IO.FileSystemInfo[] fi = di.GetFileSystemInfos();
foreach (System.IO.FileSystemInfo f in fi)
{
if (f.Extension==".mp3")
{
richTextBox1.Text += f.Name;
richTextBox1.Text += Environment.NewLine;
}
}
richTextBox1.SaveFile(@"C:\mp3ler.xls",RichTextBoxStreamType.PlainText);
}
|
|
|
|
|
Recursion:
void GetMP3(string Path)
{
...
foreach(FileSystemInfo f in fi) {
if(f.Extension == ".mp3") {
richTextBox1.Text += f.Name;
richTextBox1.Text += Environment.NewLine;
}
}
foreach(DirectoryInfo d in di) {
GetMP3(d.FullName);
}
...
}
The function will keep calling itself for all subdirectories within a folder, quite clever when you think about it.
That code might not work, but you get the idea.
You may also need to call Application.DoEvents() somewhere too, so that your window doesn't lock up while you trawl through the compy looking for mp3's
My current favourite word is: PIE!
I have changed my name to my regular internet alias. But don't let the 'Genius' part fool you, you don't know what 'SK' stands for.
-The Undefeated
|
|
|
|
|
Actually, Application.DoEvents isn't really the best way to handle that. You're absolutely right that the application should yield to the UI thread so the application remains responsive, but Application.DoEvents forces the application message pump to run and isn't recommended.
Ideally the code above should be performed on a separate thread from the main UI thread. There are many ways to do that including creating a new thread, using the ThreadPool or asynchronous execution of a delegate (delegate.BeginInvoke / EndInvoke). But one of the coolest ways to execute code off the UI thread is the Dispatcher class. You can use the Dispatcher to execute code with various priority levels and even postpone execution until the application is idle. Note that Dispatcher is new to .Net 3.0, so if you're tied to 1.x or 2.0 then you won't be able to use it. You can read about the Dispatcher here[^].
Unfortunately, once this code is running on a separate thread you still have to get back to the UI thread to update controls. If you have a listbox, for example, and you want to add a ListboxItem for each file you find you won't be able to do it from the worker thread. You can get around that using Control.Invoke, but there's an even easier way if you're in .Net 3.0 or later. That's the BackgroundWorker[^] component, which will help you in easily setting up the background thread to execute your code and even gives you a very easy way to communicate back to the UI thread. BackgroundWorker is certainly the easiest, but if it's too restrictive for what you're trying to do make sure you check out some of those other options.
Regards,
Jared Bienz
My posts may include factual data, educated guesses, personal opinion and dry humor. They should not be treated as an official Microsoft statement. Sites of Interest: MSDN Events | US ISV Team Blog
|
|
|
|
|
Be sure to run the code from the prior post in a separate thread, or Windows will assume your program is "hung" for long directory searches (such as the entire C drive), and when you do a ctl-alt-del your program will appear in the list as not responding (if not in a separate thread).
Jeff
|
|
|
|
|
Thats why i added in to use Application.DoEvents() . It gives control to the main window to do what it needs to, process messages, redraw, etc.
Though i agree that you should probably put it in a separate thread, although that will make it a little more difficult to update your textbox, though it shouldn't be too much trouble.
My current favourite word is: PIE!
I have changed my name to my regular internet alias. But don't let the 'Genius' part fool you, you don't know what 'SK' stands for.
-The Undefeated
|
|
|
|
|
hi,
Directory.GetFiles(string path, string searchPattern, SearchOption searchOption)
searches recursively if you ask for it.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
The overload of Directory.GetFiles that takes a SearchOption is only available in .Net 3.0 or later, though I agree it's the best option.
Note that this can still take a long time and should still potentially be run on a separate thread.
My posts may include factual data, educated guesses, personal opinion and dry humor. They should not be treated as an official Microsoft statement. Sites of Interest: MSDN Events | US ISV Team Blog
|
|
|
|
|
Jared Bienz [MSFT] wrote: The overload of Directory.GetFiles that takes a SearchOption is only available in .Net 3.0 or later
I have it running on 2.0, it runs fine except it fails searching at the device level
(such as path="C:\") due to an access denied on "C:\System Volume Information\".
Jared Bienz [MSFT] wrote: should still potentially be run on a separate thread
I fully agree; I would suggest to do allmost all multi-file operations on a separate thread,
and in a try-catch construct.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Thanks for your replies,
Is there an easier way for this?
|
|
|
|
|
Hello, I am trying to create a program that reads in 3 numbers and averages them for my Programming 1 class. I am trying to use method call statements in order to do this. I got it all finished but when I built it I got three "Identifier Expected" errors on line 26, and I don't understand what that means. (I'm rather new) Here is what I've got for coding...I'm not sure if there is anything else I need to change about it other than the 3 errors, either.
using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
<br />
namespace Program10<br />
{<br />
class Program<br />
{<br />
static void GetNums(out double num1, out double num2, out double num3)<br />
{<br />
Console.Write("Enter first number: ");<br />
num1 = double.Parse(Console.ReadLine());<br />
Console.Write("Enter second number: ");<br />
num2 = double.Parse(Console.ReadLine());<br />
Console.Write("Enter third number: ");<br />
num3 = double.Parse(Console.ReadLine());<br />
}<br />
static void CalcAvg(out double num1, out double num2, out double num3)<br />
{<br />
double avgNum;<br />
avgNum = num1 + num2 + num3;<br />
avgNum = avgNum / 3;<br />
<br />
return (double) avgNum;<br />
}<br />
static double DspAvg(double avgNum, out num1, out num2, out num3)<br />
{<br />
Console.WriteLine("The average of {0:n}, {1:n}, and {2:n} is {3}", num1, num2, num3, avgNum);<br />
}<br />
<br />
static void Main()<br />
{<br />
double firstNum, secNum, thirdNum, avgNum;<br />
DspAvg(firstNum, secNum, thirdNum);<br />
GetNums(out firstNum, out secNum, out thirdNum);<br />
CalcAvg(avgNum);<br />
}<br />
<br />
}
Thanks in advance for your help!
[Ross Murtle]
[Computer Science Major - Iowa Central Community College/Iowa State University]
|
|
|
|
|
and which of these is line 26?
don't tell us you are running Visual Studio with line numbering disabled!
and please use < PRE > tags for code snippets.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
static double DspAvg(double avgNum, out num1, out num2, out num3)
is line 26. Sorry for the confusion.
|
|
|
|
|
num1/2/3 are missing their types, something like int
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
First off, Welcome to Programming C#! It's my favorite language.
There are a handfull of issues, but at least you gave it the good old college try.
Let's get started: You were missing a close curly brace at the end, though that could have simply not made it to your clipboard.
Next, (and this was the 'Identifier Expected' issue) in DspAvg you didn't define the data type for num1 num2 or num3. You must specify that they're double.
After that, in DspAvg you also can't mark those parameters as 'out' because DspAvg never assigns them a value -- it just prints them on the screen. The 'out' keyword signifies that a value will be assigned to the parameter before the method returns.
The same thing happened in CalcAvg. No need to mark them as 'out' since they're not being assigned in the method.
In CalcAvg you explicitly cast avgNum to a double, which isn't necessary since avgNum is defined as a double at the beginning of the method. It'll still compile, but it's unecessary.
Now, here it get's a little interesting. DspAgv is marked to return a double, but no value is returned (and one shouldn't be because you're just displaying the results). However, your CalcAvg method doesn't return a value and of course it really should.
In Main it looks like you were trying to use DspAvg to show the collected values, but DspAvg requires a fourth parameter (the calculated average). You could either remove that parameter or not use DspAvg until you've done the calculation.
In Main, when you call CalcAvg you'll need to store the return value.
You should probably have a Console.ReadLine at the end or the window will close before you see the results (if debugging in Visual Studio).
Here are the changes, hope it works for you. Now please don't expect another free homework assignment, but DO enjoy learning C#.
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
<br />
namespace Program10<br />
{<br />
class Program<br />
{<br />
static void GetNums(out double num1, out double num2, out double num3)<br />
{<br />
Console.Write("Enter first number: ");<br />
num1 = double.Parse(Console.ReadLine());<br />
Console.Write("Enter second number: ");<br />
num2 = double.Parse(Console.ReadLine());<br />
Console.Write("Enter third number: ");<br />
num3 = double.Parse(Console.ReadLine());<br />
}<br />
<br />
static double CalcAvg(double num1, double num2, double num3)<br />
{<br />
double avgNum;<br />
avgNum = num1 + num2 + num3;<br />
avgNum = avgNum / 3;<br />
<br />
return avgNum;<br />
}<br />
<br />
static void DspAvg(double avgNum, double num1, double num2, double num3)<br />
{<br />
Console.WriteLine("The average of {0:n}, {1:n}, and {2:n} is {3}", num1, num2, num3, avgNum);<br />
}<br />
<br />
static void Main()<br />
{<br />
double firstNum, secNum, thirdNum;<br />
GetNums(out firstNum, out secNum, out thirdNum);<br />
double avgNum = CalcAvg(firstNum, secNum, thirdNum);<br />
DspAvg(avgNum, firstNum, secNum, thirdNum);<br />
Console.WriteLine("Press Enter to end the application.");<br />
Console.ReadLine();<br />
}<br />
<br />
}<br />
}<br />
My posts may include factual data, educated guesses, personal opinion and dry humor. They should not be treated as an official Microsoft statement.
|
|
|
|
|
Thank you SO MUCH for your help. I have learned from my mistakes and the fact that you took time out of your day to help a measly little college freshman like myself is greatly appreciated!
Thanks again!
|
|
|
|
|
No problem at all. The secret to becoming a successful developer is being resourceful and not giving up. Glad I could help.
My posts may include factual data, educated guesses, personal opinion and dry humor. They should not be treated as an official Microsoft statement. Sites of Interest: MSDN Events | US ISV Team Blog
|
|
|
|
|
Jared Bienz [MSFT] wrote: The secret to becoming a successful developer is being resourceful and not giving up
Its not. The secret is caffeine and pizza. Plenty of it.
|
|
|
|
|
Caffeine and Pizza inherit from Resource, don't they?
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|