CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2004
    Posts
    106

    calling static methods

    i've the following code and I want to call in the first function the ParserHelper method without instantiating an object (Form1 f=new Form1() ), how to do that?? The declaration of ParserHelper is as follows:
    public String ParserHelper (String fileName, String num, String headers)
    I tried making it static and then calling it but it gave me errors. So how shud i do it correctly??

    Code:
    namespace HL7Convert
    {
    	/// <summary>
    	/// Summary description for Converter.
    	/// </summary>
    	
    	class HL7Convert
    	{
    		
    		class HL7Viewer
    		{
    		
    			public String Parser(String fName, String fNum, String header)
    			{
    				Form1 f=new Form1();
    				return (f.ParserHelper(fName,fNum, header));
    			}
    		}
    
    		public  String HL7 (String fileName, String fileNumber, String headers)
    		{
    			HL7Viewer c = new HL7Viewer();
    			return c.Parser(fileName, fileNumber, headers);
    		}
    
    		
    		public static void Main() 
    		{
    			String text;
    			HL7Convert ct= new HL7Convert();
    			text=ct.HL7("test.txt","324", "st tt ");
    			Console.WriteLine(text);
    			Console.ReadLine();
    			
    		}
    	}
    		
    	
    }

  2. #2
    Join Date
    Jun 2005
    Location
    Maryland,USA
    Posts
    20

    Re: calling static methods

    you said your declaration is as follows:

    public String ParserHelper (String fileName, String num, String headers)

    you didnt specify the static keyword.

    it should be

    public static String ParserHelper (String fileName, String num, String headers)

    hope this helps.

    CafeDreamz.

  3. #3
    Join Date
    Jun 2005
    Location
    Maryland,USA
    Posts
    20

    Re: calling static methods

    hey,also make sure the Parserhelper method is not accessing any non-static instance members of the class to which it belongs.

  4. #4
    Join Date
    May 2004
    Posts
    106

    Re: calling static methods

    yea i had tried by putting static, cuz thts necessary if i dont want to make an object to call the function, but when i call in this HL7Convert class it says it doesn't belong to name space and stuff, although i've created a dll of the class where the ParserHelper and included here:
    Code:
    using System;
    using WindowsApplication1;
    Also, ParserHelper was calling other function too, and i made that static too. But nothing worked

  5. #5
    Join Date
    May 2004
    Posts
    106

    Re: calling static methods

    ok i figured my prob, i wasn't calling it rite..its supposed to be like this:
    return (Form1.ParserHelper(fName,fNum, header));


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

Featured