【问题标题】:How can i create in a class file a local parameters class and pass the parameters and use them in form1?如何在类文件中创建本地参数类并传递参数并在 form1 中使用它们?
【发布时间】:2015-09-07 18:08:21
【问题描述】:

例如在类文件中:

public class DoSomething
{
   public string test { get; set; }
   public string test1 { get; set; }
   public int test2 { get; set; }
   public int test3 { get; set; }
   // others
}

然后:

if (stringProgressReport[1] == "Uploading")
            {
                fileuploadpercentages = (int)Math.Round(((double)obj.BytesSent) / totalBytes * 100);
                fileuploadstatus = "uploading file";
            }

以某种方式将 int 的 fileuploadpercentage 分配给 test2 或 test3 并将 fileuploadstatus 分配给 test1 然后在 form1 中:

if (request.QueryString[0] == "uploadstatus")
                {
                    switch (Youtube_Uploader.fileuploadstatus)
                    {
                        case "uploading file":
                            return "uploading";

                        case "status":
                            return Youtube_Uploader.fileuploadpercentages.ToString();

                        case "file uploaded successfully":
                            Youtube_Uploader.fileuploadstatus = "";
                            return "upload completed";

                        default:
                            return "upload unknown state";
                    }

也许不使用 switch case,但以某种方式使其返回 test1 和 test2 所以如果在类文件中:

if (stringProgressReport[1] == "Uploading")

同时返回 test1 和 test2。两个参数,但在“上传”时返回它们

到目前为止我尝试了什么:

在新表单中我添加了这个:

public enum UploadState
{
    Uploading,
    InProgress,
    Completed,
    Unknown
}

public class UploadStatus
{
    public UploadState State { get; private set; }
    public int Progress { get; private set; }

    public UploadStatus(UploadState state)
    {
        State = state;
    }

    public UploadStatus(int progress)
    {
        Progress = progress;
        State = UploadState.InProgress;
    }
}

现在我在这个新表单中有两个事件,如何在带有 UploadState 类的事件中使用?

第一个事件:

public static string uploadstatus = "";
        private void videosInsertRequest_ResponseReceived(Video obj)
        {
            uploadstatus = obj.Status.UploadStatus;
            if (uploadstatus == "uploaded")
            {
                //fileuploadstatus = new UploadStatus("file uploaded successfully");
            }

第二个事件:

private void videosInsertRequest_ProgressChanged(IUploadProgress obj)
        {
            stringProgressReport[1] = obj.Status.ToString();
            if (stringProgressReport[1] == "Uploading")
            {

                //percentComplete = (int)Math.Round(((double)obj.BytesSent) / totalBytes * 100);
                //fileuploadstatus = "status";
                //fileuploadpercentages = new UploadStatus((int)Math.Round(((double)obj.BytesSent) / totalBytes * 100));

                //fileuploadstatus = new UploadStatus("uploading file");// + fileuploadpercentages;
            }

在所有返回的 form1 中,我都遇到了错误:

错误 1 ​​无法将类型“Automatic_Record.Youtube_Uploader.UploadStatus”隐式转换为“字符串”

case "uploading file":
                            return new Youtube_Uploader.UploadStatus(Youtube_Uploader.UploadState.Uploading);

                        case "status":
                            return new Youtube_Uploader.UploadStatus(Youtube_Uploader.fileuploadpercentages);

                        case "file uploaded successfully":
                            Youtube_Uploader.fileuploadstatus = "";
                            return new Youtube_Uploader.UploadStatus(Youtube_Uploader.UploadState.Completed);

                        default:
                            return new Youtube_Uploader.UploadStatus(Youtube_Uploader.UploadState.Unknown);

每次返回时,我都会遇到相同的错误,无法转换为字符串。

同样在form1的这一行中:

return new Youtube_Uploader.UploadStatus(Youtube_Uploader.fileuploadpercentages);

我也收到了错误:

错误 3 参数 1:无法从“Automatic_Record.Youtube_Uploader.UploadStatus”转换为“Automatic_Record.Youtube_Uploader.UploadState”

【问题讨论】:

  • 也许使用元组 来返回两个值?

标签: c#


【解决方案1】:

首先,使用字符串常量来描述对象或程序的状态被认为是一种非常糟糕的做法。你应该改用enum

其次,如果您想返回几个参数,您可以根据您的需要,通过以下几种方式进行:

  1. 创建一个struct 以将它们保存在一个对象中。您将不得不编写一些代码,但生成的代码或多或少会清晰。
  2. 返回Tuple<T1,T2>。这不需要大量的写作,但我个人讨厌记住 Item1Item2 的意图。但如果你仍然认为你想使用它,consider naming your method carefully
  3. You could also return your results as out parameters. 这很少是一个好的决定,但它确实有助于类似 TryParse 的代码。

【讨论】:

【解决方案2】:

创建一个类来保存您的信息:

public enum UploadState
{
    Uploading,
    InProgress,
    Completed,
    Unknown
}

public class UploadStatus
{
    public UploadState State { get; private set; }
    public int Progress { get; private set; }

    public UploadStatus(UploadState state)
    {
        State = state;
    }

    public UploadStatus(int progress)
    {
        Progress = progress;
        State = UploadState.InProgress;
    }
}

您可以返回该类的实例:

if (request.QueryString[0] == "uploadstatus")
{
    switch (Youtube_Uploader.fileuploadstatus)
    {
        case "uploading file":
            return new UploadStatus(UploadState.Uploading);

        case "status":
            return new UploadStatus(Youtube_Uploader.fileuploadpercentages);

        case "file uploaded successfully":
            Youtube_Uploader.fileuploadstatus = "";
            return new UploadStatus(UploadState.Completed);

        default:
            return new UploadStatus(UploadState.Unknown);
     }
}

然后您可以检查返回的 UploadStatus 实例并相应地设置 test1test2 值。

【讨论】:

  • Damir 我将课程添加到我的课程文件中。但是我如何为 int 和 string 创建实例?在我在这个 IF 中添加你的类的类文件中: if (stringProgressReport[1] == "Uploading") 我该怎么做?
  • 我在 IF 里面做了: UploadStatus uploadstat = new UploadStatus((int)Math.Round(((double)obj.BytesSent) / totalBytes * 100));我需要创建两个 UploadStatus 实例吗?
  • @Danielvanwolf 你做了什么,应该没问题。为什么需要 2 个实例?如果您需要显式设置这两个值,则可以添加另一个带有 string 和 int 参数的构造函数。此外,正如 bashis 所提到的,用枚举替换字符串是个好主意。
  • Damir 我更新了我的问题,只是在我现在尝试的部分底部添加了一个问题。请如果你可以看看它,因为它还没有工作。我做错了什么?如果你能告诉我如何使用枚举?
  • @DanielLip 我在问题中看不到任何编辑!?我已经更新了我的答案以使用枚举而不是字符串。
猜你喜欢
  • 2020-10-09
  • 1970-01-01
  • 1970-01-01
  • 2011-10-24
  • 1970-01-01
  • 1970-01-01
  • 2022-11-25
  • 1970-01-01
  • 2022-07-19
相关资源
最近更新 更多