简体   繁体   English

C#在基础方面苦苦挣扎

[英]C# struggling with the basics

I'm a beginner in the programming world and taking a C# class and I am struggling with the basics. 我是编程世界的初学者,参加了C#课程,并且在基础方面苦苦挣扎。 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. //我们正在使用"Murach's C# 2012"作为参考。

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. 小计是一个变量,它转换文本框中的"txtSubtotal.Text"值。

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 肖恩

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. string subtotal我们在这里声明一个变量。 The name's subtotal , the type's string . 名称的subtotal ,类型的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 ( = ). =在这种情况下,赋值与声明一起加入 :在一条指令中,我们将声明一个变量并对其进行赋值-在变量( ; )的末尾, subtotal变量将采用变量右边的值。赋值运算符( = )。

  • Convert.ToDecimal() is a method call . Convert.ToDecimal()方法调用 A static method, in fact. 实际上是static方法。 You can tell because you're calling this method on the Convert type, without having an instance of that type (ie you didn't need to do new Convert() ). 您可以知道,因为您是在Convert类型上调用此方法的,而没有该类型的实例(即,您不需要执行new Convert() )。 The method takes a value of some type (permitted by one of its overloads ), and converts it to a decimal value. 该方法采用某种类型的值(由其重载之一允许),并将其转换为decimal值。 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 . 由于赋值的左操作数是一个string并且此处我们没有将decimal转换为string ,因此赋值将无法编译 We can add .ToString() at the end, to convert the decimal back to a string and the assignment will work. 我们可以在末尾添加.ToString() ,以将decimal转换回string ,赋值将起作用。

    ToString() is needed to make it work, because there is no implicit conversion defined between decimal and string . 需要ToString()使其起作用,因为在decimalstring之间没有定义隐式转换

  • txtSubtotal is an object , probably an instance of a TextBox class. txtSubtotal是一个对象 ,可能是TextBox类的实例 If that's the case, this object derives from Control and is capable of being rendered on a Form . 如果是这样,则此对象派生自 Control并能够在Form上呈现。

  • .Text is a property of the TextBox object, a string representing its displayed value. .TextTextBox对象的属性 ,一个string表示其显示的值。

string is type and you try to convert textbox text to Decimal type and set it to a string type string是类型,您尝试将文本框文本转换为Decimal类型并将其设置为字符串类型

if you need decimal object created from input text then change the type as decimal. 如果需要从输入文本创建的十进制对象,则将类型更改为十进制。 like below 像下面

decimal subtotal = Convert.ToDecimal(txtSubtotal.Text);
  • string is the type. string是类型。
  • subtotal is the variable. subtotal是变量。
  • Convert.ToDecimal() is a method. Convert.ToDecimal()是一种方法。
  • txtSubtotal is a text box control. txtSubtotal是一个文本框控件。
  • .Text is a property of the text box control, of type string . .Text是文本框控件的属性,类型为string

Your example is confusing, because the result of Convert.ToDecimal() is a decimal , but your variable is typed as a string . 您的示例令人困惑,因为Convert.ToDecimal()的结果是decimal ,但是您的变量键入为string Either the type of your variable is wrong, or you are needlessly doing a conversion from a string type ( .Text ) to a decimal . 变量类型错误,或者不必要地将string类型( .Text )转换为decimal

You cannot have a method on the LHS(left hand side) of an assignment operator. 您不能在赋值运算符的LHS(左侧)上使用方法。 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. 至于其他事情,您在方法调用中指定的变量称为参数,而在方法定义中指定的变量称为参数。

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) txtSubtotal是TextBox控件的名称,Text是包含在其中的文本(字符串类型)

ToDecimal is a method of the Convert class allowing you to convert txtSubtotal.Text to a decimal type ToDecimal是Convert类的方法,允许您将txtSubtotal.Text转换为十进制类型

Unfortunatly this code will not compile because C# will expect the type of subtotal to be of type decimal 不幸的是,该代码无法编译,因为C#希望小计的类型为小数类型

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. 我认为这是学习.net和C#和软件的最佳资源。

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. C#不允许这样做。

string subTotal is a declaration of a variable of type string. 字符串subTotal是字符串类型的变量的声明。 = Convert.(...) is an assignment of the return value of a (static) function to said variable. = Convert。(...)是将(静态)函数的返回值分配给所述变量。

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). 在您发布的示例中, Convert.ToDecimal()Convert类的方法,该方法需要字符串作为输入(参数)。 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. String是一个DataType可容纳的α-数字,特殊字符(除转义字符)等等。 TextBox txtSubtotal文本转换为Decimal DataType被分配给一个String称为小计变量。

Above all this have lots of patience. 最重要的是,要有足够的耐心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM