Reputation: 21
I'm a beginner in the programming world and taking a C# class and I am struggling with the basics.
I'm finding that when the basics are defined one by one I (feel like) understand them just fine.
However when I see a statement I really struggle to understand how they work together.
//We are using "Murach's C# 2012"
for your reference.
For instance when I see the code below I have trouble defining the type, method, variable, arguments and parameters. I was hoping someone might have some tips or tricks that helps/helped them.
string subtotal = Convert.ToDecimal(txtSubtotal.Text);
string is the method or object? subtotal is a variable that converts the "txtSubtotal.Text"
value in the text box.
Perhaps the answer is to just spend more time and work on more projects but it cannot hurt to ask.
Also wanted to add that this is an online class and the professor is very lacking with answers as far as giving examples or further explanation. I have told him so and I pretty much received a response of "Sorry".
Thanks!
Shaun
Upvotes: 2
Views: 624
Reputation: 2945
Microsoft's network is a good place to start looking for the basics.
I suggest you start looking at things in this perspective: what is the input?what is the output?
In the example you have posted, Convert.ToDecimal()
is a method of the Convert
class which requires a string as an input (parameter). String
is a DataType
which can hold alpha-numerals, special characters (except escape characters) etc. The TextBox
txtSubtotal text is converted to Decimal
DataType
which is assigned to a String
variable called subtotal.
Above all this have lots of patience.
Upvotes: 0
Reputation: 71187
string subtotal = Convert.ToDecimal(txtSubtotal.Text);
This assignment expression does not compile. Let's break it down.
string subtotal
we're declaring a variable here. The name's subtotal
, the type's string
.
=
in this case the assignment is joined with the declaration: in a single instruction we're going to declare a variable and assign it - at the end of it (;
) the subtotal
variable will take the value of what's on the right of the assignment operator (=
).
Convert.ToDecimal()
is a method call. A static
method, in fact. You can tell because you're calling this method on the Convert
type, without having an instance of that type (i.e. you didn't need to do new Convert()
). The method takes a value of some type (permitted by one of its overloads), and converts it to a decimal
value. Because the left operand of the assignment is a string
and we're not converting our decimal
to a string
here, the assignment will fail to compile. We can add .ToString()
at the end, to convert the decimal
back to a string
and the assignment will work.
ToString()
is needed to make it work, because there is no implicit conversion defined betweendecimal
andstring
.
txtSubtotal
is an object, probably an instance of a TextBox
class. If that's the case, this object derives from Control
and is capable of being rendered on a Form
.
.Text
is a property of the TextBox
object, a string
representing its displayed value.
Upvotes: 2
Reputation: 1759
i'm sorry you are having such a difficult time. software development is a rewarding task, but it's also frustrating and difficult. it's not for everyone. maybe it will work out for you and maybe not. a large part of success in life and software is the idea of failing fast. if you find yourself in something that isn't right for you, you do yourself the best service by getting out quickly before wasting too much time on it. not saying that's right for you - only you can decide that. if you do stick with it and get over the difficulties you are having, i wish you luck.
i'd suggest getting a trial pluralsight account and watching as much of the beginner content on there as you can squeeze into your available time. i think that's the best resource for learning .net and c# and software out there.
that's my answer to the real meat of your question.
as far as your sample line of code - that won't compile. you are doing an assignment of the result of an expression that returns type decimal to a variable of type string. C# won't allow that.
string subTotal is a declaration of a variable of type string. = Convert.(...) is an assignment of the return value of a (static) function to said variable.
Upvotes: 0
Reputation: 468
string is the type of your subtotal variable
Convert is a class
txtSubtotal is the name of your TextBox control and Text is the text contained in it (which is of type string)
ToDecimal is a method of the Convert class allowing you to convert txtSubtotal.Text to a decimal type
Unfortunatly this code will not compile because C# will expect the type of subtotal to be of type decimal
Upvotes: 0
Reputation: 3146
You cannot have a method on the LHS(left hand side) of an assignment operator. Considering that, you can be sure string is not a method
As for other things, variables which you specify in the call to a method are called parameters and variables specified in the definition of a method are called arguments.
Upvotes: 0
Reputation: 34846
string
is the type.subtotal
is the variable.Convert.ToDecimal()
is a method.txtSubtotal
is a text box control..Text
is a property of the text box control, of type string
.Your example is confusing, because the result of Convert.ToDecimal()
is a decimal
, but your variable is typed as a string
. Either the type of your variable is wrong, or you are needlessly doing a conversion from a string
type (.Text
) to a decimal
.
Upvotes: 1
Reputation: 63065
string
is type and you try to convert textbox text to Decimal
type and set it to a string type
if you need decimal object created from input text then change the type as decimal. like below
decimal subtotal = Convert.ToDecimal(txtSubtotal.Text);
Upvotes: 1