C#中的重载方法帮助 [英] Overload method help in C#

查看:80
本文介绍了C#中的重载方法帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
我是C#编程的初学者.我正在用这种语言学习课程,但很不幸,我被卡住了:(


该作业是关于电影院系统的.
用户输入值----->如果有效,它将显示在列表框中,其中包含人员姓名,门票价格和当天的总收入.

以上我没有问题!
作业需要我上两节课.在第二堂课中
我需要将文本框的价格值转换为双精度值.
到目前为止没有问题!

在第二个类中,我需要实现一个代码来检查用户是否
代码完全填充了minlimit值,该值是0,并且在maxLimit
之下 我已经把它加到一千了.代码看起来像这样:

Hi!
Im a beginner at C# programming. Im studying a class in this language and unfortunatly im stuck:(


The assignment is about a Cinema system.
Userimputs a value-----> if valid it appears on the listbox with person name, ticket price and total revenue for the day.

the above i have no problem with!
The assignment needs me to have two classes. And in this second class
I need to convert the textbox price value into a double value.
so far no problem!

In this second class im requierd to implement a code that checks if the users
code fullfills the minlimit value which is 0 and that it is under the maxLimit
which i have put to a thousand. code looks like this:

public static bool GetDouble(string StringToConvert, out double dblOutValue, double minlimit, double maxlimit= 100) {
 
    bool ConvertedDoubleNumber = Double.TryParse(StringToConvert, out dblOutValue);

    if (dblOutValue == maxlimit)
    {
        MessageBox.Show("Please enter a valid number ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }



我假设用户输入的内容将保存在dblOutValue中,但显然不会保存.

请记住Kepp,这只是一小段代码.我有一个mainform,因为这是一个包含大量代码的表单应用程序.

这里唯一需要注意的是,代码将被发送到第二个类(效用类),并通过使用tryparse转换为双精度值.

我需要那个双精度值来发布最大/最小限制,为此我需要帮助.
这需要重载,我尝试了重载,但是变量dblOutValue
在其他方法中不存在,因为我不能使用相同的参数.
如果我只使用maxlimit和minlimit,我将无法正常工作.


请帮忙!
问候
彼得!



I assumed that the users imput would get saved in dblOutValue but apperantly it does not get saved.

Kepp in mind that this is only a small code. I have a mainform since this is a forms application which has a bunch of code.

The only relevant thing to note here is that a code gets sent to the second class(utillityclass) and gets converted to a double value by suing tryparse.

I need that double value to issue a max/minlimit and for that i need help.
This requiers overloading and I tried that but the variable dblOutValue
does not exist in the other method since i cant use the same inparametes.
if i only use maxlimit and minlimit i cant get it to work.


Please help!
Regards
Peter!

推荐答案

彼得,

您的目标应该很容易实现,但是我认为您混杂了一些东西.
希望下面的代码示例可以完成您想要实现的目标.
如果输入"值是< ;,则只返回布尔值假". 1000.


Hi Peter,

your goal should be easy to achieve, but I think you mixed a few things.
Hope my code sample below is doing what you what you want to achieve.
It simply returns a Boolean "false" if "input"-value is < 1000.


//Set Variables to save values after invoking GetDouble() In your MainForm
bool IsMaxLimit = false;
double out_value;
yes = GetDouble("646", out out_value, 100);
MessageBox.Show(IsMaxLimit.ToString() + " " + out_value);







public static bool GetDouble(string StringToConvert, out double dblOutValue, double minlimit, double maxlimit = 1000)
        {
            //I didn't recognized where you want to check the conversation in your code
            //bool ConvertedDoubleNumber = Double.TryParse(StringToConvert, out dblOutValue);

            if (dblOutValue < maxlimit)
            {
                return false;
            }
            else
            {
                return true;
            }
        }




希望它对您有用.

此致




Hope it works for you.

Best Regards


这篇关于C#中的重载方法帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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