+ Start a Discussion
Navneeth RajNavneeth Raj 

Basic Program Error

public class StudentNew 
{
    public string name;
    public static decimal fee=10000;
    
    public void setData(string name)
    {
        this.name=name;
        fee=fee+1000;
    }
    
    public static getData(decimal fee)
    {
        fee=fee+5000;
    }
}

This generates error "Duplicate variable fee". bt if i change fee to another word in getData method it says constructor cannot have static (but it is not constructor is it). am i doing it wrong or if so correct me. Please reply
Best Answer chosen by Navneeth Raj
Shrikant BagalShrikant Bagal
Hello,

Please try following code:

 
public class StudentNew 
{
    public string name;
    public static decimal m_fee=10000;
    
    public void setData(string name)
    {
        this.name=name;
        m_fee = m_fee + 1000;
    }
    
    public static decimal getData(decimal fee)
    {
        m_fee = fee + 5000;
    }
}


If its helps, please mark as best answer so it will help to other who will serve same problem.
T​hanks!
 

All Answers

Shrikant BagalShrikant Bagal
Hello ,


Cause:  You are trying to define duplicate variable "fee" in function 

Please try following code:
 
public class StudentNew 
{
    public string name;
    public static decimal m_fee=10000;
    
    public void setData(string name)
    {
        this.name=name;
        fee=fee+1000;
    }
    
    public static getData(decimal fee)
    {
        m_fee = fee + 5000;
    }
}

If its helps, please mark as best answer so it will help to other who will serve same problem.
T​hanks! 
Navneeth RajNavneeth Raj
Constructor can't be static Error
Shrikant BagalShrikant Bagal
Hello ,

In above code we were not specifing return type for "getDate" method so it will consider as Constructor Please use following code:
 
public class StudentNew 
{
    public string name;
    public static decimal m_fee=10000;
    
    public void setData(string name)
    {
        this.name=name;
        fee=fee+1000;
    }
    
    public static decimal getData(decimal fee)
    {
        m_fee = fee + 5000;
    }
}

If its helps, please mark as best answer so it will help to other who will serve same problem.
T​hanks! 
Navneeth RajNavneeth Raj
User-added image
Sorry, different errors comes on. why it gives these errors.
Shrikant BagalShrikant Bagal
Hello,

Please try following code:

 
public class StudentNew 
{
    public string name;
    public static decimal m_fee=10000;
    
    public void setData(string name)
    {
        this.name=name;
        m_fee = m_fee + 1000;
    }
    
    public static decimal getData(decimal fee)
    {
        m_fee = fee + 5000;
    }
}


If its helps, please mark as best answer so it will help to other who will serve same problem.
T​hanks!
 
This was selected as the best answer
Navneeth RajNavneeth Raj
Error at 14, Non-void method might not return a value or might have statement after a return statement.
Jigar.LakhaniJigar.Lakhani

Hello,

Please try with below code.

public class StudentNew 
{
    public string name;
    public static decimal m_fee=10000;
    
    public void setData(string name)
    {
        this.name=name;
        m_fee = m_fee + 1000;
    }
    
    public static decimal getData(decimal fee)
    {
        m_fee = fee + 5000;
		return m_fee;
    }
}

 

Thanks & Cheers,
Jigar(pateljb90@gmail.com)