|
Variables should always be defined in the tightest scope that they are needed for. This is an extension of the principle that says you should have class instance variables not static or global ones, and that you should have function/procedure local variables not instance ones.
So if the variable's only used inside the block, declare it inside. That's true whether it's a try, an if, a while or whatever.
You can even scope sections of code simply to create a localisation region, e.g.
void SomeMethod(){
{
int a = 1;
Console.WriteLine(a);
}
{
string a = "test";
Console.WriteLine(a);
}
}
That is not generally something that people recommend, though.
|
|
|
|
|
BobJanova wrote: You can even scope sections of code simply to create a localisation region
Indeed, and whenever I've seen this done, this tells me that the method should have been split into separate methods.
|
|
|
|
|
The only time I've done something which is technically that is scoping case: blocks in a message dispatching switch. (It's in the game lobby library that I posted on here, I think.) A true short method fundamentalist would have each case do nothing but call a method to handle the individual case, but I'm not one of those.
Yeah, generally it's an indication of muddled thinking and that something is actually two separate operations.
|
|
|
|
|
BobJanova wrote: in a ... switch
That's the only occaision I've ever needed it too
|
|
|
|
|
I have a question about setting up permissions on a sql server 2008 r2 datbase.
I basically wrote and enhanced some C# 2008 and C# 2010 console applications that connected to my test sql server 2008 r2 database. On my test database, I must all full right my default.
My 3 applications were deployed to a user acceptance testing environment that includes a user acceptance sql server 2008 r2 standard database.
The user accpetance database was set up by the network administrator at my small company. He is the only one at my small company that knows a little bit about the dba roles and has locked down permissions.
Due to the facts above, I would like to know what should I be aware that may need to have permissions setup for. My questions includes the followinng:
1. When to decide if role(s) need to be setup and how to setup the roles. When I ran my applications on my test database, I had the integrated security set to true. I did not need to supply the user name and password in the connection strings to the database. My user account was setup to have a role in the database.
Due to what I just said, will I need to have setup roles for the console applications to run on their own? If so, What kind of roles need to be setup and how do you setup these roles?
2. I setup 3 new tables that are under the dbo schema. Do I need to have permissions setup so people and/or roles can have read, write, update and/or execute permissions? If so, how do you setup these permissions?
3. I have also created 2 stored procedures that are used to access the 3 new tables that I setup. Thus do these stored procedures need to have read, write, update and/or execute permissions on them setup?
4. Do statistics (explain plans) need to be run on this database that has hardly evern been used before? If so, how do you accomplisth this goal?
5. Are there other items I need to consider? If so, what are the items and what do I do to resolve those issues?
If you can any part of my questions above, I would appreciate hearing what your answer is also.
|
|
|
|
|
sc steinhayse wrote: I have a question about setting up permissions on a sql server 2008 r2 datbase.
I basically wrote and enhanced some C# 2008 and C# 2010 console applications that connected to my test sql server 2008 r2 database. On my test database, I must all full right my default.
Do you have a business case that mandates these applications need DBO to run?
Or are you just granting DBO to make sure they run right?
* I suggest you start by granting the least permissions and changing those permissions until the application executes. Then understand what it is about the code that requires the elevated permissions and try to rework it so that you can run at lower permissions. This is tedious but vitally important.
My 3 applications were deployed to a user acceptance testing environment that includes a user acceptance sql server 2008 r2 standard database.
The user accpetance database was set up by the network administrator at my small company. He is the only one at my small company that knows a little bit about the dba roles and has locked down permissions.
Due to the facts above, I would like to know what should I be aware that may need to have permissions setup for. My questions includes the followinng:
1. When to decide if role(s) need to be setup and how to setup the roles.
Roles are a business case. Do you need Admins, Editors, Readers, Etc... The ASP.Net Membership provider can implement role based security with some configuration code.
When I ran my applications on my test database, I had the integrated security set to true. I did not need to supply the user name and password in the connection strings to the database. My user account was setup to have a role in the database.
Actually integrated security means you are on a domain or using LDAP/Etc... so you don't need to pass in user/pass it's just authenticating you with your domain permissions.
Due to what I just said, will I need to have setup roles for the console applications to run on their own? If so, What kind of roles need to be setup and how do you setup these roles?
What do you mean by "run on their own"? Application permissions depend on who is signed in. So for example you could create a user {console_app_name}.dbo and grant DBO to that account. Then any user passing that username/password combination will have DBO on that applications database.
2. I setup 3 new tables that are under the dbo schema. Do I need to have permissions setup so people and/or roles can have read, write, update and/or execute permissions? If so, how do you setup these permissions?
In the web.config you specify the security. So if you want to granulate on each user then use integrated security. If you want all users to have DBO via your application then connect using an account you created that has DBO on the application DB and tables.
3. I have also created 2 stored procedures that are used to access the 3 new tables that I setup. Thus do these stored procedures need to have read, write, update and/or execute permissions on them setup?
Yes they do. Google "grant execute SQL Server".
4. Do statistics (explain plans) need to be run on this database that has hardly evern been used before? If so, how do you accomplisth this goal?
I'd recommend using a very robust web stats package that is trivial to setup on IIS. Google "AWSTATS for IIS"
5. Are there other items I need to consider? If so, what are the items and what do I do to resolve those issues?
Vast topic. Backups need to be considered as well as coding best practices. This is a big topic and you should find all you need at the ASP.Net (http://www.asp.net) websit.
If you can any part of my questions above, I would appreciate hearing what your answer is also.
There have been joys too great to be described in words, and there have been griefs upon which I have not dared to dwell, and with these in mind I say, climb if you will, but remember that courage and strength are naught without prudence, and that a momentary negligence may destroy the happiness of a lifetime. Do nothing in haste, look well to each step, and from the beginning think what may be the end. - Edward Whymper
Climb On!
|
|
|
|
|
Hi Everyone,
Just wondering if there is a good class in .NET that can convert a Text string to a Code128 text string, then off course that text needs to be in Code128 font.
Any input on this would be greatly appreciated. I understand that others might have already written. This is also fine as long as what is written is future ready if you know what I mean.
|
|
|
|
|
If you want it as a string, then you will indeed need a Code128 font. A simple google search should be able to find you one for that.
If you want it as an image, this article[^] works pretty well.
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
class, struct , interface, can we say they are C# objects?
|
|
|
|
|
No you can't...
An object is basically a block of memory that has been allocated and configured according to the blueprint.(Reference[^])
An object is an instance of a class which acts as its template.
A class is a construct that enables you to create your own custom types by grouping together variables of other types, methods and events. (Reference[^])
A struct type is a value type(Reference[^])
An interface contains only the signatures of methods, delegates or events.(Reference[^])
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
how can I refer to class, struct and interface with respect C# (lessical) building block? or??
I mean If I want to say the main building block in C# are: class, struct, interface, .
is there a more precise word than building block
|
|
|
|
|
You can use ?? for both objects(instances of classes) and struct instances.
You can't do anything like this with respect to interfaces, but you can do it using classes that implement a given interface.
I hope this is what you mean with your question. I'm guessing on what you actually mean.
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
I'm sure I'm not clear. In a email I should say:
'The building block of C# language are: classes, structs and interface'
I'm asking if is there a synonymous of building block (I'm looking for a word, I thought to use the word object, but you explained it is wrong)
modified 17-Oct-12 17:00pm.
|
|
|
|
|
Marcus Kramer wrote: An interface contains only the signatures of methods, delegates or events. And properties.
/ravi
|
|
|
|
|
Despite the numerous defintions you can roughly say that:
An interface is like a template for a class and a class is a template for an object.
A struct is nothing more then a name for a group of variables that belong together.
|
|
|
|
|
No, because "object" is a concept in C# and other object-oriented languages, which causes confusion (as you've seen in the replies so far).
I would say that class, struct, and interface are elements or components of C#, although strictly speaking interfaces do not exist on their own - an interface that is not implemented has no purpose.
|
|
|
|
|
I have a C# 2008 console application where all the output is displayed in DOS popup windows instead of being written to a log file.
Thus can you tell me what I can do to have all messages in this console application appear in a log file instead of these dos pop windows? Can you show me in code how to accomplish this goal?
|
|
|
|
|
It's a little too big a solution to do in a single post, so I'll link you to a good CP article that will give you a full solution.
log4net Tutorial[^]
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
You can pipe the stdout to a file, like this (run from console)
C:\> MyProgram.exe >C:\Temp\MyFile.txt
Also works from Windows-shortcuts.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
If the following is true.
1. ONLY log output is written (presumably the case)
2. You can change the code
Then you might want to try using the System.Console.SetOut() method.
You can provide your own writer which you can then put the data anywhere you want.
|
|
|
|
|
If you can change the code you can basically change all Console.WriteLine("") to a log function.
Example:
public void WriteLog(string logtext){
StreamWriter writer = new StreamWriter("[path here]", true);
writer.writeLine(DateTime.Now.ToString() + ": " + text);
writer.Close();
}
hope this helps.
|
|
|
|
|
Hello my friends,
I want to develop a AutoData C# winform application but the first thing I want to know is, how become I all the technical information? I can not start the project...
I google it about a autodata database or a webservice that will give me information, but nothing found...
Can anyone, anywhere help me?
Regards,
KZ
|
|
|
|
|
Killzone DeathMan wrote: Can anyone, anywhere help me?
What's this "autodata" that you speak of? Is it a website, a service, an idea, an application, a business?
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Unfortunately for you, AutoData is a subscription based service, so you have to purchase a subscription to get access to the underlying auto data. You can find more details here[^].
|
|
|
|
|
You are right men, thats it!
But I want to do a c# winform app that do all that stuff!
The problem is: where can I get all the technical information?
|
|
|
|