Click here to Skip to main content
12,953,937 members (44,877 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Error Expected class, delegate, enum, interface, or struct,i am unable to detect what can be issues.

[EDIT] Code added from comment:
////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 5-Sep-11 2:48am
Updated 2-Jan-17 19:54pm
Wendelius349.1K
v2
Comments
Mika 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Md. Marufuzzaman 5-Sep-11 9:03am
   
I agreed with phil :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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:

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


if i were to add an extra } :

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!!
       {
       }
    }
}
  Permalink  
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.
Prem Kumar M 23-Jul-15 1:12am
   
Thanks
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

<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?
  Permalink  
Comments
GParkings 14-Feb-12 5:48am
   
its the extra '}' before or after //Response.Redirect("./default.aspx");
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

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;
}
}
}
  Permalink  
Comments
Philippe Mori 3-Jan-17 9:52am
   
Format your code... if you want to have some points.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,464
CHill60 3,490
Maciej Los 3,123
ppolymorphe 2,020
Jochen Arndt 1,975


Advertise | Privacy | Mobile
Web01 | 2.8.170525.1 | Last Updated 3 Jan 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100