C# beginner need some pro advice

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 174
Reputation: TobbeK is an unknown quantity at this point 
Solved Threads: 3
TobbeK TobbeK is offline Offline
Junior Poster

Re: C# beginner need some pro advice

 
0
  #11
Jan 19th, 2009
VBScript mostly and VB. Completely different languages.

What do I need to pass for example the variable ReadName and pick just that one up into the main method. As you can see I have no problem creating new instances of the class and pass the entire method ReadInput to the main.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: C# beginner need some pro advice

 
0
  #12
Jan 19th, 2009
OK, but how would you have made a sub in vb, to send it say 2 parameters, and return the sum of them as a result?

(c# while different is not as hugely different in this respect)
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,006
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 298
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Postaholic

Re: C# beginner need some pro advice

 
0
  #13
Jan 19th, 2009
Perhaps it is something like this you want?
Will this clarify your mind?

  1. class Product
  2. {
  3. public string ReadName; //name of the product
  4. //other fields here
  5.  
  6. public void ReadInput()
  7. {
  8. Console.Write(" Product name: ");
  9. string ReadName = Console.ReadLine();
  10. //other things here
  11. }
  12. //other methods here
  13. }
  14.  
  15. class MyConsoleApp
  16. {
  17. static void Main(string[] args)
  18. {
  19. Product objGet = new Product();
  20. objGet.ReadInput();
  21.  
  22. //now here I can use
  23. Console.WriteLine("Name of product : {0}", objGet.ReadName);
  24.  
  25. Console.ReadLine();
  26. }
  27. }
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 174
Reputation: TobbeK is an unknown quantity at this point 
Solved Threads: 3
TobbeK TobbeK is offline Offline
Junior Poster

Re: C# beginner need some pro advice

 
0
  #14
Jan 19th, 2009
Yes thanks!

I dont know the difference between set public variable as you did and a method. But it seem to be a good start.

I will work with this a bit and send you a feedback here!
Thanks
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 174
Reputation: TobbeK is an unknown quantity at this point 
Solved Threads: 3
TobbeK TobbeK is offline Offline
Junior Poster

Re: C# beginner need some pro advice

 
0
  #15
Jan 19th, 2009
No errors, but no one of the public variables can be assigned to any values. No matter if I try to change data types. I dont know whats wrong. My code here should do a simple calculation and print out the results.

  1. class Product
  2. {
  3. public string ReadName; //name of the product
  4. public double ReadPrice; //price of the product
  5. public int UnitCount; //Unit count
  6. public string FoodItem; //Unit count
  7.  
  8. public void ReadInput()
  9. {
  10. Console.Write(" Product name: ");
  11. string ReadName = Console.ReadLine();
  12. Console.Write(" Product price: ");
  13. double ReadPrice = Convert.ToDouble(Console.ReadLine());
  14. Console.Write(" Unit count: ");
  15. int UnitCount = Convert.ToInt32(Console.ReadLine());
  16. Console.Write(" Food item y/n: ");
  17. string FoodItem = Console.ReadLine();
  18. }
  19.  
  20. static void Main(string[] args)
  21. {
  22. Product objGet = new Product();
  23. objGet.ReadInput();
  24.  
  25. double iPrice = objGet.ReadPrice;
  26. int iCount = objGet.UnitCount;
  27.  
  28. double iCalc = iPrice * iCount;
  29.  
  30. Console.Write(" Price total: {0}", iCalc);
  31.  
  32. Console.ReadLine();
  33. }
  34. }
Last edited by TobbeK; Jan 19th, 2009 at 3:07 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,006
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 298
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Postaholic

Re: C# beginner need some pro advice

 
0
  #16
Jan 19th, 2009
That is totaly my fault! Not yours!
  1. public void ReadInput()
  2. {
  3. Console.Write(" Product name: ");
  4. string ReadName = Console.ReadLine();
  5. Console.Write(" Product price: ");
  6. double ReadPrice = Convert.ToDouble(Console.ReadLine());
  7. Console.Write(" Unit count: ");
  8. int UnitCount = Convert.ToInt32(Console.ReadLine());
  9. Console.Write(" Food item y/n: ");
  10. string FoodItem = Console.ReadLine();
  11. }
Can you see why?
I redefined ReadName as a local string variable of the method ReadInput!
Console.Write(" Product name: ");
string ReadName = Console.ReadLine();

should be
Console.Write(" Product name: ");
ReadName = Console.ReadLine();

do the same for the rest of your variables.
Sorry for the trouble, but do you know the definition of an expert?
An expert is a person who has made all the possible mistakes...
Last edited by ddanbe; Jan 19th, 2009 at 3:35 pm.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 276
Reputation: rapture has a spectacular aura about rapture has a spectacular aura about 
Solved Threads: 37
rapture rapture is offline Offline
Posting Whiz in Training

Re: C# beginner need some pro advice

 
1
  #17
Jan 19th, 2009
hmm, well then ddanbe I am fast on my way to becoming an expert in everything!
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 174
Reputation: TobbeK is an unknown quantity at this point 
Solved Threads: 3
TobbeK TobbeK is offline Offline
Junior Poster

Re: C# beginner need some pro advice

 
0
  #18
Jan 19th, 2009
Thanks again...
That was it !

I agree, and I must do a lot of mistakes myself to learn this. It is fun language but a little bit hard sometimes.

I am sure I will be back here soon..

best regards
Torbjorn
Last edited by TobbeK; Jan 19th, 2009 at 3:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 276
Reputation: rapture has a spectacular aura about rapture has a spectacular aura about 
Solved Threads: 37
rapture rapture is offline Offline
Posting Whiz in Training

Re: C# beginner need some pro advice

 
0
  #19
Jan 19th, 2009
if that fixed your problem then mark the thread as solved for him
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,006
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 298
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Postaholic

Re: C# beginner need some pro advice

 
1
  #20
Jan 19th, 2009
Torbjorn, feel free to come back with your questions. Mark this thread as solved if you want.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC