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

    setting a var problem

    Hello!

    I am really new to this and was wondering if somebody could have a look at this. What I'm trying to do is get the selected folder value put into my dirlocation var.
    Code:
       private void dirLocation_Click(object sender, EventArgs e)
            {
      
                FolderBrowserDialog fdb = new FolderBrowserDialog();
                fdb.Description ="Please choose the directory your .rlt files are located in";
                if (fdb.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    dirLocation = fdb.SelectedPath;
                }
    Is this how you do it? I keep getting an error like Cannot implicitly convert type 'type' to 'type'

    Thanks in advance guys

    Cheers!
    Stephen

  2. #2
    Join Date
    Nov 2012
    Posts
    12

    Re: setting a var problem

    It seems that this works

    Code:
    
                FolderBrowserDialog fdb = new FolderBrowserDialog();
                fdb.Description ="Please choose the directory your .rlt files are located in";
                if (fdb.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string dirLocation = fdb.SelectedPath;
                    
                }
    Even though in I thought I had it declared in another .cs file, oh the joys of being new

  3. #3
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: setting a var problem

    Stevish,
    That has to do with the scope of the variable you are using. You might have declared it in a different .cs file, but the location this function is in, does not have access to that variable.

    In fact, the code the you have posted above, as soon as you fall out of the IF statement, your dirLocation variable will go out of scope and you will no longer be able to access it.

  4. #4
    Join Date
    Nov 2012
    Posts
    12

    Re: setting a var problem

    Ok so should I do something like this.

    in my other .cs file I have a class called instronProgram that has this line of code in it.

    Code:
    public string dirLocation { get; set; }
    and I changed my other code to look like this.

    Code:
        FolderBrowserDialog fdb = new FolderBrowserDialog();
                fdb.Description ="Please choose the directory your .rlt files are located in";
                if (fdb.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    instronProgram floc = new instronProgram();
                    floc.dirLocation = fdb.SelectedPath;
                    
                    
                }
    Now is that the way I should be setting the dirLocation var?

    Thanks again for your help.

    Stevish

  5. #5
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: setting a var problem

    If you initialize your instronProgram class there, once again, this is a NEW instance of the class, and will go out of scope and be destroyed in this particular method. If this is going to be used throughout your program, you should look at making it static. If it is just used in this method, just declare the var/class at the top of the method. So you need to think about when this will be used, and how long you need access to them.

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

    Re: setting a var problem

    Don't think of this as "having to declare it in a different cs file". Instead think along the lines of the scope of the variable. Remember, with the 'partial' keyword, the same class declaration can span multiple cs files.

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

    Re: setting a var problem

    Yeah, think in terms of classes - the *.cs files are there simply to provide a physical way of organizing the code. Scope is not that hard to understand. Generally speaking, it is pretty much defined by the { and }. These confine the scope of a variable to a certain space. You can think of class scope as a top level box, containing various variables and other members. Methods of a class define their own scope, within the top-level scope of the class - a box within a box. But these boxes are of a special kind - you can only see outside (from within), but not inside (if you're looking from outside - maybe it's too dark inside or something ). So, a class-level variable is visible from a function, but a local function variable is not visible to other parts of the class, let alone the outside world.

    Another aspect of scope is the temporal one; not only it can refer to constraints within a space, but also during time; local function variables (or local variables of a block, like the body of an if statement) exist in memory only during the execution of the function (block), and class member variables exist during the lifetime of a particular object of that class.

    The bottom line is - declare the variable somewhere where it will last - make it a member field of a class, and it will be visible everywhere from within the class, living as long as the object of the class exists (if it's a Form object, it will stay alive until you close it).
    Last edited by TheGreatCthulhu; November 8th, 2012 at 09:27 PM.

  8. #8
    Join Date
    Nov 2012
    Posts
    12

    Re: setting a var problem

    OK I was wondering if you could maybe point me to a page that will walk me through making a whole forms program.
    What I'm trying to do if let a user picks type a folder location and then pick a type of item from a drop down box.
    after doing that the program would open all the file that are named the same at the type picked and read the
    16th line. The 16th line would be split by comma's and I would grab one value out of each array and find
    the average of it.

    Thanks any pointing in the right direction would be great, I could do this in PHP easy enough but having
    trouble getting my head around it in C# from some reason.

    Thanks in advance !!!

    Cheers!

    Stephen

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

    Re: setting a var problem

    OK, here are some tutorials I found that might help - but I want to go over a few things here just in case.
    You can start here:
    C# Tutorial - Your first Windows Form

    and then skip a few chapters to continue with this:
    Add Menus to Windows Forms in C#

    But, you should definitely also learn about classes and objects, since it's the lack of understanding of these that's causing your problem, not Windows Forms.

    (here's the whole series)


    Since you've said you're familiar with PHP, let's exploit that:
    PHP Code:
    <?php
    $a 
    0;   // Global variable

    function SomeFunction()
    {
        
    // PHP requires you to make the global variables visible from the func.
        // In C# (and languages like C++ or Java) you don't have to do this.
        
    global $a;      

        
    $b 3;  // a local var

        
    $a $b;


    Sum();
    echo 
    $a;
    // $b is not visible from here (it's local to SomeFunction - so it doesn't "exist" at this point)
    ?>
    Now, in C#, everything is within a class.
    Code:
    public class ExampleClass
    {
        private int a = 0;   // class-level member variable
    
        public void SomeFunction()
        {
            int b = 3;   // local variable
    
            // a is visible from here
            a = 2 * b;
        }
    
        // let's provide a property so that the val of a can be retrieved
        public int Value 
        { 
            get 
            {
                // b is not visible from here, but a is
                return a; 
            } 
        }
    }
    
    // Let's assume this is a console app
    static class Program
    {
        [STAThread]
        public static void Main()
        {
            ExampleClass example = new ExampleClass();  // create an instance (object) of ExampleClass
    
            // Note: all the variables declared in the ExampleCass are instance variables, which means
            // that each object get's it's own set, and they must be accessed via an object
            
            example.SomeFunction();  // call SomeFunction() on that instance
    
            Console.WriteLine(example.Value);  // prints out "6" (since Value gets example.a)
        }
    }
    Note that none of the variables here is truly global - the use of global variables should be reduced to a minimum. As a side note, a global variable can be modeled by a static class member, but that's a different topic.

    What's important here is for you to realize that Forms work exactly the same way: Form is just a class (like ExampleClass above) that is created inside the Main() method, and used from it (if you open Program.cs you'll see that).

    So here's what's the problem with your code:
    Code:
    FolderBrowserDialog fdb = new FolderBrowserDialog();
                fdb.Description ="Please choose the directory your .rlt files are located in";
                if (fdb.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    instronProgram floc = new instronProgram();
                    floc.dirLocation = fdb.SelectedPath;                
                    
                }
    The floc variable is local to the body of the if-statement. Once the execution steps out of the if-statement, floc is lost.

    If you want to store the location for later use, declare a string member variable in the class containing the method that launches the folder browser dialog (is it the form class?).
    Code:
    public class NameOfYourClass
    {
        // ...
        private string dirLocation = "";  // has to be declared as a class member
    
        //...
    
        private void dirLocation_Click(object sender, EventArgs e)
        {
             FolderBrowserDialog fdb = new FolderBrowserDialog();
             fdb.Description ="Please choose the directory your .rlt files are located in";
             if (fdb.ShowDialog() == System.Windows.Forms.DialogResult.OK)
             {
                 dirLocation = fdb.SelectedPath;                
             }
        }
    }

  10. #10
    Join Date
    Nov 2012
    Posts
    12

    Re: setting a var problem

    Thank you very much in helping understand this stuff better, I look forward to reading the above links.
    You examples have really helped me a lot as well, again thank you so much and I hope to have better
    questions later on once I start building my app/program.

    Cheers!
    Stephen

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