none
CS0246 and CS0103 Issues in Windows-Form-Application RRS feed

  • Question

  • Hi to all,

    I am almost new at programming (java) and I have just started with C#. I hope its the right place to post my issues.
    I started for educational reasons a window-form application where the user inserts datas in 3 forms and then saves in a xml file.

    Unfortunately, I have stuck because of 2 issues (2x CS0246, CS0103) that I cant resolve. I've searched in web but didn't found any solution.

    Here are the parts of my code:

    Form1.cs:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    
    
    namespace WindowsFormsApp
    {
    
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
    
            public void buttonCreate_Click(object sender, EventArgs e)
            {
                try
                {
                    Information info = new Information();
                    info.Info1 = TextboxInfo1.Text;
                    info.Info2 = TextboxInfo2.Text;
                    info.Info3 = TextboxInfo3.Text;
                    SaveToXML.SaveData(info, "data.xml");
                }
    
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }

    The issues (CS0246) are with Bold  (Form1.cs): Information info = new Information();

    And a CS0103 at SaveToXML.SaveData(info, "data.xml");

    My additional code:

    SaveToXML.cs

    using System;
    using System.xml.serialization;
    using System.IO;
    using System.Text;
    
    
    namespace WindowsFormsApp
    {
        public class SaveToXML
        {
    
            public static void SaveData(object obj, String filename)
            {
                XmlSerializer serial = new XmlSerializer(obj.GetType());
                TextWriter writer = new StreamWriter(filename);
                serial.Serializer(writer, obj);
                writer.Close();
            }
    
        }
    }

    and Information.cs:

    using System;
    using System.Text;
    using System.Linq;
    
    namespace WindowsFormsApp
    {
    
        public class Information
        {
    
            private string Info1;
            private string Info2;
            private string Info3;
            
    
            public string Info1
            {
                get { return Info1; }
                set { Info1 = value; }
            }
    
            public string Info2
            {
                get { return Info2; }
                set { Info2 = value; }
            }
    
            public string Info3
            {
                get { return Info3; }
                set { Info3 = value; }
            }
    
            
        }
    }
    

    Do you have any ideas?

    Wednesday, April 11, 2018 7:37 AM

Answers



  • and Information.cs:

    using System;
    using System.Text;
    using System.Linq;
    
    namespace WindowsFormsApp
    {
    
        public class Information
        {
    
            private string Info1;
            private string Info2;
            private string Info3;
            
    
            public string Info1
            {
                get { return Info1; }
                set { Info1 = value; }
            }
    
            public string Info2
            {
                get { return Info2; }
                set { Info2 = value; }
            }
    
            public string Info3
            {
                get { return Info3; }
                set { Info3 = value; }
            }
    
            
        }
    }


    Does that code not show errors from Intellisense and/or the compiler?

    You appear to have multiple conflicting definitions/declarations - both
    private and public are specified for the same name:

    private string Info1;
    public string Info1

    private string Info2;
    public string Info2

    private string Info2;
    public string Info2

    - Wayne

    • Marked as answer by nkuru Saturday, April 14, 2018 12:35 PM
    Wednesday, April 11, 2018 10:17 PM
  • Greetings nkuru.

    It looks like SaveToXML.cs and Information.cs are not included in your project. This would explain why the classes Information and SaveToXML can't be found, and also why you are not getting the errors Wayne talked about (because Information.cs is not being built).

    On the Visual Studio menu, go to "Project->Add Existing Item" and find those two source files, then try building again. You should find your original errors go away, but the errors Wayne mentioned will appear. You can get rid of the new errors by giving the public properties and private variables different names.



    • Edited by Ante Meridian Thursday, April 12, 2018 1:30 AM Typo.
    • Marked as answer by nkuru Saturday, April 14, 2018 12:27 PM
    • Unmarked as answer by nkuru Saturday, April 14, 2018 12:27 PM
    • Marked as answer by nkuru Saturday, April 14, 2018 12:35 PM
    Thursday, April 12, 2018 1:28 AM

All replies

  • Hi nkuru,

    >>(CS0246) are with Bold  (Form1.cs): Information info = new Information();

    You need to add a namespace to your project:

    using System.Xml.Serialization;//add this

    >>CS0103 at SaveToXML.SaveData(info, "data.xml");

    This issue is because there is not a control named "TextboxInfo1" in your Form, so please change the 'Name' property of textBox1 to TextboxInfo1:

    Regards,

    Frankie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Stanly Fan Wednesday, April 11, 2018 7:55 AM
    Wednesday, April 11, 2018 7:55 AM
  • Hi Frankie,
    I added the
    "using System.Xml.Serialization;" but nothing happened. The issue stills exists.

    About the "Name" Property change, I already had TextboxInfo1, TextboxInfo2, TextboxInfo3
    ...seems it wasn't that the problem.

    Wednesday, April 11, 2018 2:01 PM


  • and Information.cs:

    using System;
    using System.Text;
    using System.Linq;
    
    namespace WindowsFormsApp
    {
    
        public class Information
        {
    
            private string Info1;
            private string Info2;
            private string Info3;
            
    
            public string Info1
            {
                get { return Info1; }
                set { Info1 = value; }
            }
    
            public string Info2
            {
                get { return Info2; }
                set { Info2 = value; }
            }
    
            public string Info3
            {
                get { return Info3; }
                set { Info3 = value; }
            }
    
            
        }
    }


    Does that code not show errors from Intellisense and/or the compiler?

    You appear to have multiple conflicting definitions/declarations - both
    private and public are specified for the same name:

    private string Info1;
    public string Info1

    private string Info2;
    public string Info2

    private string Info2;
    public string Info2

    - Wayne

    • Marked as answer by nkuru Saturday, April 14, 2018 12:35 PM
    Wednesday, April 11, 2018 10:17 PM
  • Hi Frankie,
    I added the
    "using System.Xml.Serialization;" but nothing happened. The issue stills exists.

    About the "Name" Property change, I already had TextboxInfo1, TextboxInfo2, TextboxInfo3
    ...seems it wasn't that the problem.

    Hi nkuru,

    Please download the following demo to solve your problem:

    Project demo.

    Regards,

    Frankie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 12, 2018 1:09 AM
  • Greetings nkuru.

    It looks like SaveToXML.cs and Information.cs are not included in your project. This would explain why the classes Information and SaveToXML can't be found, and also why you are not getting the errors Wayne talked about (because Information.cs is not being built).

    On the Visual Studio menu, go to "Project->Add Existing Item" and find those two source files, then try building again. You should find your original errors go away, but the errors Wayne mentioned will appear. You can get rid of the new errors by giving the public properties and private variables different names.



    • Edited by Ante Meridian Thursday, April 12, 2018 1:30 AM Typo.
    • Marked as answer by nkuru Saturday, April 14, 2018 12:27 PM
    • Unmarked as answer by nkuru Saturday, April 14, 2018 12:27 PM
    • Marked as answer by nkuru Saturday, April 14, 2018 12:35 PM
    Thursday, April 12, 2018 1:28 AM
  • Thanks guys for the solution!

    I corrected the doubled variable names and definitions and I also included the 2 source files to the project. After those corrections, I fought with some other errors but now debugging shows 0 errors!

    The only problem left is, when I run the application, the form produces the results, doubled!

    <?xml version="1.0" encoding="utf-8"?>
    <Information xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Info1>Deep Purple</Info1>
      <Info2>Iron Maiden</Info2>
      <Info3>U2</Info3>
      <Infos1>Deep Purple</Infos1>
      <Infos2>Iron Maiden</Infos2>
      <Infos3>U2</Infos3>
    </Information>

    And here follows my "Designer" class if it helps:

    namespace Zeta_Software___WindowsFormsApp
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.TextboxInfo1 = new System.Windows.Forms.TextBox();
                this.TextboxInfo2 = new System.Windows.Forms.TextBox();
                this.TextboxInfo3 = new System.Windows.Forms.TextBox();
                this.ButtonCreate = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // TextboxInfo1
                // 
                this.TextboxInfo1.Location = new System.Drawing.Point(215, 128);
                this.TextboxInfo1.Name = "TextboxInfo1";
                this.TextboxInfo1.Size = new System.Drawing.Size(277, 20);
                this.TextboxInfo1.TabIndex = 0;
                this.TextboxInfo1.TextChanged += new System.EventHandler(this.TextboxInfo1_TextChanged);
                // 
                // TextboxInfo2
                // 
                this.TextboxInfo2.Location = new System.Drawing.Point(215, 179);
                this.TextboxInfo2.Name = "TextboxInfo2";
                this.TextboxInfo2.Size = new System.Drawing.Size(277, 20);
                this.TextboxInfo2.TabIndex = 1;
                // 
                // TextboxInfo3
                // 
                this.TextboxInfo3.Location = new System.Drawing.Point(215, 228);
                this.TextboxInfo3.Name = "TextboxInfo3";
                this.TextboxInfo3.Size = new System.Drawing.Size(277, 20);
                this.TextboxInfo3.TabIndex = 2;
                // 
                // ButtonCreate
                // 
                this.ButtonCreate.Location = new System.Drawing.Point(314, 283);
                this.ButtonCreate.Name = "ButtonCreate";
                this.ButtonCreate.Size = new System.Drawing.Size(75, 23);
                this.ButtonCreate.TabIndex = 3;
                this.ButtonCreate.Text = "Speichern";
                this.ButtonCreate.UseVisualStyleBackColor = true;
                this.ButtonCreate.Click += new System.EventHandler(this.ButtonCreate_Click);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(800, 451);
                this.Controls.Add(this.ButtonCreate);
                this.Controls.Add(this.TextboxInfo3);
                this.Controls.Add(this.TextboxInfo2);
                this.Controls.Add(this.TextboxInfo1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.TextBox TextboxInfo1;
            private System.Windows.Forms.TextBox TextboxInfo2;
            private System.Windows.Forms.TextBox TextboxInfo3;
            private System.Windows.Forms.Button ButtonCreate;
        }
    }
    
    

    Saturday, April 14, 2018 12:35 PM
  • Solved. I have deleted the getters in "Information" class.

    Thanks again guys!

    Saturday, April 14, 2018 7:54 PM