13 Replies - 5427 Views - Last Post: 23 November 2010 - 11:29 AM Rate Topic: -----

#1 Guest_Marcus*


Reputation:

The name does not exist in the current context

Posted 23 November 2010 - 05:08 AM

Hello,

I am trying to make a really simple application that takes some numbers, does some calculations and outputs an answer, but I can't seem to get it right. I understand it has something to do with scope. It's the last part of the code I get an error on. (The name 'lopm' does not exist in the current context). How can I make the different variables available outside of the "fogmassa" context so that I can show them to the user?

Any other comments on the code?

Thanks in advance.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace fogmassa
{
    public partial class fogmassa : Form
    {
        int kakellength = 1;
        int kakelwidth = 1;
        int fogwidth = 1;

        public fogmassa()
        {
            InitializeComponent();
            

            if (txtLength.Text != "")
            {
                kakellength = Convert.ToInt32(txtLength.Text);
            }

            if (txtWidth.Text != "")
            {
                kakelwidth = Convert.ToInt32(txtWidth.Text);
            }

            if (txtFogWidth.Text != "")
            {
                fogwidth = Convert.ToInt32(txtFogWidth.Text);
            }

            
            
        }

        public void function_lopmeter()
    {
        int lopm = ((1 / (kakelwidth + fogwidth)) + (1 / (kakellength + fogwidth)));
    }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void txtFogWidth_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnCalc_Click(object sender, EventArgs e)
        {
            lblLopM.Text = ("Löpmeter fog " + lopm + " m");
        }
    }
}


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 23 November 2010 - 05:19 AM


Is This A Good Question/Topic? 0

Replies To: The name does not exist in the current context

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: The name does not exist in the current context

Posted 23 November 2010 - 05:21 AM

Yes it's scope. You're creating the lopm variable inside the function_lopmeter function. How is kakellength, kakelwidth, and fogwidth visible to the rest of the class?
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Marcus*


Reputation:

Re: The name does not exist in the current context

Posted 23 November 2010 - 05:27 AM

Thanks for the answer. I made some changes to the code. I don't get any compile errors, but the result of lopm is always 0, no matter what I enter into the text boxes!


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace fogmassa
{
    public partial class fogmassa : Form
    {
        int kakellength = 1;
        int kakelwidth = 1;
        int fogwidth = 1;
        int lopm;

        public fogmassa()
        {
            InitializeComponent();


            if (txtLength.Text != "")
            {
                kakellength = Convert.ToInt32(txtLength.Text);
            }

            if (txtWidth.Text != "")
            {
                kakelwidth = Convert.ToInt32(txtWidth.Text);
            }

            if (txtFogWidth.Text != "")
            {
                fogwidth = Convert.ToInt32(txtFogWidth.Text);
            }

            lopm = ((1 / (kakelwidth + fogwidth)) + (1 / (kakellength + fogwidth)));


        }

        private void btnCalc_Click(object sender, EventArgs e)
        {
            lblLopM.Text = ("Löpmeter fog " + lopm + " m");
        }
    }
}


Was This Post Helpful? 0

#4 Guest_Marcus*


Reputation:

Re: The name does not exist in the current context

Posted 23 November 2010 - 06:14 AM

Another perhaps better example.

Why are the variables not available to the button function? It doesn't help making them "public".

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace fogmassa
{
    public partial class fogmassa : Form
    {
        

        public fogmassa()
        {
            InitializeComponent();

        
        }

        private void txtWidth_TextChanged(object sender, EventArgs e)
        {
            int kakelwidth = Convert.ToInt32(txtWidth.Text);
        }

        private void txtLength_TextChanged(object sender, EventArgs e)
        {
            int kakellength = Convert.ToInt32(txtLength.Text);
        }

        private void txtThick_TextChanged(object sender, EventArgs e)
        {
            int kakelthick = Convert.ToInt32(txtThick.Text);
        }

        private void txtFogWidth_TextChanged(object sender, EventArgs e)
        {
            int fogwidth = Convert.ToInt32(txtFogWidth.Text);
        }

        private void btnCalc_Click(object sender, EventArgs e)
        {
            int lopm = ((1 / (kakelwidth + fogwidth)) + (1 / (kakellength + fogwidth)*1000));
            lblLopM.Text = ("Löpmeter fog " + lopm + " m");
        }
    }
}


Was This Post Helpful? 0

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: The name does not exist in the current context

Posted 23 November 2010 - 06:26 AM

Where are the variables in that latest code? I don't see any except in the button Click handler?

As to why the value is 0, the answer is integer division. When dividing integers, you're always going to get an integer result (and hell, you're ASSIGNING it to an integer). If you have an integer 1 and you divide it by the integer 3 and assign it to an integer variable, what would you expect the integer variable to contain?
Was This Post Helpful? 0
  • +
  • -

#6 Guest_Marcus*


Reputation:

Re: The name does not exist in the current context

Posted 23 November 2010 - 06:34 AM

View PostJackOfAllTrades, on 23 November 2010 - 05:26 AM, said:

Where are the variables in that latest code? I don't see any except in the button Click handler?

As to why the value is 0, the answer is integer division. When dividing integers, you're always going to get an integer result (and hell, you're ASSIGNING it to an integer). If you have an integer 1 and you divide it by the integer 3 and assign it to an integer variable, what would you expect the integer variable to contain?

I changed it to decimal. The variables are in these parts of the code:

private void txtWidth_TextChanged(object sender, EventArgs e)
        {
            decimal kakelwidth = Convert.ToDecimal(txtWidth.Text);
        }



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace fogmassa
{
    public partial class fogmassa : Form
    {


        public fogmassa()
        {
            InitializeComponent();


        }

        private void txtWidth_TextChanged(object sender, EventArgs e)
        {
            decimal kakelwidth = Convert.ToDecimal(txtWidth.Text);
        }

        private void txtLength_TextChanged(object sender, EventArgs e)
        {
            decimal kakellength = Convert.ToDecimal(txtLength.Text);
        }

        private void txtThick_TextChanged(object sender, EventArgs e)
        {
            decimal kakelthick = Convert.ToDecimal(txtThick.Text);
        }

        private void txtFogWidth_TextChanged(object sender, EventArgs e)
        {
            decimal fogwidth = Convert.ToDecimal(txtFogWidth.Text);
        }

        private void btnCalc_Click(object sender, EventArgs e)
        {
            decimal lopm = ((1 / (kakelwidth + fogwidth)) + (1 / (kakellength + fogwidth)*1000));
            lblLopM.Text = ("Löpmeter fog " + lopm + " m");
        }
    
    }
}


Was This Post Helpful? 0

#7 Guest_Marcus*


Reputation:

Re: The name does not exist in the current context

Posted 23 November 2010 - 06:38 AM

This is how my instance of visual studio looks like
Posted Image
Was This Post Helpful? 0

#8 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: The name does not exist in the current context

Posted 23 November 2010 - 06:46 AM

You've still got the same problem that you had to begin with. You're declaring the variables inside functions, which means that these variables are not visible to any other portion of your code. Scope is an important thing to learn for any programmer. Declare your variables inside the class, but outside of any functions, then you can use the functions to alter the values of those variables, and call the functions that do your calculations inside your button press function.
Was This Post Helpful? 0
  • +
  • -

#9 Guest_Marcus*


Reputation:

Re: The name does not exist in the current context

Posted 23 November 2010 - 08:16 AM

Great, I got it to compile without errors now. However, there are other errors now.

kg_fog always returns 0, even though lopm contains a value...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace fogmassa
{
    public partial class fogmassa : Form
    {
        float kakelwidth;
        float kakellength;
        float kakelthick;
        float fogwidth;
        float lopm;
        float basvikt;
        float kg_fog;
        


        public fogmassa()
        {
            InitializeComponent();

            
        }

       
        private void txtWidth_TextChanged(object sender, EventArgs e)
        {
            kakelwidth = float.Parse(txtWidth.Text);
        }

        private void txtLength_TextChanged(object sender, EventArgs e)
        {
            kakellength = float.Parse(txtLength.Text);
        }

        private void txtThick_TextChanged(object sender, EventArgs e)
        {
            kakelthick = float.Parse(txtThick.Text);
        }

        private void txtFogWidth_TextChanged(object sender, EventArgs e)
        {
            fogwidth = float.Parse(txtFogWidth.Text);
        }

        private void lopmeter()
        {
            lopm = ((1 / (kakelwidth + fogwidth)) + (1 / (kakellength + fogwidth) * 1000));
        }

        private void kilo()
        {
            lopmeter();
            basvikt = ((fogwidth/100)*(kakelthick/100)*(lopm*100));
        }
    
        private void FB19()
        {
            kg_fog = (basvikt * 2.1f);
        }
        private void btnCalc_Click(object sender, EventArgs e)
        {
            kilo();
            lblLopM.Text = ("FB19 " + kg_fog + " kg");
        }
    
    }
}


Was This Post Helpful? 0

#10 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: The name does not exist in the current context

Posted 23 November 2010 - 08:33 AM

That's because your FB19() method is the only one that TOUCHES the kg_fog variable, yet the FB19() method is never called anywhere.
Was This Post Helpful? 0
  • +
  • -

#11 Guest_Marcus*


Reputation:

Re: The name does not exist in the current context

Posted 23 November 2010 - 08:44 AM

View PostSergio Tapia, on 23 November 2010 - 07:33 AM, said:

That's because your FB19() method is the only one that TOUCHES the kg_fog variable, yet the FB19() method is never called anywhere.


Thank you! However, even after these modifications, the kg_fog still returns 0...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace fogmassa
{
    public partial class fogmassa : Form
    {
        float kakelwidth;
        float kakellength;
        float kakelthick;
        float fogwidth;
        float lopm;
        float basvikt;
        float kg_fog;
 
        public fogmassa()
        {
            InitializeComponent();
       
        }

       
        private void txtWidth_TextChanged(object sender, EventArgs e)
        {
            if (txtWidth.Text != "")
            {
                kakelwidth = float.Parse(txtWidth.Text);
            }
        }

        private void txtLength_TextChanged(object sender, EventArgs e)
        {
            if (txtLength.Text != "")
            {
                kakellength = float.Parse(txtLength.Text);
            }
        }

        private void txtThick_TextChanged(object sender, EventArgs e)
        {
            if (txtThick.Text != "")
            {
                kakelthick = float.Parse(txtThick.Text);
            }
        }

        private void txtFogWidth_TextChanged(object sender, EventArgs e)
        {
            if (txtFogWidth.Text != "")
            {
                fogwidth = float.Parse(txtFogWidth.Text);
            }
        }

        private void lopmeter()
        {
            lopm = ((1 / (kakelwidth + fogwidth)) + (1 / (kakellength + fogwidth) * 1000));
        }

        private void kilo()
        {
            lopmeter();
            basvikt = ((fogwidth/100)*(kakelthick/100)*(lopm*100));
        }
    
        private void FB19()
        {
            kg_fog = basvikt * 2.1f;
        }
        private void btnCalc_Click(object sender, EventArgs e)
        {
            FB19();
            kilo();
            lblLopM.Text = ("Mängd fog: " + kg_fog + " kg");
        }
    
    }
}



Thanks again!
Was This Post Helpful? 0

#12 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: The name does not exist in the current context

Posted 23 November 2010 - 08:49 AM

Your variable names are giving me a headache. :lol:

Just follow your logic and you'll see what's going on. I'm sure something you THINK is firing, isn't actually firing. Use breakpoints and go step by step.
Was This Post Helpful? 0
  • +
  • -

#13 Guest_Marcus*


Reputation:

Re: The name does not exist in the current context

Posted 23 November 2010 - 08:51 AM

View PostSergio Tapia, on 23 November 2010 - 07:49 AM, said:

Your variable names are giving me a headache. :lol:

Just follow your logic and you'll see what's going on. I'm sure something you THINK is firing, isn't actually firing. Use breakpoints and go step by step.

I'll do that! Thank you!

My variable names are in swedish :)
Was This Post Helpful? 0

#14 Mitja Bonca  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 82
  • Joined: 07-May 09

Re: The name does not exist in the current context

Posted 23 November 2010 - 11:29 AM

Try this code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace fogmassa
{
    public partial class fogmassa : Form
    {
        float kakelwidth;
        float kakellength;
        float kakelthick;
        float fogwidth;
        float lopm;
        float basvikt;
        float kg_fog;
 
        public fogmassa()
        {
            InitializeComponent();
       
        }

       
        private void txtWidth_TextChanged(object sender, EventArgs e)
        {
            if (txtWidth.Text != "")
            {
                kakelwidth = float.Parse(txtWidth.Text);
            }
        }

        private void txtLength_TextChanged(object sender, EventArgs e)
        {
            if (txtLength.Text != "")
            {
                kakellength = float.Parse(txtLength.Text);
            }
        }

        private void txtThick_TextChanged(object sender, EventArgs e)
        {
            if (txtThick.Text != "")
            {
                kakelthick = float.Parse(txtThick.Text);
            }
        }

        private void txtFogWidth_TextChanged(object sender, EventArgs e)
        {
            if (txtFogWidth.Text != "")
            {
                fogwidth = float.Parse(txtFogWidth.Text);
            }
        }

        private void lopmeter()
        {
            lopm = ((1 / (kakelwidth + fogwidth)) + (1 / (kakellength + fogwidth) * 1000));
        }

        private void kilo()
        {
            lopmeter();
            basvikt = ((fogwidth/100)*(kakelthick/100)*(lopm*100));
        }
    
        private void FB19()
        {
            kg_fog = basvikt * 2.1f;
        }
        private void btnCalc_Click(object sender, EventArgs e)
        {
            kilo();
            FB19();            
            lblLopM.Text = ("Mängd fog: " + kg_fog + " kg");
        }
    
    }
}




This should work now.
Where was the catch you will ask?
In your last method, you called 1st the method FB19() and then kilo(). Wrong. Method FB19() has the variable "basvikt", which is only set in method kilo(). So kio() has to be called first.

You only HAVE TO make sure that variables:
- fogwidth
- kakelthick
- lopm
are set before buttonCalc is clicked!

Hope this has salved the issue here.
bye
Mitja

This post has been edited by Mitja Bonca: 23 November 2010 - 11:33 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1