Click here to Skip to main content
15,891,841 members
Please Sign up or sign in to vote.
2.94/5 (3 votes)
See more:
Error Expected class, delegate, enum, interface, or struct,i am unable to detect what can be issues.

[EDIT] Code added from comment:
C#
////code with error///
public class Class1
    {
        private CardType _cardTypes;

     }
        public bool IsValidCardType(string cardNumber)
        {
            // AMEX -- 34 or 37 -- 15 length
            if ((Regex.IsMatch(cardNumber, "^(34|37)")) && ((_cardTypes & CardType.Amex) != 0))
                return (15 == cardNumber.Length);


                // MasterCard -- 51 through 55 -- 16 length
            else if ((Regex.IsMatch(cardNumber, "^(51|52|53|54|55)")) && ((_cardTypes & CardType.MasterCard) != 0))
                return (16 == cardNumber.Length);

                // VISA -- 4 -- 13 and 16 length
            else if ((Regex.IsMatch(cardNumber, "^(4)")) && ((_cardTypes & CardType.VISA) != 0))
                return (13 == cardNumber.Length || 16 == cardNumber.Length);

                // Diners Club -- 300-305, 36 or 38 -- 14 length
            else if ((Regex.IsMatch(cardNumber, "^(300|301|302|303|304|305|36|38)")) && ((_cardTypes & CardType.DinersClub) != 0))
                return (14 == cardNumber.Length);

                // enRoute -- 2014,2149 -- 15 length
            else if ((Regex.IsMatch(cardNumber, "^(2014|2149)")) && ((_cardTypes & CardType.DinersClub) != 0))
                return (15 == cardNumber.Length);

                // Discover -- 6011 -- 16 length
            else if ((Regex.IsMatch(cardNumber, "^(6011)")) && ((_cardTypes & CardType.Discover) != 0))
                return (16 == cardNumber.Length);

                // JCB -- 3 -- 16 length
            else if ((Regex.IsMatch(cardNumber, "^(3)")) && ((_cardTypes & CardType.JCB) != 0))
                return (16 == cardNumber.Length);

                // JCB -- 2131, 1800 -- 15 length
            else if ((Regex.IsMatch(cardNumber, "^(2131|1800)")) && ((_cardTypes & CardType.JCB) != 0))
                return (15 == cardNumber.Length);
            else
            {
                // Card type wasn't recognised, provided Unknown is in the CardTypes property, then
                // return true, otherwise return false.
                if ((_cardTypes & CardType.Unknown) != 0)
                    return true;
                else
                    return false;
            }
        }
Posted
Updated 30-Oct-17 2:13am
v2
Comments
Wendelius 5-Sep-11 8:50am    
Could you post the code giving the error...
raj_sagoo 5-Sep-11 9:23am    
code moved to the question

Expected class, delegate, enum, interface, or struct

You didn't declare of of these elements in your namespace.
I suggest you get a book on C#/ASP.NET basics.
 
Share this answer
 
Comments
[no name] 5-Sep-11 9:03am    
I agreed with phil :)
It could literally be anything. A missing semi-colon, a missing assembly reference, incorrect syntax, and other things. Not being able to see your code makes it impossible to provide more specific assistance.
 
Share this answer
 
The most likely cause of this message is that you have written code that should be within a class, delegate, enum, interface or struct directly into the namespace scope. This is often caused by an extra a closing brace ('}') causing your scoping to go a bit screwy

take the following:

C#
namespace xxxx
{
    public class yyyy
    {
       public method zzzz()
       {
          //blah
       }

       public method qqqqq()
       {
       }
    }
}


if i were to add an extra } :

C#
namespace xxxx
{
    public class yyyy
    {
       public method zzzz()
       {
          //blah
       } // this shouldnt be here
       }//this becomes the closing brace of the class rather than the method

       public method qqqqq() // this method is now outside the class!!
       {
       }
    }
}
 
Share this answer
 
Comments
raj_sagoo 5-Sep-11 9:12am    
yes it works.
GParkings 5-Sep-11 9:14am    
you would not believe how many times that one has caught me out :)
raj_sagoo 5-Sep-11 9:18am    
Bingo atlast,it worked..:)
johannesnestler 5-Sep-11 11:23am    
... since VS has perfect code formatting support, I try to write the code in the "correct order", if formatting goes wrong I know I missed something. (Or if the VS shortcut for formatting doesn't work [STRG+K then STRG+D])
Dalek Dave 17-Oct-11 3:26am    
Good Call.
C#
<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Security;
using System.Data;

namespace testing{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            string username = TextBox1.Text;
            string password = TextBox2.Text;
            SqlConnection connection = null;
            SqlCommand command = null;
            SqlDataReader dataReader = null;
            try
            {
                string connectionString = ConfigurationManager.ConnectionStrings["testing ConnectionString"].ConnectionString;
                connection = new SqlConnection(connectionString);
                connection.Open();
                //prepare sql statements
                string sql = "SELECT * from Staff where username='" + username + "'And Password='" + password + "'";
                command = new SqlCommand(sql, connection);
                command.Parameters.Add("@username", SqlDbType.VarChar, 15).Value = username;
                command.Parameters.Add("@password", SqlDbType.VarChar, 30).Value = password;

                dataReader = command.ExecuteReader();

                if (dataReader.Read())
                {
                    //save username in session
                    Session.Add("username", username);
                }
                else
                {
                    //audit failure and notify user
                    System.Diagnostics.EventLog.WriteEntry(
                        "Membership Registration"
                        , string.Format("Invalid login attempt for user {0}", username)
                        , System.Diagnostics.EventLogEntryType.FailureAudit);
                Response.Write("Invalid username");
            }
            dataReader.Close();
            connection.Close();
            
        }
        catch(Exception ex)
    {
        System.Diagnostics.EventLog.WriteEntry (
        "Membership Registration"
        , string.Format("Unexpected error: {0}", ex.Message)
        ,System.Diagnostics.EventLogEntryType.Error);

        Response.Write ("Unexpected error");
    }
        
        
            }
      
            //Response.Redirect("./default.aspx");
        }


        protected void Button2_Click(object sender, EventArgs e)        {
            Response.Redirect("Membership Registration.aspx");
        }
    }


I also having this error at void Button2_Click. Can someone tell me my error?
 
Share this answer
 
Comments
GParkings 14-Feb-12 5:48am    
its the extra '}' before or after //Response.Redirect("./default.aspx");
Heyy Try this:

public class Class1
{
private CardType _cardTypes;


public bool IsValidCardType(string cardNumber)
{
// AMEX -- 34 or 37 -- 15 length
if ((Regex.IsMatch(cardNumber, "^(34|37)")) && ((_cardTypes & CardType.Amex) != 0))
return (15 == cardNumber.Length);


// MasterCard -- 51 through 55 -- 16 length
else if ((Regex.IsMatch(cardNumber, "^(51|52|53|54|55)")) && ((_cardTypes & CardType.MasterCard) != 0))
return (16 == cardNumber.Length);

// VISA -- 4 -- 13 and 16 length
else if ((Regex.IsMatch(cardNumber, "^(4)")) && ((_cardTypes & CardType.VISA) != 0))
return (13 == cardNumber.Length || 16 == cardNumber.Length);

// Diners Club -- 300-305, 36 or 38 -- 14 length
else if ((Regex.IsMatch(cardNumber, "^(300|301|302|303|304|305|36|38)")) && ((_cardTypes & CardType.DinersClub) != 0))
return (14 == cardNumber.Length);

// enRoute -- 2014,2149 -- 15 length
else if ((Regex.IsMatch(cardNumber, "^(2014|2149)")) && ((_cardTypes & CardType.DinersClub) != 0))
return (15 == cardNumber.Length);

// Discover -- 6011 -- 16 length
else if ((Regex.IsMatch(cardNumber, "^(6011)")) && ((_cardTypes & CardType.Discover) != 0))
return (16 == cardNumber.Length);

// JCB -- 3 -- 16 length
else if ((Regex.IsMatch(cardNumber, "^(3)")) && ((_cardTypes & CardType.JCB) != 0))
return (16 == cardNumber.Length);

// JCB -- 2131, 1800 -- 15 length
else if ((Regex.IsMatch(cardNumber, "^(2131|1800)")) && ((_cardTypes & CardType.JCB) != 0))
return (15 == cardNumber.Length);
else
{
// Card type wasn't recognised, provided Unknown is in the CardTypes property, then
// return true, otherwise return false.
if ((_cardTypes & CardType.Unknown) != 0)
return true;
else
return false;
}
}
}
 
Share this answer
 
Comments
Philippe Mori 3-Jan-17 9:52am    
Format your code... if you want to have some points.
using System;

interface Drawable
{
void draw1();
}

interface Rectangle
{
void draw2();
}

//interface Circle
//{
// void draw3();
//}

public class Result : Drawable, Rectangle//, Circle
{
public void draw1()
{
Console.WriteLine("drawing Drawable...");
}
}



// ERROR : Error 1 Expected class, delegate, enum, interface, or struct

public void draw2();
{
Console.WriteLine("drawing Rectangle...");
}




//public void draw3()
//{
// Console.WriteLine("drawing circle...");
//}




public class TestInterface
{
public static void Main()
{
Drawable kmp1 = new Result();
Rectangle kmp2 = new Result();
//Circle Kmp3 = new Result();

kmp1.draw1();
kmp2.draw2();
}
}
 
Share this answer
 
Comments
CHill60 30-Oct-17 8:58am    
This is not a solution to the 6 year old question!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900