如何在类文件中创建本地参数类并传递参数并在form1中使用它们?

时间:2015-09-07 18:08:21

标签: c#

例如在类文件中:

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";
            }

以某种方式将fileuploadpercentage(int为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'

2 个答案:

答案 0 :(得分:0)

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

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实例并相应地设置test1 ans test2值。

答案 1 :(得分:0)

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

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

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