|
What are the specific rules to name a C# source file? I came across many posts about this issue but all are about special cases. For example, Microsoft's page about this topic does not really get to the point.
https://msdn.microsoft.com/en-us/library/ms228500%28v=vs.90%29.aspx[^]
I need clear and to-the-point answers. Is a C# source file name arbitrary? I am using code samples from several C# books and the samples tend to pick up the name of the top (non-nested) class in the file or the name of namespace, or maybe something else. I have not tried to play with this yet but my feeling is that the file name could be just arbitrary. Is it correct? What are the imposed restrictions on file name and what are the actual guidelines?
|
|
|
|
|
Well, it sort of gets to the point:
Quote: Unlike Java, source files can contain more than one top-level public class declaration, and the file name does not need to match any of the classes' names. So you wouldn't violate an official naming rule by naming your files what you deem most appropriate. Normally they should contain only one class and have the same name as that class because that's what's most intuitive.
In my opinion, it sometimes makes sense to have more than one class in a source file. In that case I would name the file like the class that is either the base class or represents the functionality that is most important within that source file. In some cases, e.g. lots of small classes that serve a very similar purpose, I put them all in a single source file and name it according to the purpose of those classes. But it should remain the exception from the rule.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
There are no real rules around the way you name class files, but something to consider is what the purpose of your naming is. Generally, it's a good idea to name the file the same as the class because it makes finding your class a lot easier. If your class is called Foo but you name it Bar.cs then it's going to be harder to find the file in the solution. Generally, it makes sense to try to replicate your namespace structure with folders and your classes in the file name.
|
|
|
|
|
CRobert456 wrote: I have not tried to play with this yet but my feeling is that the file name
could be just arbitrary. Is it correct? Yup, if you generate a GUID and use that as a name, things will work too. Wouldn't be very practical.
It is often yelled that each file should contain a single class, and be named after it. That does lead to a "lot" of files if you stoically do so. There's often some helper-classes that are local to the form, like an inherited EventArgs , or an Enum . If they're really local, they become nested classes within the same file.
Keeps things in a place that is a bit predictable
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Consider generic types with the same name, like Tuple.
You could put
Tuple, Tuple<T>, Tuple<T1, T2> , etc in separate files (Tuple.cs, Tuple`1.cs, Tuple`2.cs), but it's acceptible to put them in one file.
|
|
|
|
|
I have disabled the button on form load. Then I want to enable it when a condition is accomplished, but something's wrong and i can't figure it out what exactly. Conditions are accomplished but button is still disabled.
private void button1_EnabledChanged(object sender, EventArgs e)
{
if (textBox1.Text != "" && textBox2.Text != "" || textBox3.Text != "")
if(ok)
button1.Enabled = true;
}
|
|
|
|
|
You're changing the buttons enabled state in the enable state changed event handler. That doesn't seem right to me. Also, you might want to check your and/or logic in the text box checks.
|
|
|
|
|
So there is no method that I can use it for this? I can enable the button from another function after I check those conditions, but I wanted to do it in this way..
|
|
|
|
|
This looks like WinForms to me. If so, you can use a technique like this[^] to accomplish what you want.
|
|
|
|
|
Aaaa yes, sorry. Is WinForm. Thanks, I'll check it right now
|
|
|
|
|
The task you describe can be approached in several different ways:
I. You could have very specific implementations of each "rule" for each change in "UI state;" for example, you could view the criteria for setting 'button1 to enabled as being dependent upon three TextBoxes all having content that is not string.Empty.
Which means you will have to have event handlers of some Form monitoring the changes in content in the three TextBoxes.
II. You could implement a "generic" strategy for controlling Control properties like 'Visible, 'Enabled (or any other properties) which would contain the data for the settings of each Control's properties in a number of "states:" i.e., a "state machine" approach.
Let's say I defined several "methods" (using Func<>) to express different possible states of the three TextBoxes:
private Func<bool> AllEmpty;
private Func<bool> AllFull;
private Func<bool> AnyEmpty;
private Func<bool> CurrentTextBoxTest;
private void Form1_Load(object sender, EventArgs e)
{
AllEmpty = new Func<bool>(() =>
{
return textBox1.Text == string.Empty && textBox2.Text == string.Empty && textBox3.Text == string.Empty;
});
<pre>
AnyEmpty = new Func<bool>(() =>
{
return (textBox1.Text == string.Empty || textBox2.Text == string.Empty || textBox3.Text == string.Empty);
});
AllFull = new Func<bool>(() => {! AnyEmpty() });
CurrentTextBoxTest = AllFull;
}
And I wired-up the same 'Leave EventHandler, named 'TB_Leave,' to the three TextBoxes, defined like this:
private void TB_Leave(object sender, EventArgs e)
{
if(CurrentTextBoxTest != null) button3.Enabled = CurrentTextBoxTest();
} In this case, the contents of 'CurrentTextBoxTestState would be 'AllFull: so the button would be enabled only if all three TextBoxes had content.
If the UI then "moved" to a "state" where the content of those three TextBoxes should have no impact on 'button1's current Enabled state: then I'd just set the 'Func CurrentTextBoxTest to null.
If the UI then "moved" to a "state" where all TextBoxes being "empty" meant the Button should be enabled: I'd set 'CurrentTextBoxTestState to 'AllEmpty.
Note that I'm not saying you should implement some kind of "formalism" for UI state control ... as described here ... for any given project.
The point it becomes worth the time and effort to implement such a formalism is, imho, when you have a complex UI with several possible state changes, and various complex dependencies of Controls on the state of other Controls.
Other "tools" for dealing with UI state change include dynamically moving, and/or restoring, Control EventHandler definitions depending on state.
Another strategy (which I have implemented in the past) involves keeping a Dictionary<Control, List<ControlState>>, where 'ControlState in the 'ControlState List contains data (like values for 'Enabled, 'BackgroundColor, etc.) per state, and/or an executable Function. The idea being that for all Controls that have a special "marker" (Attribute, Tag, etc.) a "state controller" can take a state index as an input, traverse the Dictionary, pull out the matching state, and apply it.
I am not familiar with using the IExtender Provider mentioned by Pete, but look forward to studying it.
«To kill an error's as good a service, sometimes better than, establishing new truth or fact.» Charles Darwin in "Prospero's Precepts"
|
|
|
|
|
The Button1_EnableChanged event doesn't fire until Button1.Enabled = true; executes. Hence, Button1.Enabled = true; must go into another method, such as, for example, the method that detects the change that makes it appropriate to enable the button.
As an example, if I have a routine that has a TextBox1_TextChanged event that evaluates the Text property of TextBox1 , and determines that its new value warrants enabling Button1 , it does so by setting its Enabled property to True .
|
|
|
|
|
I use RouteCollection with FriendlyUrlSettings to remove aspx from being shown... so now every time someone access my site, instead of display www.mysite.com/login.aspx , it will display www.mysite.com/login
But I have a problem where an old app needs to access a page, and it's already hard-coded inside the app to access www.mysite.com/service/AuthenticateUser.aspx page. But because of the friendlyurl, the app doesn't work.
So I added this line
routes.Ignore("Service/AuthenticateUser.aspx");
but it doesn't seem to do anything. Every time I access the page Service/AuthenticateUser.aspx , it will still redirect me to Service/AuthenticateUser only
|
|
|
|
|
How about asking this in the ASP.NET forum as it has nothing to do with C#.
|
|
|
|
|
err... why is this a asp.net question? As per my understanding, the codes that handles the ignoring of specific file or folder needs to be written in the back code (c#) in my case.
public static void RegisterRoutes(RouteCollection routes)
{
routes.RouteExistingFiles = false;
routes.Ignore("SERVICE/{*pathInfo}");
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
}
the code above is what I did but still doesn't work
|
|
|
|
|
Because you're asking about the functionality of ASP.NET and/or possibly MVC routing.
|
|
|
|
|
Hello there. I am given a small xml script. How can I use that script to change the frame rate of this IP camera. Thanks for any input.
|
|
|
|
|
The question is not clear. Please explain what C# issues you are having problems with.
|
|
|
|
|
Well, I would say that you need to read some value from your XML file. It will either be an attribute or an element. Oh wait, it might be a set of values that you use to calculate the rate. If it is, you will have to read the values in and apply whatever calculation is appropriate. However, unless I'm greatly mistaken, IP cameras don't use framerates - they use bandwidth, and while you may be able to change the frame rate, the underlying bandwidth might not be able to cope.
As for actually choosing a frame rate and displaying your images then why not look at Aforge and set the DesiredFrameRate[^].
|
|
|
|
|
I have a textBox where i want to insert a string in a licence model (fdsf-dfss-fdfd-fdhd). I tried to select first 4 char and when I have 4 chars, is inserted "-". But it starts from the beginning when "-" is inserted after first 4 chars. This is how I tried:
private void textBox4_TextChanged(object sender, EventArgs e)
{
for (int i=0;i<=textBox4.Text.Length;i++)
{
while (textBox4.Text.Length == 4)
{
textBox4.Text += "-";
}
}
}
How can I make like after I have 4 chars and "-" is inserted, to be able to continue inserting the text after "-" ?
|
|
|
|
|
|
|
Change it to:
textBox4.Text += "-";
textBox4.SelectionStart = textBox4.Text.Length;
..in order to set the input cursor to the end of the text again.
Unrelated suggestion: Give your TextBoxes and all other controls a name that explains their purpose - in this case maybe "LicenceInput". That will make it much easier to read your code, even for yourself, after you haven't worked on it for a while
edit: A MaskedTextBox might be the most convenient way to handle this. The input mask would be: 0000\-0000\-0000\-0000
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
modified 20-Apr-15 7:15am.
|
|
|
|
|
it works, but after I insert another 4 characters, "-" is not inserted.
|
|
|
|
|
Please edit your original post and add your reworked code in a second code-block.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|