Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot declare a variable of static type and Cannot create an instance of the static class [closed]

Tags:

c#

First apologize for my duplicate question for short time..

again i have problem on use class in other place and my errors is:

Error 1 Cannot declare a variable of static type 'umEdition.Program'    
Error 2 Cannot create an instance of the static class 'umEdition.Program'   

and my code is:

using DBNUpdater;
using System;
using System.IO;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Windows.Forms;
using System.Reflection;


namespace umEdition
{
internal static class Program
{
    [CompilerGenerated]
    private static bool BackingField;
    private static bool openUpdater = false;
    private static ProgressDialog pdialog;
    private static bool updaterFinished = false;

    private static void _core_Completed(object sender, EventArgs e)
    {
        updaterFinished = true;
        Completed = true;
    }

    private static void _core_Failed(object sender, FailedEventArgs e)
    {
        MessageBox.Show(e.Exception.ToString(), "loader", MessageBoxButtons.OK, MessageBoxIcon.Hand);
        Completed = true;
        updaterFinished = false;
    }

    private static void _core_StatusChanged(object sender, StatusChangedEventArgs e)
    {
        if (e.StatusText == "Downloading")
        {
            if (pdialog != null)
            {
                pdialog.Line1 = e.StatusText;
                pdialog.Line2 = e.DetailedStatus + string.Format(" ({0}%)", Math.Round(e.ExactPercentage, 1));
                pdialog.Line3 = " ";
                pdialog.Value = (uint)(e.ExactPercentage * 10.0);
            }
            if (!Completed)
            {
                openUpdater = true;
                Completed = true;
            }
        }
    }

    [MTAThread]
    private static void Main1()
    {
        string[] strArray = new string[] { "a", "b", "c", "d", "e" };
        Random random = new Random();
        string str = strArray[random.Next(0, strArray.Length)];
        Completed = false;

        var icon = new NotifyIcon();
        icon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
        icon.Text = "PlusGamer - starting";
        icon.Visible = true;

        Core core = new Core();
        core.CacheLocation = Environment.CurrentDirectory;
        core.WebCacheServer = string.Format("http://mysite.com/", str);
        core.LocalCacheServer = "";
        core.WantedCaches = new string[] { "anyfiles" };
        core.EnableUploading = false;
        core.StatusChanged += new EventHandler<StatusChangedEventArgs>(Program._core_StatusChanged);
        core.Failed += new EventHandler<FailedEventArgs>(Program._core_Failed);
        core.Completed += new EventHandler<EventArgs>(Program._core_Completed);

        core.Start();

        var time = DateTime.UtcNow;
        var shown = false;

        while (!Completed)
        {
            System.Threading.Thread.Sleep(1);

            var newTime = DateTime.UtcNow;
            var diff = newTime - time;

            if (diff.TotalMilliseconds > 2000)
            {
                if (!shown)
                {
                    icon.ShowBalloonTip(2500, "any text ;)", ToolTipIcon.Info);

                    shown = true;
                }
            }
        }
        if (openUpdater)
        {
            Completed = false;
            pdialog = new ProgressDialog(IntPtr.Zero);
            pdialog.Title = "loading";
            pdialog.Maximum = 0x3e8;
            pdialog.ShowDialog(new ProgressDialog.PROGDLG[1]);
        }
        while (!Completed)
        {
            Thread.Sleep(1);
            if ((pdialog != null) && pdialog.HasUserCancelled)
            {
                core.Kill();
                return;
            }
        }
        if (updaterFinished)
        {
            if (!File.Exists("chrome.exe"))
            {
                //MessageBox.Show("Cannot find iw4m.exe");
                MessageBox.Show("OPSS!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);


                return;
            }

            int promptValue = Prompt.ShowDialog("lol", "lol2");

        }

        icon.Visible = false;
        icon.Dispose();

    }

    public static class Prompt
    {
        public static int ShowDialog(string text, string caption)
        {
            Form prompt = new Form();
            prompt.Width = 500;
            prompt.Height = 100;
            prompt.Text = caption;
            Label textLabel = new Label() { Left = 50, Top = 20, Text = text };
            prompt.ShowDialog();
            return 1;
        }
    }

    public static bool Completed
    {
        [CompilerGenerated]
        get
        {
            return BackingField;
        }
        [CompilerGenerated]
        set
        {
            BackingField = value;
        }
    }
}
}

Then i try use class Program from the above code in here:

 private void Window_Loaded(object sender, RoutedEventArgs e)
    {

        Program update = new Program();

    }

But i have error on it?! again thank you for help.

like image 402
artin es Avatar asked Jun 08 '13 22:06

artin es


1 Answers

Because your Program class (just like everything else in your code, seems like) is defined as static - which means that you cannot create a new instance of it. And you can't declare a variable of type Program, again, because it is static.

like image 125
Igal Tabachnik Avatar answered Sep 20 '22 14:09

Igal Tabachnik