Solved

enum in C#

Posted on 2007-11-27
5
1,634 Views
Last Modified: 2008-02-01
This works in VB.Net but cant figure out what's wrong in C# with this. (new to C#).

1. I have a class like this in my DAL :
namespace Uni.DIAL.IDAL.Common {
 public sealed class DBTools
    {
       private static DataProvider _DBType;
        public static DataProvider DBType
        {
            get { return _DBType; }
            set { _DBType = value; }
        }
   
    }
}

2. In my Presenation layer I have:
class CheckPCALL
    {
        public enum DataProvider
        {
            SqlServer, OleDb, Odbc, IBM
        }
        static void Main(string[] args)
        {
           //Console.Write("Hello");
                Uni.DIAL.IDAL.Common.DBTools.DBType =  DataProvider.SqlServer;
                      }
    } //end of class

*** I get the Cannot implicitly convert type 'Uni.DIAL.DispositionNumber.CheckPCALL.DataProvider' to 'DataProvider'. An explicit conversion exists (are you missing a cast?)

*** I found http://www.csharp-station.com/Tutorials/Lesson17.aspx and added an int to it but no.

How can I fix this??
0
Comment
Question by:Camillia
5 Comments
 
LVL 7

Assisted Solution

by:Maverick_Cool
Maverick_Cool earned 100 total points
ID: 20359754
Uni.DIAL.IDAL.Common.DBTools.DBType = (Uni.DIAL.IDAL.Common.DBTools.DBType) DataProvider.SqlServer;


You need convert c# doesnot accept as variant as in vb or asp

May be you define custom enum  if required
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 400 total points
ID: 20359782
you have define DataProvider enum twice, somewhere.
0
 
LVL 7

Author Comment

by:Camillia
ID: 20359939
eh, i did have it defined somewhere else. I was copying from VB.Net and pasting to C#. Got an error , and copied the enum somewhere else and forgot to take that one out...

but is Maverick correct? I have "Uni.DIAL.IDAL" at the top of the class...so maybe that's why DataProvider.SQLServer works without specifying the class?
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20360228
If the type of the DBType property is an enum, you should use one of the items from that enum.

If you create your own enum and cast to another enum, the value will be converted directly without matching the names of the enums. If the items of the enums are not specified in the exact same order, you will get the wrong value. Also, if the definition of either enum changes so that they don't match any more, you will not get any warning when compiling.
0
 
LVL 7

Author Comment

by:Camillia
ID: 20360233
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now