CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Apr 2011
    Posts
    27

    Talking Working with multiple .cs documents, passing data back and forth

    [.Net 4.0]

    Hello,

    I have multiple .cs documents in the same project. The first project I was able to pass data back and forth by creating a public string function. But for some reason in my new one I am missing something. I using the same namespace name for all .cs documents.

    In MainWindow.xaml.cs I have this function:

    public string whatis_SQL()
    {
    return SQLServerName.Text;
    }


    In Backup.xaml.cs when I try to run the following it does not recognize whatis_SQL.

    string selectedSQLServer = whatis_SQL();


    Thanks!
    Last edited by DeepThought; April 27th, 2011 at 12:17 PM.

  2. #2
    Join Date
    Dec 2008
    Posts
    144

    Re: Working with multiple .cs documents, passing data back and forth

    A .cs document is actually a class file. I can't really give you much help without knowing what you really mean by "passing data back and forth."

    Moving data between classes is very easy- you send the data to a method within the desired class as a parameter and you send it back by having the same method use a non-void return type. Consumer code will need an appropriate variable to store the value returned from the method.

    Search google for class tutorials for C# or look into the book Head First C#.
    Code:
    if (Issue.Resolved)
    {
         ThreadTools.Click();
         MarkThreadResolved();
    }

  3. #3
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Working with multiple .cs documents, passing data back and forth

    Yes. From time to time, I see people still learning C# or another language talk about the logical structure of their code in terms of files - the documents they write their code in, as you've called them. The .cs files are just text files that store your code - they aren't that remarkable or important by themselves. What defines your program's structure aren't the files (they are just a way of organizing what you've written), but the code itself. The code defines classes, which in turn define what objects will be interacting in your program. In C#, you don't talk about passing data between .cs files - you talk about passing data between classes. (To be more precise - mostly between objects that are instances of those classes; but we usually aren't that precise because it's often obvious from the context.)
    You can have more than 1 class defined in a .cs file. C# even makes it possible to have a single class defined in multiple files (for example: Form1.cs (the one you work with) and Form1.designer.cs (reserved for the IDE)).
    So - it is the system that is defined by your code that matters the most, not the files themselves.

    As for your problem, I'm guessing that you are trying to call a method of one class from a different class. To do that, the other class needs to have some sort of a reference to the object of the first class (it can either store it, or get it as an argument to one of it's own methods).
    But, CreganTur's right, you need to post some more code so we can see what have you actually done. It would also be a good idea to read up a bit - find some basic stuff about classes, either in some online tutorials or in an introductory C# book.
    Last edited by TheGreatCthulhu; April 27th, 2011 at 04:23 PM.

  4. #4
    Join Date
    Apr 2011
    Posts
    27

    Re: Working with multiple .cs documents, passing data back and forth

    Ok I did a little research on how to work with classes and it is sort of working out. Each .cs file has a class. One class is called MainWindow, another is called Backup.

    From MainWindow I can access Backup with

    Backup BackupWin;

    BackupWin = new Backup();

    BackupWin.somepublicfunction();

    I see that I can send data by making a string like this in Backup.

    string strTemp;

    public string strSomeVariable
    {
    get
    {
    strTemp = value;
    }
    }

    and I can now set this somevariable just to pass my data from one class to another.


    This all seems like a lot of extra code and harder to follow the logic than when I kept all my functions in one class.

    I did not mean to end up with multiple .cs files. What happened was I wanted different parts of my interface into different windows and so I ended up creating new .xaml files and each file came with it's own .cs file. Hopefully I am going down the right path.

  5. #5
    Join Date
    Dec 2008
    Posts
    144

    Re: Working with multiple .cs documents, passing data back and forth

    In the future please post code within code tags- makes it easier to read.

    You can define and instantiate variables in a single line:
    Code:
    Backup BackupWin = new Backup();
    Good class design can take longer to code than procedural code, but it is much easier to maintain and makes a lot more sense logically.

    You can have multiple classes in the same .cs file, but a lot of beginners use 1 file per class until they get the hang of it. It can also just help you keep things organized better.

    To really blow your mind you can also have 1 class spread across multiple .cs files- these are called Partial classes. As a rough description Partial classes allow you to separate a class across multiple files and chunks and it is compiled back together at runtime.
    Code:
    if (Issue.Resolved)
    {
         ThreadTools.Click();
         MarkThreadResolved();
    }

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Working with multiple .cs documents, passing data back and forth

    Quote Originally Posted by DeepThought View Post
    Ok I did a little research on how to work with classes and it is sort of working out. Each .cs file has a class.
    My advice to you as a beginner is to forget thinking about this in terms of cs files. It is completely irrelevent where an entity resides (i.e which cs file it lives in). The reason being is once the code it compiled it all sits in the same assembly (or multiple assemblies).

    Read TheGreatCthulhu's post again - he's right on in terms of how to think of this.

  7. #7
    Join Date
    Apr 2011
    Posts
    27

    Re: Working with multiple .cs documents, passing data back and forth

    Quote Originally Posted by Arjay View Post
    My advice to you as a beginner is to forget thinking about this in terms of cs files. It is completely irrelevent where an entity resides (i.e which cs file it lives in). The reason being is once the code it compiled it all sits in the same assembly (or multiple assemblies).

    Read TheGreatCthulhu's post again - he's right on in terms of how to think of this.
    In my .xaml I have a reference to a Class such as

    <Windows x:Class="SQLGuide2.MainWindow"

    I noticed that this only gives MainWindow access to the elements in this .xaml file.

    Is it ok to reference Multiple classes so that other classes can modify UI elements in the main window?

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Working with multiple .cs documents, passing data back and forth

    Quote Originally Posted by DeepThought View Post
    Is it ok to reference Multiple classes so that other classes can modify UI elements in the main window?
    No. In general, tight coupling between different UI components should be avoided.

  9. #9
    Join Date
    Apr 2011
    Posts
    27

    Re: Working with multiple .cs documents, passing data back and forth

    Ok, so it helps me keep track of what is what by using different cs files with the same name as the UI file.

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Working with multiple .cs documents, passing data back and forth

    Quote Originally Posted by DeepThought View Post
    Ok, so it helps me keep track of what is what by using different cs files with the same name as the UI file.
    Great for you, but irrelevant in terms of compiling and code execution. Use it if it helps you keep track of things, but don't confuse not being able to access one class from another because the classes are in different files.

  11. #11
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Working with multiple .cs documents, passing data back and forth

    Yeah. You can have 1 class defined over 2 or more files (there's a special keyword that allows you to do that), and you would still be able to access all the class fields (member variables) normally, without resorting to anything special.

    So - organize your code throughout the files as you see fit and convenient, but keep in mind that the .cs files themselves have absolutely nothing to do with your program's structure. What's written in them does.

  12. #12
    Join Date
    Apr 2011
    Posts
    27

    Re: Working with multiple .cs documents, passing data back and forth

    Got it, thanks!

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