I am writing a class. I have encountered the problem in the title.
Here is the code:
class delivery
{
private string strDeliveryName;
private string strDeliveryAddress;
private string strDeliveryDay;
private string strDeliveryTime;
private string strDeliveryMeal;
private string strDeliveryInstructions;
private string strDeliveryStatus;
}
public delivery(string deliveryName, string deliveryAddress, string deliveryDay, string deliveryTime, string deliveryMeal, string deliveryInstructions, string deliveryStatus)
{
strDeliveryName = deliveryName;
strDeliveryAddress = deliveryAddress;
strDeliveryDay = deliveryDay;
strDeliveryTime = deliveryTime;
strDeliveryMeal = deliveryMeal;
strDeliveryInstructions = deliveryInstructions;
strDeliveryStatus = deliveryStatus;
}
I get the error on the public delivery, any idea why?
Your constructor should be within the brackets of the class definition. On an unrelated note, the convention is to capitalize the first letter of class names.
Your constructor code is not inside the class. Move it inside and all should be fine. :-)
To answer your second question (in the comment), you need to change the name of the constructor to match the name of the class.
This error is because you have declared the function outside of the main class. You should insert your code inside the main class.
I received this error because I accidentally missed an open brace in code above where the error occurred. This meant the class ended prematurely. So if you get this error maybe check that your braces are correct.
Related
I'm attempting to construct a database using the C# Console, but I'm getting an error thrown that I can't seem to find the problem with. The problem is in my Query function, when I attempt to define the path based off of user input. Essentially, it looks into a given folder in the user folder, and within it another subfolder containing the files wherein is the database information. Im using the Path.Combine function to dynamically define the path throughout my code, but I'm getting the following error;
ArgumentNullException: Value cannot be null. (Parameter 'path3')
Even though all strings are defined and not null.
this is what the line of code looks like:
path = Path.Combine(Info.path, "infolder", classrooms.inventory, classrooms.geninv, userinput)
And here is all of the code that the strings in the path arguement are referencing:
class classrooms
{
public static string Teacher;
public static string Roomnumber;
public static string Changes;
public static string inventory = "inventory";
public static string geninv = "GenInv";
public static string classinv = "ClassInv";
}
and
class Info
{
public static string path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
}
if necessary I can attach my full code.
Screenshot of Error
I figured it out.
It was an improperly defined string that the pathcombine function was using in another instance of the code, and it was just getting caught up once i reused the path.combine function. Thanks for all the answers, I wouldn't have thought to look at that without all the input.
I'm having trouble understanding why a part of my code can't resolve another part.
I have this class that contains two properties. The second property relies on the first, but it keeps throwing this error:
cannot resolve symbol 'yearlyEmployees'
public class Financials
{
public static IEnumerable<SalaryEntity> yearlyEmployees = FactoryManagement(12345);
//cannot resolve symbol 'yearlyEmployees'
public static IEnumerable<CompanyEntity> YearlyGroup(IList<yearlyEmployees> allExempt)
{
}
}
I'm sure there's an easy answer, but I just can't find it.
Thanks!
yearlyEmployees is a variable name, not a class name. Try:
public static IEnumerable<CompanyEntity> YearlyGroup(IList<SalaryEntity> allExempt)
You must use the type SalaryEntity as item for your list.
public static IEnumerable<CompanyEntity> YearlyGroup(IList<SalaryEntity> allExempt) {}
That is because you don't have a type of yearlyEmplyee - that is your variable.
Instead:
public static IEnumerable<CompanyEntity> YearlyGroup(IList<SalaryEntity> allExempt)
{ }
And then just pass a collection of SalaryEntity to the function. If you always want to process only yearlyEmployees (which I don't think is the case but not sure) then just call it from within the method `Financials.yearlyEmployees'.
I want to apply this:
private string _name;
public string name
{
get { return _name; }
set { _name = Fix(value); }
}
to all string the members of a class, and don't want to repeat the same code for all the class members.
An obvious solution would be to put that code on a class to handle the problem and declare all string members as: myString instead of string, however that would mean that I would have to access the main class members like this: email.fixed instead of just email.
So I was wondering, is there is some kind of template I can define and then apply easily?
You could create a Code Snippet for Visual Studio to handle building a property this way.
MSDN includes documentation on Creating a Code Snippet, which can include replacement parameters (the name).
You might want to research Aspect Oriented Programming, which allows you to easily do things like this.
http://www.codeproject.com/Articles/337564/Aspect-Oriented-Programming-Using-Csharp-and-PostS
Create a type with an implicit conversion to and from string:
public class FixedString
{
private string str;
public FixedString(string str){this.str = str;}
public static implicit operator FixedString(string str)
{
return new FixedString(Fix(str));
}
public static implicit operator string(FixedString fixed)
{
return fixed.str;
}
}
You can now create a property of this type, but treat it as if it's a string.
Create a regex replace and use Visual Studio's (v2012/2013) find and replace regex functionality.
For example let us say you have a field like this to change into a property
public string Name;
and you want to change it to have a backing field and the setter you desire.
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = Fix(value); }
}
The find /replace regex pattern in Visual Studio to find is this
public\s+string\s([^;]+);
the replace pattern (with appropriate line spacings and tabs) is this
private string _$1;\r\n\r\n\tpublic string $1\r\n\t{\r\n\t\tget\r\n\t\t\t{ return _$1; }\r\n\t\tset\r\n\t\t\t{\r\n\t\t\t\t_$1 = Fix(value);\r\n\t\t\t}\r\n\t\t}
Then step through the finds and do a replace as needed within your class.
I have done similar to add INotifyChange method calls on blank properties created after doing the code snippet <tab><tab>prop action.
I'm fairly new to C# but have extensive experience in Objective-C and OOP. I'm using Json.NET to automatically parse API responses to objects. It so happens that one of the objects returned has a property named protected. Obviously this is a problem, because protected is a keyword for class member declaration.
"protected": true
Is it possible to add a member with the name protected at all?
Is it possible to add setters and getters that get triggered, if the parser tries to set the protected property? (but assign the value to a private member named _protected)
Should I modify the parser to behave different when he encounters a property named protected?
Thanks for any advice.
1:
For question #1: You can put an # symbol before it any keyword you want to use as a variable name.
E.g.
public string #protected {get; set; }
I recommend against doing this, however. You should be able to remap the "protected" field in your JSON to a different property in your POCO.
2:
private string _protected;
public string #protected
{
get
{
//any additional code you want
return _protected;
}
set
{
//any additional code you want
_protected = value;
}
}
3:
Up to you!
I implemented this solution:
[JsonProperty("protected")] public bool Protected { get; set; }
Like Daniel Mann suggested in his comment:
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Naming convention for private fields
I read on msdn about C# naming conventions but they don't talk about naming private fields vs parameters.
public void SetAnimation(int framesX, int framesY)
{
framesX = framesX; // the first one is private class member
framesY = framesY;
}
I must name private field differently than a parameter. I can't do camel case for both.
What do you suggest?
You can have same name. To have it more elegant, you can have:
this.framesX = framesX; // the first one is private class member
this.framesY = framesY;
I generally do:
private int _framesX;
private int _framesY;
That makes it more clear:
public void SetAnimation(int framesX, int framesY)
{
_framesX = framesX;
_framesY = framesY;
}
This really depends on the company you work at. Basically a naming convention is a team rule and for example, Resharper allows you to add a team convention
You can name it the same, but if referring to the private member you have to use
this.framesX;
So for example;
this.framesX = framesX;
I normaly name my private fields (are there others ;) ) with a '_' prefixed. For some people this is a smell others do the same - I guess it's a matter of taste.
I use _framesX for marking private fields.
You could go with the "_" prefix for private members or use "this.framesX" or both "this._framesX".
I'd say it depents on what your team is using.
Your code is fine, you might need to specify this thought.
Well thats how I name my Fields and parameters mostly.
public void SetAnimation(int framesX, int framesY)
{
this.framesX = framesX; // the first one is private class member
this.framesY = framesY;
}
private variables should start with lowercase. so you've got that right.
you can make yr code work by using 'this' keyword to refer back to the instantiated object.
public void SetAnimation(int framesX, int framesY)
{
this.framesX = framesX; // the first one is private class member
this.framesY = framesY;
}
I have a tendency to use only properties starting with a capital, and define the getter and setter public or private
public int FramesX { public get; private set; }
public int FramesY { get; set; }