943,616 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1465
  • C# RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jan 19th, 2009
0

Re: C# beginner need some pro advice

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.
Reputation Points: 10
Solved Threads: 3
Junior Poster
TobbeK is offline Offline
190 posts
since Feb 2008
Jan 19th, 2009
0

Re: C# beginner need some pro advice

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)
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Jan 19th, 2009
0

Re: C# beginner need some pro advice

Perhaps it is something like this you want?
Will this clarify your mind?

C# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is online now Online
3,735 posts
since Oct 2008
Jan 19th, 2009
0

Re: C# beginner need some pro advice

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
Reputation Points: 10
Solved Threads: 3
Junior Poster
TobbeK is offline Offline
190 posts
since Feb 2008
Jan 19th, 2009
0

Re: C# beginner need some pro advice

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.

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 3
Junior Poster
TobbeK is offline Offline
190 posts
since Feb 2008
Jan 19th, 2009
0

Re: C# beginner need some pro advice

That is totaly my fault! Not yours!
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is online now Online
3,735 posts
since Oct 2008
Jan 19th, 2009
1

Re: C# beginner need some pro advice

hmm, well then ddanbe I am fast on my way to becoming an expert in everything!
Reputation Points: 155
Solved Threads: 41
Posting Whiz in Training
rapture is offline Offline
294 posts
since Jul 2007
Jan 19th, 2009
0

Re: C# beginner need some pro advice

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.
Reputation Points: 10
Solved Threads: 3
Junior Poster
TobbeK is offline Offline
190 posts
since Feb 2008
Jan 19th, 2009
0

Re: C# beginner need some pro advice

if that fixed your problem then mark the thread as solved for him
Reputation Points: 155
Solved Threads: 41
Posting Whiz in Training
rapture is offline Offline
294 posts
since Jul 2007
Jan 19th, 2009
1

Re: C# beginner need some pro advice

Torbjorn, feel free to come back with your questions. Mark this thread as solved if you want.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is online now Online
3,735 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Reverse connection in C#
Next Thread in C# Forum Timeline: incremental flag in C# 2.0 compiler





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC