我如何重用代码 [英] How do I re-use code

查看:58
本文介绍了我如何重用代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我基本上试图在c#中重用代码,有点像方法(尽管我对如何使用方法有些不确定)。一个例子是,如果我想重新使用几行代码,使几个文本框可见。将非常感谢帮助!

So I'm basically trying to re-use a code within c#, sort of like a method (although I'm a little unsure about how to use methods). An example is if I wanted to re-use several lines of code that makes several textboxes visible. Help will be most appreciated!

推荐答案

对不起,这与询问如何编程几乎相同?如果你有点不确定如何使用方法,没有什么可谈的。事实上,使用方法是重用代码的最基本方法之一。首先,为了重用某些东西,你需要知道如何使用某些东西。



无论如何,你所需要的只是从非常开始学习编程开始。拿一些手册然后继续。关于进一步措施的建议可以在我过去的答案中找到:

我的程序有问题。请帮忙! [ ^ ],

C#/ Web开发学习策略 [ ^ ],

实际学习c#.net的最佳方法是什么?[ ^ ],

使用和有意义的框架 [ ^ ],

我希望成为一名优秀的dot net开发人员,使用c# [ ^ ],

什么是Apache Struts和JBoss Hibernate [ ^ ]。



-SA
Sorry, bit this is nearly the same as asking "how to do programming"? If you are a bit unsure how to use methods, there is nothing to talk about. As a matter of fact, using methods is one of the most basic ways to reuse the code. First of all, to "reuse" something, you need to know how to "use" something.

Anyway, all you need is to start learning programming from the very beginning. Grab some manual and go ahead. My recommendations on further steps can be found in my past answers:
I have a problem with my program. Please help![^],
C# / Web Development learning strategy[^],
What is the best way to learn c#.net practically[^],
using and meaningful of Framework[^],
I want to be a good dot net developer using language c#[^],
What are Apache Struts and JBoss Hibernate[^].

—SA


重用行的最基本方法是使用方法。你可以使用这样的方法:

The most basic way to re-use lines is to use a method. You can use a method like this:
void ShowTextboxes()
{
   // put the lines that you want to re-use here
}



然后,在您表单中的另一个地方,您可以这样调用您的方法:


Then, on another place in your form, you can call your method like this:

ShowTextboxes();



在这里阅读更多关于方法的信息:

MSDN:方法(C#编程指南) [ ^ ]


方法与基于类的函数相同。在C#中,您编写的每一段代码都将采用某种方法。



这意味着您拥有此代码:

Methods are the same thing as class based functions. In C# every bit of code you write will be in a method of some sort.

It means where you have this:
statement1;
statement2;
statement3;



你可以创建一个这样的函数:


You can create a function that does that:

void MyFunction()
{
    statement1;
    statement2;
    statement3;
}



然后可以通过调用函数执行三个语句:


and can then execute the three statements by calling the function:

MyFunction();



这真的是软件开发的第1页,所以我建议你在尝试之前先阅读。


This really is page 1 in software development so I'd advise you read up before trying anything more.


这篇关于我如何重用代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆