-1

For example in the class file:

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

Then:

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

Somehow to assign fileuploadpercentage that is int to test2 or test3 and fileuploadstatus to test1 and then in 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";
                    }

Maybe not using switch case but somehow to make that it will return both test1 and test2 so if in the class file:

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

Return both test1 and test2 at the same time. Two parameters but return them whtnh it's "Uploading"

What i tried so far:

In the new form i added this:

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

Now i have in this new form two events how do i use in the events with the UploadState class ?

The first event:

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

The second event:

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

Also in form1 in all the return i'm getting errors:

Error 1 Cannot implicitly convert type 'Automatic_Record.Youtube_Uploader.UploadStatus' to 'string'

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);

On each return i'm getting same error can't convert to string.

Also in this line in form1:

return new Youtube_Uploader.UploadStatus(Youtube_Uploader.fileuploadpercentages);

I'm getting the also the error:

Error 3 Argument 1: cannot convert from 'Automatic_Record.Youtube_Uploader.UploadStatus' to 'Automatic_Record.Youtube_Uploader.UploadState'

Daniel van wolf
  • 393
  • 1
  • 5
  • 16

2 Answers2

0

Create a class to hold your information:

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

You can return an instance of that class:

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

You can then inspect the returned UploadStatus instance and set test1 ans test2 values accordingly.

Damir Arh
  • 17,637
  • 2
  • 45
  • 83
  • Damir i added the class to my class file. But how do i make the instance/s for the int and string ? In the class file where i added your class inside this IF: if (stringProgressReport[1] == "Uploading") what do i do ? – Daniel van wolf Sep 07 '15 at 19:10
  • I did inside the IF: UploadStatus uploadstat = new UploadStatus((int)Math.Round(((double)obj.BytesSent) / totalBytes * 100)); do i need to make two instances of the UploadStatus ? – Daniel van wolf Sep 07 '15 at 19:11
  • @Danielvanwolf What you did, should be ok. Why would you need 2 instances? You could add another constructor with both string and int parameters if you need to set both values explicitly. Also, as bashis mentioned, it would be a good idea replacing the string with an enum. – Damir Arh Sep 08 '15 at 04:50
  • Damir i updated my question just one added at the bottom the part of what i tried now. Please if you can take a look on it since it's not working yet. What i did wrong ? And after fixing that how to use enum if you could show me please ? – Daniel Lip Sep 08 '15 at 07:11
  • @DanielLip I don't see any edits in the question!? I've updated my answer to use enums instead of strings. – Damir Arh Sep 08 '15 at 14:08
  • Damir the other side is a new form not class file. But still how do i use the UploadStatus and the UploadState classes with the two events ? And the errors i get in form1. – Daniel van wolf Sep 08 '15 at 15:57
  • To fix the first error, change `public static string uploadstatus = "";` to `public static UploadStatus uploadstatus = "";`. As the error stated, you are now trying to put `UploadStatus` value into `string` variable. – Damir Arh Sep 08 '15 at 17:13
  • @Danielvanwolf To fix the rest of the errors, make sure the types match everywhere. The compiler is giving you errors to show mismatches. I think you need to have `UploadStatus` as the return value for the methods with all the `return` statements in your snippets. – Damir Arh Sep 08 '15 at 17:15
0

First of all, it is considered a really bad practice to use string constants to describe a state of an object or a program. You should use enum instead.

Secondly, if you want to return a couple of parameters you could, depending on your needs, do it in a several ways:

  1. Create a struct to keep them in a single object. You will have to write some lines but the resulting code will be more or less clear.
  2. Return a Tuple<T1,T2>. This does not require a lot of writing but I personally hate remembering what Item1 and Item2 were intended to be. But if you still think you want to use it, consider naming your method carefully.
  3. You could also return your results as out parameters. This is rarely a good decision but it really helps in a TryParse-like code.
Community
  • 1
  • 1
bashis
  • 1,200
  • 1
  • 16
  • 35
  • Can you show me please how to do it with one single object struct ? – Daniel van wolf Sep 07 '15 at 19:09
  • @Danielvanwolf it is just like @DamirArh suggested, but I would rather use `struct` instead of `class` because your piece of code likely mathes [all the rules that are generally applied to structs](http://msdn.microsoft.com/en-us/library/ms229017.aspx) – bashis Sep 07 '15 at 19:13