|
Hey guys,
I have this weird problem, when I want to open the FolderBrowserDialog, I can't select any folder on my phone! When I want to select a folder on my local HDD, there's no problem, but when it comes to the phone the select button get's deactivated and I can't select a folder. (see attached picture)
Here's the code I'm using:
private void button3_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
dialog.Description = "Open Folder";
dialog.ShowNewFolderButton = true;
dialog.RootFolder = Environment.SpecialFolder.MyComputer;
if (dialog.ShowDialog() == DialogResult.OK)
{
textBox2.Text = dialog.SelectedPath;
}
}
}
What I want to do is copying my music to the phone, but therefore I need a path.
When I open the phone with Microsoft Explorer, the path is shown such as "Computer\GT-I9300\Card\Bluetooth" (that's the folder "Bluetooth" on the root of the external card of the phone). So there's no explicit path either! Is there any other way to get the path? Programs such as WinAmp or WMP can connect to the phone to copy files without any problems, so it must be possible somehow.
|
|
|
|
|
|
Instead of trying to type this up, which would take forever… I recorded my screen and uploaded it to YouTube.
Here is the link to the video:
[^]
using System;
namespace CalculatetothePowerof
{
class Program
{
static void Main()
{
AcceptValueOne();
}
static int AcceptValueOne()
{
Console.WriteLine("Enter your first number:");
string valueOne = Console.ReadLine();
int IntOne = 0;
bool result = Int32.TryParse(valueOne, out IntOne);
if (!result)
{
Console.WriteLine("Attempted conversion of '{0}' failed.", IntOne);
AcceptValueOne();
}
Console.WriteLine("\n{0}", IntOne + ", is what you entered?\n\nPress lowercase y for yes\nPress lowercase n for no.");
string yesNo = Console.ReadLine();
if (yesNo == "y")
{
AcceptValueTwo();
}
if (yesNo == "n")
{
Console.Clear();
AcceptValueOne();
}
else if (yesNo != null)
{
Console.Clear();
AcceptValueOne();
}
return IntOne;
}
static int AcceptValueTwo()
{
Console.WriteLine("\nEnter your second number:");
string valueTwo = Console.ReadLine();
int IntTwo = 0;
bool result = Int32.TryParse(valueTwo, out IntTwo);
if (!result)
{
Console.WriteLine("Attempted conversion of '{0}' failed.", IntTwo);
AcceptValueTwo();
}
Console.WriteLine("\n{0}", IntTwo + ", is what you entered?\n\nPress lowercase y for yes\nPress lowercase n for no.");
string yesNo = Console.ReadLine();
if (yesNo == "y")
{
CalcPowerOf();
}
if (yesNo == "n")
{
Console.Clear();
AcceptValueTwo();
}
if (yesNo != null)
{
Console.Clear();
AcceptValueTwo();
}
return IntTwo;
}
int valOne = AcceptValueOne();
int valTwo = AcceptValueTwo();
static void CalcPowerOf()
{
Program MyObj = new Program();
Console.WriteLine(Math.Pow(MyObj.valOne, MyObj.valTwo));
Console.Read();
}
}
}
When I changed this:
int valOne = AcceptValueOne();
int valTwo = AcceptValueTwo();
Into this:
int valOne = IntOne;
int valTwo = IntTwo;
Blue squiggly lines appear underneath both:
IntOne;
IntTwo;
Does not exist in the current context.
If you make any changes to my code, please explain what you're doing and why.
Thank you to all who took the time.
Rob
|
|
|
|
|
You variables IntOne and IntTwo exist in the scope of the methods AcceptValueOne and AcceptValueTwo respectively. They do not exist in the scope on the valOne and valTwo members.
Please take a look at scopes in C#[^]. Another article I found on google is this[^].
I hope this helps.
"It was when I found out I could make mistakes that I knew I was on to something."
-Ornette Coleman
"Philosophy is a study that lets us be unhappy more intelligently."
-Anon.
|
|
|
|
|
First of all, I don't think anyone is going to watch a YouTube video to figure out what you are doing.
However, you seem to have a fundamental misunderstanding of how values are saved from called methods. Your main method makes a call to AcceptValueOne but does not store the returned value. You also have the following statements:
int valOne = AcceptValueOne();
int valTwo = AcceptValueTwo();
but not inside any method, so they will not be executed; the compiler should have told you that. You should delete your CalcPowerOf method, and change your main method to something like:
static void Main()
{
int valOne = AcceptValueOne();
int valTwo = AcceptValueTwo();
Console.WriteLine(Math.Pow(valOne, valTwo));
Console.Read();
}
You also need to remove your calls to CalcPowerOf as they serve no purpose.
Use the best guess
|
|
|
|
|
Okay, here is what my code looks like now.
I got rid of the third method and moved all of that up into my main method like you suggested.
I put a breakpoint on my main method so I could watch and see what happens.
The compiler didn't complain, as I was able to step through…
Once I hit the integer variable valOne = AcceptValueOne();
It loops back down to my method AcceptValueOne(); and starts all over again.
Yes, I do have a fundamental misunderstanding that I am trying to learn.
I haven't quite figured it out yet.
I know my code is ugly, you old pros are probably wincing and saying something like "Ah, newbie! Your code stinks and you need to change your shorts!"…
Trying to learn this stuff on my own without any formal classes. Just some e-books and YouTube videos and forums similar to this one.
I know there's probably some shortcut techniques that could be using, like… Maybe a few string arrays and reusing what's in my first method… For right now, let's just leave my code the way it is as much as possible… Because, I wrote it… I understand it… It's what I've learned so far… I'm not just some script kiddie trying to get an easy way out.
I'm doing my best to learn this stuff and understand how and what things do…
You're going to be seeing a lot of me, when I learned something and understand it and how to write it… You'll see it reflected in my code.
For now, these basic things in this post… Is what I know and some of it I'm trying to understand yet.
If you make any changes to my code, please try not to change too much of it… That way I can study what you changed and try to learn from it…
Thank you for replying and trying to help… I appreciate it.
Here is what my code looks like now:
using System;
namespace CalculatetothePowerof
{
class Program
{
static void Main()
{
AcceptValueOne();
AcceptValueTwo();
int valOne = IntOne;
int valTwo = IntTwo;
Console.WriteLine(Math.Pow(valOne, valTwo));
Console.Read();
}
static int AcceptValueOne()
{
Console.WriteLine("Enter your first number:");
string valueOne = Console.ReadLine();
int IntOne = 0;
bool result = Int32.TryParse(valueOne, out IntOne);
if (!result)
{
Console.WriteLine("Attempted conversion of '{0}' failed.", IntOne);
AcceptValueOne();
}
Console.WriteLine("\n{0}", IntOne + ", is what you entered?\n\nPress lowercase y for yes\nPress lowercase n for no.");
string yesNo = Console.ReadLine();
if (yesNo == "y")
{
return IntOne;
}
if (yesNo == "n")
{
Console.Clear();
AcceptValueOne();
}
else if (yesNo != null)
{
Console.Clear();
AcceptValueOne();
}
return IntOne;
}
static int AcceptValueTwo()
{
Console.WriteLine("\nEnter your second number:");
string valueTwo = Console.ReadLine();
int IntTwo = 0;
bool result = Int32.TryParse(valueTwo, out IntTwo);
if (!result)
{
Console.WriteLine("Attempted conversion of '{0}' failed.", IntTwo);
AcceptValueTwo();
}
Console.WriteLine("\n{0}", IntTwo + ", is what you entered?\n\nPress lowercase y for yes\nPress lowercase n for no.");
string yesNo = Console.ReadLine();
if (yesNo == "y")
{
return IntTwo;
}
if (yesNo == "n")
{
Console.Clear();
AcceptValueTwo();
}
if (yesNo != null)
{
Console.Clear();
AcceptValueTwo();
}
return IntTwo;
}
}
}
|
|
|
|
|
WidmarkRob wrote: you old pros are probably wincing Not at all, we all had to learn, and we're just trying to help you do the same. I would strongly recommend you take a look at .NET Book Zero[^], by Charles Petzold, one of the best writers of books on Windows programming.
As to your code, you are getting there but you still have an issue with the scope of your variables, and getting method return values. Your main method should be:
static void Main()
{
int valOne = AcceptValueOne();
int valTwo = AcceptValueTwo();
Console.WriteLine(Math.Pow(valOne, valTwo));
Console.Read();
}
... when a method returns a value then you need to capture that value into a local variable on the call, as the local variables in the called method have gone out of scope and no longer exist. Also, within your AcceptValueOne and AcceptValueTwo methods, you are making recursive calls (calling a method from within itself), which could lead you to disappear up your own fundament. You should rewrite these methods to use loops which break out when correct values have been received, something like:
IntValue = -1;
do
{
display the message
read the response string
try and convert the string to an integer storing in IntValue
if the IntValue is not valid then set IntValue = -1
} while (IntValue == -1);
return IntValue;
Use the best guess
|
|
|
|
|
Okay, Some guy at MSDN was getting a little frustrated… So, he wrote out the program… Probably the way you guys would have done also… It looks very clean and it's only about 35 lines of code…
Anyway, I was looking at his code and studying it a little bit… I thought I would try to apply some of it to what I have written so I could understand it better…
It does look a little better…
Now, where I'm getting stuck is at:
Program.AcceptValueTwo(message2);
I also tried it without the "Program."
I get blue squiggly lines underneath (message2)
the name does not exist in the current context
Let's try to focus on this part before I tackle implementing the do while loop.
I'll still have to go back to my book and probably watch a few more YouTube videos to get the basics of the do while loop.
using System;
namespace CalculatetothePowerof
{
class Program
{
static void Main()
{
int valOne = AcceptValueOne("Enter your first number:");
int valTwo = AcceptValueTwo("\nEnter your second number:");
Console.WriteLine(Math.Pow(valOne, valTwo));
Console.Read();
}
static int AcceptValueOne(string message)
{
Console.WriteLine(message);
string valueOne = Console.ReadLine();
int IntOne = 0;
bool result = Int32.TryParse(valueOne, out IntOne);
if (!result)
{
Console.WriteLine("Attempted conversion of '{0}' failed.", IntOne);
AcceptValueOne(message);
}
Console.WriteLine("\n{0}", IntOne + ", is what you entered?\n\nPress lowercase y for yes\nPress lowercase n for no.");
string yesNo = Console.ReadLine();
if (yesNo == "y")
{
Program.AcceptValueTwo(message2);
}
if (yesNo != "y")
{
Console.Clear();
AcceptValueOne(message);
}
return IntOne;
}
static int AcceptValueTwo(string message2)
{
Console.WriteLine(message2);
string valueTwo = Console.ReadLine();
int IntTwo = 0;
bool result = Int32.TryParse(valueTwo, out IntTwo);
if (!result)
{
Console.WriteLine("Attempted conversion of '{0}' failed.", IntTwo);
AcceptValueTwo(message2);
}
Console.WriteLine("\n{0}", IntTwo + ", is what you entered?\n\nPress lowercase y for yes\nPress lowercase n for no.");
string yesNo = Console.ReadLine();
if (yesNo == "y")
{
return IntTwo;
}
if (yesNo != "y")
{
Console.Clear();
AcceptValueTwo(message2);
}
return IntTwo;
}
}
}
|
|
|
|
|
You are still making those recursive calls to your AcceptValuexxx methods within the methods themselves. This is wrong, and will lead to either incorrect results or program crashes. You also do not need two methods, which do exactly the same thing. Try without the check for a valid number first, and ensure you enter a valid number in your tests, thus your method would be something like:
Display the message
Read the input string
Convert the string to the integer value
Return the integer value
and in main() you would have:
int valueOne = AcceptValue("gimme value one";
int valueTwo = AcceptValue("gimme value two";
That will get your basic code working, you can then add the extra checks later.
The link I gave you to Charles Petzold's book contains samples that show this in much greater detail than I have given here. I would suggest you work through that book, which will give you a good grounding in how the pieces all fit together.
Use the best guess
|
|
|
|
|
Tada!
Took me long enough!
using System;
namespace CalculatetothePowerof
{
class Program
{
static void Main()
{
double valOne = AcceptValue("Enter your first number:");
double valTwo = AcceptValue("\nEnter your second number:");
Console.WriteLine(Math.Pow(valOne, valTwo));
Console.Read();
}
static int AcceptValue(string message)
{
Console.WriteLine(message);
string valueOne = Console.ReadLine();
int IntOne = Convert.ToInt32(valueOne);
return IntOne;
}
}
}
|
|
|
|
|
Nice and tidy.
You also need to understand that AcceptValue is returning a variable of type int (an integer), which you are storing in a variable of type double , which is floating point. In this case it's not a big problem, the compiler will merely convert it to double before storing it. However, going the other way from double to int can lead to strange results. for the moment don't think about it.
Use the best guess
|
|
|
|
|
Yeah, I was just playing around with the different data types to see what kind of output I would get…
I guess I forgot to change it back to an int datatype before posted…
I tried playing around with the do while loop… I'm going to have to study that much more before I try to implement it in anything.
However, my if statements are acting a little funny…
I couldn't get the regular do while loop to work quite right either, I'll try to play around with a little bit later…
When I answered yes to both numbers, it does fine and does the calculation…
I entered the first number and then answer no, it starts over like it's supposed to…
Then I go to enter the first number again, this time I answered yes…
it moves like it's supposed to…
I go to enter the second number and answer no, it starts the second number over like it's supposed to…
then I answered the second number again, this time answering yes… For some reason it looks like the first digit is cut off
For both numbers I used the number 5, in this particular scenario… When it's working, the answer should be 3125…
only 125 shows up when I answer no for the second question, the first time…
enter number 5
y
enter number 2
n
enter second number again 5
calculates and shows 125
using System;
namespace CalculatetothePowerof
{
class Program
{
static void Main()
{
int valOne = AcceptValue("Enter your first number:");
int valTwo = AcceptValue("\nEnter your second number:");
Console.WriteLine(Math.Pow(valOne, valTwo));
Console.Read();
}
static int AcceptValue(string message)
{
Console.WriteLine(message);
string valueOne = Console.ReadLine();
int IntOne = Convert.ToInt32(valueOne);
Console.WriteLine("Are you sure you want to use that number?\nPress lowercase y or n, to continue.");
string YesNo = Console.ReadLine();
if (YesNo == "y")
return IntOne;
else
{
Console.Clear();
AcceptValue(message);
}
return IntOne;
}
}
}
|
|
|
|
|
Aaarrgh!!!
Do you sense my frustration? You must not call AcceptValue from within itself - it does not do what you think it's doing. And even if it was, you are not storing its returned value. The only way to repeat the sequence until you get a valid number is by using a loop as I showed you before.
Use the best guess
|
|
|
|
|
I need a beer now...
using System;
namespace CalculatetothePowerof
{
class Program
{
static void Main()
{
int valOne = AcceptValue("Enter your first number:");
int valTwo = AcceptValue("\nEnter your second number:");
Console.WriteLine(Math.Pow(valOne, valTwo));
Console.Read();
}
static int AcceptValue(string message)
{
string valueOne;
int IntOne;
string YesNo;
do
{
Console.WriteLine(message);
valueOne = Console.ReadLine();
IntOne = Convert.ToInt32(valueOne);
Console.WriteLine("Are you sure you want to use that number?\nPress lowercase y or n, to continue.");
YesNo = Console.ReadLine();
if (YesNo == "y")
{
break;
}
Console.Clear();
} while (YesNo != "y");
return IntOne;
}
}
}
|
|
|
|
|
Excellent!
Use the best guess
|
|
|
|
|
I had to step away from my laptop for a day… That literally made me mentally tired.
Before I became a quadriplegic in '98, I was a blue-collar monkey with calluses all over my hands and feet. I've always been using my gray matter, even in my blue-collar days. Always started out at the bottom of the totem pole, eventually working my way up to some sort of management position.
Learning this programming language, kind of brings me back to my elementary days in school.
Sir Richard, I greatly appreciate the time you took to help me figure some of this out.
By the way, I did eventually open the book suggested and started reading some. The book I bought for $85 , I'm going to continue and finish that book first before I pick your book up again (the book you suggested).
They both start out fairly similar in trying to explain about the .Net framework and trying to give you a little history about the programming language C#.
$85 is why I'm going to continue, read, finish the other book first. Both books are about 350 pages, I'm already 100 pages into the book I bought. I only got about 20 pages into the book you suggested. That's when I realized that I should stop reading that book and continue with the other one, they both started off fairly similar and they're both about the same length.
I will definitely take your book up again, there were a few things in there that my book did not mention that. So, it is a good resource to have.
Again, thank you for your patience and help Sir Richard.
|
|
|
|
|
Happy to help, and good luck with your reading. Remember, this forum is always open, and there are lots of people offering assistance.
Use the best guess
|
|
|
|
|
Rob, I see several problems in your code here. Let's break down some of the more apparent errors (I'm ignoring style issues such as using Pascal casing for variables):
string valueOne = Console.ReadLine();
int IntOne = 0;
bool result = Int32.TryParse(valueOne, out IntOne);
if (!result)
{
Console.WriteLine("Attempted conversion of '{0}' failed.", IntOne);
AcceptValueOne();
} In this section of code, you call AcceptValueOne again, and you aren't returning anything from it. The issue you have here is that you are recursively calling this method is completely unecessary, and also will return 0 to the calling function if the condition fails the first time (also, you are using the failed conversion in your WriteLine). This could easily be solved with a loop instead:
bool result = false;
do
{
string valueOne = Console.ReadLine();
result = Int32.TryParse(valueOne, out IntOne);
if (!result)
{
Console.WriteLine("Attempted conversion of '{0}' failed, please try again.", valueOne);
}
} while (!result); As you can see, the test is straightforward and there's no recursion. Well, as you can see, the same logic applies in cases where you are doing your tests for the y/n combination.
Another problem with your code is that you have two paths to AcceptValueTwo - the one in AcceptValueOne is completely useless because you aren't doing anything with the value. You're actually getting this value elsewhere (by the way, your code won't compile at the moment because you have a class level variable that's calling a method - and this is something you can't do; also, your valOne and valTwo members aren't static which means they won't be visible in any of your static methods).
|
|
|
|
|
I appreciate you replying and trying to help… Making the call back to the first method if the user enters "n" or anything else, does turn the variable `zero…
Which is what I wanted…
I realize and understand there is probably a better way to do it, but… So you understand where I'm coming from and what I'm trying to accomplish and do… Please read my reply that I made to Richard MacCutchan
|
|
|
|
|
Rob, the reason I have shown you that you need corrections is because the underlying logic in your code is flawed. No amount of fiddling is going to get around the fact that you have paths through your code that return you unexpected values. If you try and simplify your code the way I have shown you, you should find it easier.
|
|
|
|
|
I'm learning this stuff at my pace… Not your pace.
Feel free not to comment on any more of my posts…
|
|
|
|
|
Rob, as I've tried to offer you advice which is intended to help you, I find that attitude highly offensive. Your code has logic problems in it, and there are easy fixes, which I attempted to point out. If you are this hostile to people who try to help you improve, then I would suggest that you need to take a long hard look at whether or not you really want to get better at this.
|
|
|
|
|
Hello All,
My windows form application which allows users to import, edit, and save images works for the most part. However, it lacks many features that I would like to have but I have no idea on how to go about creating them. On top of the list of features I'd like to have is the ability to create images on separate layers. Right now I can put paint on top of an image or lay one image on top of another image but once I do that I can not separate them since they are on the same layer. Any suggestion will be greatly appreciated thanks in advance.
|
|
|
|
|
You would need to store each layer as a separate entity. When your persisted "image file" is read by your app, it should render the layers in order. In practice, every layer object will have its own properties (e.g. opacity, visibility, etc.)
/ravi
|
|
|
|
|
You should move to WPF. WPF already implements all this for you.
|
|
|
|
|