none
CS0234 C# error

    Question

  • Hello,

    I converted my VB.net project into a C# project using the SharpDevelop Team Vb.net to C# Converter extension in VS2017. As a result of the conversion, some errors were generated.

    There is this code on my LoginForm:-

    using System.Windows.Forms;
    using System;
    
    namespace mydatabase
    {
        public partial class LoginForm
        {
            private void Btnpassword_Click(object sender, EventArgs e)
            {
                if (txtUserName.Text == "mylogin" & txtpassword.Text == "mypassword")
                {
                    this.Visible = false;
                    mydatabase.My.MyProject.MyForms.Form1.Show();
                }
                else
                {
                    MessageBox.Show("You have entered an incorrect login details", "Try Again", MessageBoxButtons.OK, MessageBoxIcon.Question);
                    txtpassword.Text = "";
                    txtUserName.Text = "";
                    txtUserName.Focus();
                }
            }

    An error occurs on the line: -

    mydatabase.My.MyProject.MyForms.Form1.Show();

    and error message is: -

    CS0234: The type or namespace name 'MyProject' does not exist in the namespace 'mydatabase.mydatabase.My' (are you missing an assembly reference?)

    Can you kindly assist me with this issue? Thanks
     
    • Edited by wirejp Wednesday, May 22, 2019 11:31 PM
    Wednesday, May 22, 2019 11:26 PM

Answers

  • It's possible that the problems you are having in the other thread are preventing Form1 code from compiling, which might explain the error in this thread. That is, "'Form1' does not exist" because it won't compile. So you might need to fix those other problems first.

    But there is one more thing to try. For some reason, the compiler is looking for Form1 in mydatabase.mydatabase, not just mydatabase. I'm not sure why it's doing this, but you could try removing the qualification from the line with the error.

    Form1 frm = new Form1();
    frm.Show();

    That qualification shouldn't be necessary anyway, because you have a 'using' statement at the top of the file for mydatabase.


    Edit: Drat. You don't have a using statement. I wasn't looking closely. But you are inside that namespace, which has (more or less) the same effect.
    • Edited by Ante Meridian Friday, May 24, 2019 3:53 AM Wasn't paying attention.
    • Marked as answer by wirejp Friday, May 24, 2019 4:42 AM
    Friday, May 24, 2019 3:33 AM

All replies

  • It means what it says. That namespace doesn't exist. At a guess, you typed one "My" too many, and what you need is

    mydatabase.MyProject.MyForms.Form1.Show();

    If that doesn't work, show us the code for Form1, so we can see what namespace it is in.

    Wednesday, May 22, 2019 11:31 PM
  • I would look at the Form1.cs namespace to see what it should be.  I suspect this will work

    mydatabase.Form1 frm = new mydatabase.Form1();
    frm.Show();


    • Proposed as answer by KareninstructorMVP, Moderator Thursday, May 23, 2019 12:23 AM
    • Marked as answer by wirejp Thursday, May 23, 2019 6:01 PM
    • Unmarked as answer by wirejp Friday, May 24, 2019 12:58 AM
    • Marked as answer by wirejp Friday, May 24, 2019 4:46 AM
    • Unmarked as answer by wirejp Friday, May 24, 2019 5:42 AM
    Wednesday, May 22, 2019 11:35 PM
  • I would suspect Ken's reply to be correct. 

    What to understand, "My" is a VB.NET key word (this would be the equivalent in C#), MyProject corresponds to the Property folder in C#,  MyForms in VB.NET provides properties to access forms in the current project which is different in C# e.g.

    var formType = typeof(Form);
    foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
    {
        if (formType.IsAssignableFrom(type))
        {
            // type is a Form
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by wirejp Thursday, May 23, 2019 6:02 PM
    • Unmarked as answer by wirejp Friday, May 24, 2019 12:58 AM
    Thursday, May 23, 2019 12:23 AM
    Moderator
  • Hi Everyone,

    Thank you very much for your feedback. I really appreciate it. I followed Ken's suggestion and the error did not go away and the Form1 was highlighted in red:-

    mydatabase.Form1 frm = new mydatabase.Form1();
    frm.Show();

    However when I tried this code below, the error disappeared.

     this.Visible = false;
     // mydatabase.My.MyProject.MyForms.Form1.Show();
    System.Windows.Forms.Form frm = new System.Windows.Forms.Form();
    frm.Show(); 


    Thursday, May 23, 2019 1:03 PM
  • That will certainly make the error go away, but it won't display your Form1. It will display a standard windows Form with no controls on it (except perhaps the OK and Cancel buttons). You might have to think again.
    Thursday, May 23, 2019 11:12 PM
  • Hi Ante Meridian,

    I am not sure how to fix the problem.

    Friday, May 24, 2019 12:59 AM
  • Greetings wirejp.

    The problem is very simple and straightforward for someone who is fluent in c#, but very difficult to explain in a few sentences to someone who is not. It's to do with namespaces, which you can look up on the MSDN.

    Here's my best attempt at an explanation.

    Namespaces are a way of categorising and partitioning your code into logically related chunks. A class is (usually) defined as existing in a particular namespace, and that namespace is used to access the class elsewhere in the code. For example, you might have one namespace for data and another for utilities that work on the data.

    Her's an example.

    // Define a class in a namespace.
    namespace Space1
    {
       namespace Space2
       {
          public class MyClass1
          {
          }
       }
    }
    
    
    // To use the class elsewhere in the code, we could do this.
    public class MyClass2
    {
       // Define and instantiate a variable of the MyClass1 class called mc1. Qualify it using the namespace in which it belongs.
       Space1.Space2.MyClass1 mc1 = new Space1.Space2.MyClass1();
    }
    
    
    // Or, we could define the namespace at the top of the 
    // source file in a 'using' statement. That way, we don't
    // need to 'qualify' MyClass1 with its namespace.
    
    using Space1.Space2; // This line goes at the top of the file.
    
    public class MyClass2
    {
       MyClass1 mc1 = new MyClass1();
    }

    You could produce the kind of error you are getting by doing something like this.

    namespace Space1
    {
       namespace Space3 // Note the number.
       {
          public class MyClass1
          {
          }
       }
    }
    
    
    // Elsewhere...
    Space1.Space2.MyClass1 mc1 = new Space1.Space2.MyClass1();
    
    
    The above line will produce an error saying Space2 doesn't
    exist in Space1. That's because we defined Space3, but not
    Space2.

    So, getting back to your error...

    mydatabase.My.MyProject.MyForms.Form1.Show();

    It's telling you MyProject doesn't exist in mydatabase.My. So it's like MyProject has not been defined, like Space2 was not defined in the second example above.

    Judging by what Karen said, it looks like the translator got mixed up between VB keywords and C# namespaces. (I don't know a thing about VB, so I take her word for it.)

    I suspect Ken was on the right track, suggesting you need to instantiate a Form1 on that line and call Show (or possibly ShowDialog). If his suggestion didn't work, it's probably because his guess that Form1 is in the namespace mydatabase was wrong. You need to look at the code where the class Form1 is defined to see what the correct namespace is (like Space1 and Space2 in my examples). If you're still having trouble, post that code for the Form1 class and we'll see if we can help.

    Friday, May 24, 2019 1:46 AM
  • Hi Ante Meridian,

    Thank you for the explanation. Here is a snippet of the code on the Form1 class: -

    namespace mydatabase
    {
        public partial class Form1
        {
            public Form1()
            {
                Dt = new DataTable() { TableName = "Orders" };
                InitializeComponent();
    
                this.UpdateCombo();
                ThemeResolutionService.ApplicationThemeName = "MaterialTeal";
            }

    Your assistance is greatly appreciated.



    • Edited by wirejp Friday, May 24, 2019 2:57 AM
    Friday, May 24, 2019 2:16 AM
  • In that case, Ken's guess should have been correct.

    You say that when you tried his suggestion the error didn't go away, but what do you mean exactly? It must have been a different error, surely?

    One thing I do notice is that in the line...

            Public Form1()

    ... there should be a lower-case 'p' in 'public'. It's possible that was stopping the Form1 class from compiling and thus producing an error along the lines of "Form1 does not exist" instead of the namespace error you were getting before.

    Friday, May 24, 2019 2:33 AM

  • I am not sure how to fix the problem.

    Is the new thread you started in another forum related to this same program
    and problem? 

    Failed to parse method 'InitializeComponent' after conversion from vb.net to c#
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/be2d57dc-020a-45f1-9a50-a6ed57ac8c4e/failed-to-parse-method-initializecomponent-after-conversion-from-vbnet-to-c?forum=winforms

    If so, you should not open multiple threads for the same issue. It wastes the 
    time and energy of those posting replies who may not be aware that another 
    thread addresses the same issue and so may duplicate work already done by 
    others.

    If however it relates to a different program or project then starting a new 
    thread as you have done is fine and is the correct protocol.

    - Wayne

    Friday, May 24, 2019 2:38 AM
  • In that case, Ken's guess should have been correct.

    You say that when you tried his suggestion the error didn't go away, but what do you mean exactly? It must have been a different error, surely?

    One thing I do notice is that in the line...

            Public Form1()

    ... there should be a lower-case 'p' in 'public'. It's possible that was stopping the Form1 class from compiling and thus producing an error along the lines of "Form1 does not exist" instead of the namespace error you were getting before.

    Apologies, it was a lower-case 'p' in public, I just updated the code sample in the previous post. 

    When I tried Ken's suggestion, the error did not go away. I meant that the "Form1" was underlined in red and it resulted in 2 error messages: -

    code snippet

    mydatabase.Form1 frm = new mydatabase.Form1();
    frm.Show();

    error message: -

    CS0234: The type or namespace name 'Form1' does not exist in the namespace 'mydatabase.mydatabase' (are you missing an assembly reference?)

    Friday, May 24, 2019 3:11 AM

  • I am not sure how to fix the problem.

    Is the new thread you started in another forum related to this same program
    and problem? 

    Failed to parse method 'InitializeComponent' after conversion from vb.net to c#
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/be2d57dc-020a-45f1-9a50-a6ed57ac8c4e/failed-to-parse-method-initializecomponent-after-conversion-from-vbnet-to-c?forum=winforms

    If so, you should not open multiple threads for the same issue. It wastes the 
    time and energy of those posting replies who may not be aware that another 
    thread addresses the same issue and so may duplicate work already done by 
    others.

    If however it relates to a different program or project then starting a new 
    thread as you have done is fine and is the correct protocol.

    - Wayne

    Hi Wayne,

    Yes, I created another post, but I done so after I had thought that I had initially resolved this issue using Ken's suggestion. But Ante pointed out that my code was not correct and issue still persist. I had posted the other issue in the other forum because, I cannot open the Form Designer  for the Form1 class, as it loads with an error message.  I thought that it was a Form Designer issue and so I should open a topic in the Windows Forms General  Forum. 


    • Edited by wirejp Friday, May 24, 2019 3:18 AM
    Friday, May 24, 2019 3:17 AM
  • It's possible that the problems you are having in the other thread are preventing Form1 code from compiling, which might explain the error in this thread. That is, "'Form1' does not exist" because it won't compile. So you might need to fix those other problems first.

    But there is one more thing to try. For some reason, the compiler is looking for Form1 in mydatabase.mydatabase, not just mydatabase. I'm not sure why it's doing this, but you could try removing the qualification from the line with the error.

    Form1 frm = new Form1();
    frm.Show();

    That qualification shouldn't be necessary anyway, because you have a 'using' statement at the top of the file for mydatabase.


    Edit: Drat. You don't have a using statement. I wasn't looking closely. But you are inside that namespace, which has (more or less) the same effect.
    • Edited by Ante Meridian Friday, May 24, 2019 3:53 AM Wasn't paying attention.
    • Marked as answer by wirejp Friday, May 24, 2019 4:42 AM
    Friday, May 24, 2019 3:33 AM
  • Further info has come to light. I can reproduce your error as per the following screenshot.

    Defining mydatabase.mydatabase as one inside another would have the same effect.

    namespace mydatabase
    {
       namespace mydatabase // This also creates a mydatabase.mydatabase namespace.
       {
       }
    }

    So when you are in the mydatabase namespace (because of the 'using' statement) and you instantiate mydatabase.Form1, the compiler thinks you mean mydatabase.mydatabase.Form1. You probably need to get rid of the double-barrelled namespace to avoid the confusion.


    Edit : And to repeat my error from the post above -  Drat. You don't have a using statement. I wasn't looking closely. But you are inside that namespace, which has (more or less) the same effect.
    • Edited by Ante Meridian Friday, May 24, 2019 3:53 AM Same as above.
    Friday, May 24, 2019 3:45 AM
  • Hi Ante,

    Thank you so much! I changed my code as you shown on the LoginForm and now the error is gone:

    code snippet:-

    Form1 frm = new Form1();
    frm.Show();

    Friday, May 24, 2019 4:44 AM