Hi there,
I'm new to C# and I have to admit it feels very overwhelming. I was wondering if anyone could help me and explain it to me so that I can understand.

Here's my code...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Counter1
{

    public class Counter
    {
        private int n = 0;

        public int Increment()
        {
            n = n + 1;
            
         }
    
              
        public readonly int GetCount()
        {
           get
            {
             return n;
            }
    }
    }
}

I'm not sure what I'm actually doing wrong... it doesn't recognise the get function and I have researched through many websites and they don't do much besides using the word get and I have already done that.

If anyone could help me out I'd appreciate it so much!! It's hard to get my mind around it.

Properties don't have parameters. Your line 20 says that the method GetCount has zero parameters, which is not the same as not having them. Change the line to

public int GetCount {
    get {
        return n;
    }
}

readonly isn't needed as you don't provide a set method so there is no way someone outside the class could set the value.

Also, it is standard practice to name properties after what they are and to exclude the Get/Set on the name, so your property would become:

public int Count {
    get {
        return n;
    }
}

Thank you so much for your reply! That has helped me understand a lot better now.

But I appear to be having a problem with Increment()

It says 'Counter1.Counter.Increment()': Not all code paths return a value.

What does this mean?

The method signature for Increment() is public int Increment() . It consists of four parts:

  • Access modifier (public)
  • Return type (int)
  • Method name (Increment)
  • Parameters (N/A, as you don't have any)

In your case the problem is with the return type. You've said "This method will return an integer value". But you method has no return statement. That's what it means by "not all code paths return a value" as the compiler can tell that it is possible to finish running the method without returning anything.

You can fix this by adding return n as the last line, or you can change the return type to void (meaning nothing is returned so no return is needed).

private int n = 0;

        public void Increment()
        {
            n = n + 1;

        }


        public  int GetCount()
        {
            return n;
        }

in Increment button

Increment();

in show button

MessageBox.Show("" + GetCount());

Edited 5 Years Ago by alasser222: n/a

Momerath already showed you how to use properites. This how you have to do:

public partial class Form1 : Form
    {
        private int n;
        public int N { get { return n; } }

        public Form1()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }       

        public void Increment()
        {
            n = n + 1;
        }        

        private void button1_Click(object sender, EventArgs e)
        {
            Increment();
            MessageBox.Show("New number is " + n);
        }
    }

Mitja

Hi there,
I'm new to C# and I have to admit it feels very overwhelming. I was wondering if anyone could help me and explain it to me so that I can understand.

Here's my code...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Counter1
{

    public class Counter
    {
        private int n = 0;

        public int Increment()
        {
            n = n + 1;
            
         }
    
              
        public readonly int GetCount()
        {
           get
            {
             return n;
            }
    }
    }
}

I'm not sure what I'm actually doing wrong... it doesn't recognise the get function and I have researched through many websites and they don't do much besides using the word get and I have already done that.

If anyone could help me out I'd appreciate it so much!! It's hard to get my mind around it.

If you got condition. can use for loop.

This article has been dead for over six months. Start a new discussion instead.