Thread: Function from .cs file calling function from a different .cs file

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    55

    Angry Function from .cs file calling function from a different .cs file

    What I am trying to do (in brief terms):
    I have a function in a .cs file (A) that needs to access a function in a different .cs file (B). Based on certain calculations, (B) will alter (A) variables.

    The problem:
    I keep getting the following WARNINGs:
    warning CS0649: Field 'programX.MainWindow.tCodes' is never assigned to, and will always have its default value null
    warning CS0649: Field 'programX.TriggerCodes.function_MainWindow' is never assigned to, and will always have its default value null

    Possibly needed details:
    OS: Windows 7 64-bit
    Compiler: MS Visual Studio 2010 Ultimate
    Language: C# (WPF)
    Framework: 4.0

    And finally, a sample piece of code:

    In "MainWindow.xaml.cs":

    Code:
    namespace programX
    {
    	public partial class MainWindow : Window
    	{
    		public TriggerCodes tCodes;
    		
    		public MainWindow()
    		{
    			InitializeComponent();
    		}
    		
    		protected internal void doSomething()
    		{
    			if (tCodes.triggerCodeFound("Hello World"))
    				return;
    			
    			Console.WriteLine("Didn't find it");
    		}		
    		
    		public void sayHello(string message)
    		{
    			StringBuilder msg = new StringBuilder();
    			msg.Append("Hello ");
    			msg.Append(message);
    			
    			// *should* write "Hello Silly Person!"
    			Console.WriteLine(msg.ToString());
    		}
    	}
    }

    In "TriggerCodes.cs":

    Code:
    namespace programX
    {
    	public partial class TriggerCodes
    	{
    		public MainWindow function_MainWindow;
    		
    		public bool triggerCodeFound(String theData)
    		{
    			if ((theData.IndexOf("World") > (-1)))
    			{
    				this.function_MainWindow.sayHello("Silly Person!");
    				return (true);
    			}
    			return (false);
    		}
    	}
    }

    I am a C/C++ coder by trade, and just learning C#. In C/C++, this is a rather simple concept for me to grasp, however, C# is kicking my arse and laughing all the way.

    Any help to avenge this temporary nemesis would be awesome.

    Thanks!

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    41
    In line 5 of your first block of code you create an instance variable tCodes. Where is the code that assigns an object to this instance variable?
    In line 5 of your second block of code you create an instance variable function_MainWindow. Where is the code that assigns an object to this instance variable?

    This is basic OO stuff, applies to C++, C#, etc.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    55

    Question

    I think that I figured it out. Well, it *seems* to be working, although I'm not sure if I could/should have done it a better way.

    Here's what I have now:

    In "MainWindow.xaml.cs":

    Code:
    namespace programX
    {
    	public partial class MainWindow : Window
    	{
    		public MainWindow()
    		{
    			InitializeComponent();
    		}
    		
    		protected internal void doSomething()
    		{
    			triggerDerivedClass tCodes = new triggerDerivedClass();
    			
    			if (tCodes.triggerCodeFound(this, "Hello World"))
    				return;
    			
    			Console.WriteLine("Didn't find it");
    		}		
    		
    		public void sayHello(string message)
    		{
    			StringBuilder msg = new StringBuilder();
    			msg.Append("Hello ");
    			msg.Append(message);
    			
    			// *should* write "Hello Silly Person!"
    			Console.WriteLine(msg.ToString());
    		}
    	}
    }

    In "TriggerCodes.cs":

    Code:
    namespace programX
    {
    	public abstract class triggerBaseClass
    	{
    		public abstract bool triggerCodeFound(MainWindow sender, String theData);
    	}
    	public class triggerDerivedClass : triggerBaseClass
    	{		
    		public override bool triggerCodeFound(MainWindow sender, String theData)
    		{
    			if ((theData.IndexOf("World") > (-1)))
    			{
    				sender.sayHello("Silly Person!");
    				return (true);
    			}
    			return (false);
    		}
    	}
    }
    Last edited by xwielder; 05-10-2012 at 06:01 PM. Reason: left old piece of code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling a function from another file
    By cuo741 in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2010, 04:16 AM
  2. Calling a function from a different file
    By riders29 in forum C Programming
    Replies: 1
    Last Post: 04-21-2010, 06:30 AM
  3. Calling function in another file
    By cashmerelc in forum C Programming
    Replies: 10
    Last Post: 12-05-2007, 01:49 AM
  4. Calling a member function from a different file
    By cdonlan in forum C++ Programming
    Replies: 3
    Last Post: 02-01-2005, 12:50 AM
  5. calling function from separate cpp file
    By dontknow in forum C++ Programming
    Replies: 6
    Last Post: 04-06-2002, 10:42 AM