|
OriginalGriff wrote: Stop doing that - it's a pretty poor way to do things. If you are following a book, or other training course, then fine, but if you are just trying to pick it up "randomly" as you go, then stop right now! Your will miss too much stuff, which can save you a lot of time and heartache later!
maybe I should have said semi-informal/self-paced study…http://www.brainmeasures.com/courses/online/626/c-sharp-certification.aspx
it was a little confusing at first, but… With the help of Richard, I was able to figure it out and understand what the book was asking of me to do…
thanks for your help as well, it helped clarify what I could possibly do in the future using different keywords.
Thank you
|
|
|
|
|
You're more than welcome for any help I gave!
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
You need to use the argument array that is provided by the system, as shown below:
static void Main(string[] args)
{
int number = Convert.ToInt32(args[0]);
int power = Convert.ToInt32(args[1]);
Console.WriteLine(Math.Pow(number, power));
}
You would then call the program in a command window by something like:
programname 23 3
Use the best guess
|
|
|
|
|
Both you and the book lost me… You both are saying the same thing, but…
Here is the whole chapter, so you can see what they're talking about and maybe why I'm lost.
4-8 Using Program Arguments
One possible form of the Main method is:
static void Main(string[] args)
Using this form allows the C# program to accept as arguments a sequence
of strings. The arguments are received as string array elements named
args[0], args[1], and so forth. The string arguments can also be converted
to other data types, as explained in Chapter 3.
In the following example, the program receives two arguments at run
time. The program converts the first one to a long number and the second
to a double number, and then displays them and their product.
Example 4-11
using System;
public class ParseClass
{
static void Main(string[] args)
{
long myLong = Convert.ToInt64(args[0]);
double myDouble = Convert.ToDouble(args[1]);
double result = myLong * myDouble;
Console.WriteLine("Your long number is: {0}", myLong);
Console.WriteLine("Your double number is: {0}", myDouble);
Console.WriteLine("The result of multiplication is: {0}",
result);
}
}
Sample Run:
Assuming that the program is called “example,” enter the following at the
command line:
> example 2 1.1
The output should be:
Your long number is: 2
Your double number is: 1.1
The result of multiplication is: 2.2
Drill 4-4
Rewrite the program created for Drill 4-2 to read the number and the
power as program arguments. For example, if the program is called
“power,” you can invoke it as shown in these sample runs:
Sample Run 1:
>power 4 2
The number 4 raised to the power 2 = 16
Sample Run 2:
>power 4 3
The number 4 raised to the power 3 = 64
Here is my code, it's basically the same as yours… Just the names of the variables are changed.
using System;
namespace PracticePassingParameters
{
class Program
{
static void Main(string[] args)
{
int FN = Convert.ToInt32(args[0]);
int SN = Convert.ToInt32(args[1]);
Console.Write(Math.Pow(FN, SN));
Console.Read();
}
}
}
When I run it in the debugger, "index was outside the bounds of the array"… WTH
|
|
|
|
|
WidmarkRob wrote: When I run it in the debugger You need to set the program arguments in your properties, for the debugger to correctly run the program. In Visual Studio, select Properies in Solution Explorer, and click on Debug in the left hand selection list of the Properties page. Then put your values into the textbox labelled "Command line arguments", separated by spaces.
Use the best guess
|
|
|
|
|
|
WidmarkRob wrote: So, I don't have to turn this into an executable file before calling it from the command line? Yes you do, that's what building the project is all about.
WidmarkRob wrote: From the command line, I would have to type in "PracticingPassingParameters"?
Then I could just enter my two numbers and get the result? See my anser to your other question.
WidmarkRob wrote: all I really needed was four lines of code in the first place? No, the book is trying to teach you how to write applications, by using different features, even though some of it seems like overkill.
WidmarkRob wrote: even when I publish this simple little program and click on the executable, it still telling me I have an out of range exception You need to show us exactly what you have typed at the command line.
Use the best guess
|
|
|
|
|
Richard MacCutchan wrote: You need to show us exactly what you have typed at the command line.
my blue-collar mind is starting to get a little better, like an idiot… When I published and created a regular executable file, I went and clicked on the executable instead of calling it from the commandline…
When I called up from the commandline, it works just like it was supposed to…
|
|
|
|
|
WidmarkRob wrote: my blue-collar mind Please don't make the mistake of believing that we are all geniuses. Some (Pete O'Hanlon, Sacha Barber, OriginalGriff, and others) definitely are, but most of us are just slightly ahead of you in experience. That is to say, we already made these mistakes and learned how to fix them.
Use the best guess
|
|
|
|
|
I fingered it out, was a pain in the butt… Had to enter the full file path name all the way to the bin folder.
Okay, now that I figured that part out… How do I turn this into an executable file and have it run the same way as I did calling it in the bin folder from the command line?
|
|
|
|
|
WidmarkRob wrote: How do I turn this into an executable file It's already an executable; that's the only type that you can run (yes I know about the exceptions). You open a command window, navigate to the application's bin/Debug or bin/Release directory, and enter the executable's name followed by the relevant parameters, something like:
>cd "\My Documents\Visual Studio\Projects\mytest\bin\Debug"
>testprogram 23 5
When you debug your code, the debugger runs the executable program within a special environment that allows it to single step, and track variable values.
Use the best guess
|
|
|
|
|
I am using your drivedetector(Using Dolinay) to detect usb devices connected to PC. but I want to detect all the devices connected to my PC so that I can transfer data to my form in C#. what Can I do?
C# user
|
|
|
|
|
If you've got questions about a particular article, do to the forum at the bottom of that article.
This site is run by an entire community, volunteering their time, not a couple of people writing thousands of articles. The best person to ask about an article is the person who wrote it.
|
|
|
|
|
I am looking for a C# wrapper to make calls with Skype. I found 3 'Skype' related articles here on CP, and spent an hour Googling, but I can' seem to find a decent example of plaching a call with Skype.
My app will have the skype Id. When the icon is clicked, I want it to call using skype. How hard can this be??
Anyone have an example of this?
If it's not broken, fix it until it is
|
|
|
|
|
|
Have you looked at it?
If it's not broken, fix it until it is
|
|
|
|
|
Briefly, they have an ActiveX control that you can use to issue commands to the Skype system, or if you find one of the wrappers you may be able to roll your own. Tracer looks like it can help you understand the API calls and what is supported, so combining the two and a lot of trial and error it looks like you can interact with Skype via C#.
|
|
|
|
|
Yss I looked at these earlier, but I really didn't want to reinvent something if someone else already has done it.
Ya, Tracer looks promising. Guiess I'll start there.
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
I have the following interfaces, they must inherit in that order, the problem is that i can't make IVideoPlayer to return IVideo as Media. Is this not possible or am I missing something? I'd prefer not to have IAudioPlayer & IVideoPlayer as generic instances - but even if I am - is it possible at all?
I can only see that it would work if IVideoPlayer allso inherited IMediaPlayer<ivideo>, but I cant do that since IAudioPlayer already inherites it? My head hurts.
public interface IMedia { }
public interface IAudio : IMedia { }
public interface IVideo : IAudio { }
public interface IMediaPlayer<T> where T : IMedia
{
T Media { get; }
}
public interface IAudioPlayer : IMediaPlayer<IAudio>
{
}
public interface IVideoPlayer : IAudioPlayer
{
}
|
|
|
|
|
What if you did something like...
public interface IVideoPlayer : IMediaPlayer<IVideo>, IAudio
{
}
Maybe there are some implementation details that IVideoPlayer needs from IAudioPlayer, I'm not sure.
|
|
|
|
|
I guess you ment IAudioPlayer?
IVideoPlayer would then have both Media as IAudio annd IVideo.
Ambiguity between 'IMediaPlayer<IVideo>.Media' and 'IMediaPlayer<IAudio>.Media'
|
|
|
|
|
Got it working. Without generic mess.
Thougt I tryed everything.
public interface _IMedia { }
public interface _IAudio : _IMedia { }
public interface _IVideo : _IAudio { }
public class CMedia : _IVideo
{
}
public interface _IMediaPlayer
{
_IMedia Media { get; }
}
public interface _IAudioPlayer : _IMediaPlayer
{
new _IAudio Media { get; }
}
public interface _IVideoPlayer : _IAudioPlayer
{
new _IVideo Media { get; }
}
public class _CMediaPlayer : _IMediaPlayer
{
private CMedia m_Media = new CMedia();
public _IMedia Media { get { return m_Media; } }
}
public class _CAudioPlayer : _CMediaPlayer, _IAudioPlayer
{
public new _IAudio Media { get { return base.Media as _IAudio; } }
}
public class CVideoPlayer : _CAudioPlayer, _IVideoPlayer
{
public new _IVideo Media { get { return base.Media as _IVideo; } }
}
|
|
|
|
|
Hm, what about the Liskov principle (see e.g. http://en.wikipedia.org/wiki/Liskov_substitution_principle[^])? Your example somehow reminds me of the square-and-rectangle issue there...
In case that the VideoPlayer can use some code of the AudioPlayer, I'd go for composition instead of inheritance.
|
|
|
|
|
Thanks. It's a good idea, but in my case it was more about hiding properties that isnt 'needed'. Player & Media is allways initzialed as video type.
|
|
|
|
|
Hi,
You could do it with generics in the following way:
public interface IMedia
{
}
public interface IAudio : IMedia
{
}
public interface IVideo : IAudio
{
}
public interface IMediaPlayer<T> where T : IMedia
{
T Media
{
get;
}
}
public interface IAudioPlayer<T> : IMediaPlayer<T> where T : IAudio
{
}
public interface IVideoPlayer<T> : IAudioPlayer<T> where T : IVideo
{
}
public class Class1
{
public void Example()
{
IVideoPlayer<IVideo> videoPlayer = null;
IAudioPlayer<IAudio> audioPlayer = null;
}
}
The problem is that your video player can't be compiled against IVideo and use an audio player compiled against IAudio. Your video player and audio player need to be compiled against the same type.
I don't know what this issue with generics is called, but I always find that generics bread more generics.
Kind Regards,
Keld Ølykke
|
|
|
|