Results 1 to 7 of 7

Thread: dont understand this error

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Resolved dont understand this error

    im writing a c# console app and have this code:
    VB Code:
    1. using System;
    2.  
    3. namespace Anonymous_Mail_CS
    4. {
    5.     /// <summary>
    6.     /// Send eMail Anonymousely
    7.     /// using any smtp server.
    8.     /// </summary>
    9.     class Anonymous_Mail_CS
    10.     {
    11.  
    12.             System.Web.Mail.MailMessage myMessage = new System.Web.Mail.MailMessage();
    13.  
    14.         static void Main()
    15.         {
    16.  
    17.             Console.Write("Specify SMTP Server: ");
    18.             System.Web.Mail.SmtpMail.SmtpServer = Console.ReadLine();
    19.  
    20.             Console.Write("Specify 'To': ");
    21.             myMessage.To = Console.ReadLine();
    22.  
    23.             Console.Write("Specify 'From': ");
    24.             myMessage.From = Console.ReadLine();
    25.  
    26.             Console.Write("Specify 'Subject': ");
    27.             myMessage.Subject = Console.ReadLine();
    28.  
    29.             Console.WriteLine("Write your message:");
    30.             myMessage.Body =  Console.ReadLine();
    31.            
    32.            
    33.         }
    34.    
    35.     }//end class Anonymous_Mail_CS
    36. }//end namespace Anonymous_Mail_CS

    here is the series of errors when trying to build the project:
    c:\Visual Studio Projects\Anonymous Mail CS\Class1.cs(21): 'Anonymous_Mail_CS.Anonymous_Mail_CS.myMessage' denotes a 'field' where a 'class' was expected
    c:\Visual Studio Projects\Anonymous Mail CS\Class1.cs(24): 'Anonymous_Mail_CS.Anonymous_Mail_CS.myMessage' denotes a 'field' where a 'class' was expected
    c:\Visual Studio Projects\Anonymous Mail CS\Class1.cs(27): 'Anonymous_Mail_CS.Anonymous_Mail_CS.myMessage' denotes a 'field' where a 'class' was expected
    c:\Visual Studio Projects\Anonymous Mail CS\Class1.cs(30): 'Anonymous_Mail_CS.Anonymous_Mail_CS.myMessage' denotes a 'field' where a 'class' was expected


    can i NOT declare a class level variable of MailMessage type?

    also, Intellisense doesn't recognize the variable when it's declared where it currently is.

    what am i overlooking here?

    another thing i'd like to find out is why i have to use a static member of smtpMail (smtpserver) when in visual basic.net, i can use an instance of the smtpMail class? can someone tell me what the difference is between the languages here?
    Last edited by Andy; Apr 5th, 2005 at 08:36 PM.

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: dont understand this error

    Quote Originally Posted by Andy
    im writing a c# console app and have this code:
    VB Code:
    1. using System;
    2.  
    3. namespace Anonymous_Mail_CS
    4. {
    5.     /// <summary>
    6.     /// Send eMail Anonymousely
    7.     /// using any smtp server.
    8.     /// </summary>
    9.     class Anonymous_Mail_CS
    10.     {
    11.  
    12.             System.Web.Mail.MailMessage myMessage = new System.Web.Mail.MailMessage();
    13.  
    14.         static void Main()
    15.         {
    16.  
    17.             Console.Write("Specify SMTP Server: ");
    18.             System.Web.Mail.SmtpMail.SmtpServer = Console.ReadLine();
    19.  
    20.             Console.Write("Specify 'To': ");
    21.             myMessage.To = Console.ReadLine();
    22.  
    23.             Console.Write("Specify 'From': ");
    24.             myMessage.From = Console.ReadLine();
    25.  
    26.             Console.Write("Specify 'Subject': ");
    27.             myMessage.Subject = Console.ReadLine();
    28.  
    29.             Console.WriteLine("Write your message:");
    30.             myMessage.Body =  Console.ReadLine();
    31.            
    32.            
    33.         }
    34.    
    35.     }//end class Anonymous_Mail_CS
    36. }//end namespace Anonymous_Mail_CS

    here is the series of errors when trying to build the project:
    c:\Visual Studio Projects\Anonymous Mail CS\Class1.cs(21): 'Anonymous_Mail_CS.Anonymous_Mail_CS.myMessage' denotes a 'field' where a 'class' was expected
    c:\Visual Studio Projects\Anonymous Mail CS\Class1.cs(24): 'Anonymous_Mail_CS.Anonymous_Mail_CS.myMessage' denotes a 'field' where a 'class' was expected
    c:\Visual Studio Projects\Anonymous Mail CS\Class1.cs(27): 'Anonymous_Mail_CS.Anonymous_Mail_CS.myMessage' denotes a 'field' where a 'class' was expected
    c:\Visual Studio Projects\Anonymous Mail CS\Class1.cs(30): 'Anonymous_Mail_CS.Anonymous_Mail_CS.myMessage' denotes a 'field' where a 'class' was expected


    can i NOT declare a class level variable of MailMessage type?

    also, Intellisense doesn't recognize the variable when it's declared where it currently is.

    what am i overlooking here?

    another thing i'd like to find out is why i have to use a static member of smtpMail (smtpserver) when in visual basic.net, i can use an instance of the smtpMail class? can someone tell me what the difference is between the languages here?
    hmm that's all the code? the errors seem to be from lines 21, 24, 27, 30... anything on those lines?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: dont understand this error

    do you need using System.Web.Mail; after using System;

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Mark the vari "myMessage" as 'static' .

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Re: dont understand this error

    That sure worked! Thanks. If you use a class level variable anywhere in a console app, you need to mark it as STATIC?

  6. #6
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: dont understand this error

    Quote Originally Posted by Andy
    That sure worked! Thanks. If you use a class level variable anywhere in a console app, you need to mark it as STATIC?
    Nope, you're actually using the console application start up incorrectly. You should be using something along the lines of:

    Code:
    public class ConsoleApp
    {
    	/// <summary>Fires when the console application is started</sumary>
    	public static void main(string[] args)
    	{
    		ConsoleApp myConsole = new ConsoleApp();
    		myConsole.Foo();
    	}
    
    	private int classLevelVariable = 0;
    
    	/// <summary>Default constructor</summary>
    	public ConsoleApp()
    	{
    	}
    
    	/// <summary>The actual console process</summary>
    	public void Foo()
    	{
    		Console.WriteLine("Value = " + classLevelVariable.ToString());
    	}
    }

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Quote Originally Posted by Andy
    That sure worked! Thanks. If you use a class level variable anywhere in a console app, you need to mark it as STATIC?
    It depends on the logic and the functionality you're making . It requires more OOP read . To sum up, variables accessibility and visiblity (info hiding) :
    if you want to reference a non-static variable in static method , then you have to have object reference of that class first .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width