|
Okay, that's not inheritance. What you would be looking to do is either make this a public property or public field and have the other form access that if necessary. Now, if your other form is opened by this one, a better method would be to raise an event back to this form, and set the values based off that event. That way you stop the other form having to know about Rycerz.
|
|
|
|
|
well i dont understand exatly what you mean, make the variable gracz public? how do i make the other form to access it (i made variable public but still other form cant see it)? sorry for such noob questions.
|
|
|
|
|
I think you want something like this:
public class Child : System.Windows.Forms.Form
{
private int _privateField = 0;
public int PublicProperty
{
get { return(_privateField); }
set { _privateField = value; }
}
}
public class Parent : System.Windows.Forms.Form
{
private void SomeMethod
{
Child child = new Child();
child.PublicProperty = 42;
child.ShowDialog();
int processedValue = child.PublicProperty;
child.Dispose();
}
}
In the parent form, you create an instance of the child form and pass the initial integer value to it via its public property. The child form can then work with it in every way you can think of.
ShowDialog() is a blocking call. It returns when the child form closes. You can then still read from it until it is disposed. That way you can get the changed value of PublicProperty.
You can as well create multiple public properties. That could be necessary if child form's processing results in a value of different type than the input.
Ciao,
luker
|
|
|
|
|
You form would not inherit from form but would inherit from your base form. I THINK that's what you're trying to do. It's not clear at all.
A class can inherit from only one other base class, but that base class can itself inherit from some other class, like System.Windows.Form.
|
|
|
|
|
i will state it other way, i have a variable created in one form i need to use it in other form how can do that?
|
|
|
|
|
Make a public Property that returns the value.
|
|
|
|
|
You can make your own Dialog Form instead of trying to inherit from the existing class.
Or easier, your form can inherit from the Dialog class.
|
|
|
|
|
Hi Guys ,
Could you please let me know how to consume the following REST service from C# code. Your answers will be very helpful. Thanks in advance.
Authenticate(POST)
http://localhost:50448/UserRest/GetSampleMethod_With_OAuth
json script -Request Body
{
"Password":"subin",
"UserName":"Subin"
}
Get All(GET)
http://localhost:50448/UserRest/getall?token={token}
Update(PUT)
http://localhost:50448/UserRest/update?token={token}
json script -Request Body
{
"Bank":"String content",
"DOB":"String content",
"ID":"1",
"Name":"String content"
}
Delete(DELETE)
http://localhost:50448/UserRest/delete/1?token={token}
Logout(POST)
http://localhost:50448/UserRest/LogoutSession?token={token}
With Regards
|
|
|
|
|
|
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.
|
|
|
|
|