|
Cool thanks will look into this as I am still very new to C#.
Any recommended reading on this topic?
|
|
|
|
|
Yes[^].
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Hi all,
I've been trying to become more familiar with classes and instantiating them and the different times one would need to do so. I came across the following tutorial (http://www.csharp-station.com/Tutorials/Lesson05.aspx[^]
and it has the code below. Albeit, this code doesn't do anything useful, but again, just raises a question. The question I have is what would drive instantiating the class versus declaring getChoice() as static? As I'm a ME, please use plain english.
using System;
class Address
{
public string name;
public string address;
}
class MethodParams
{
public static void Main()
{
string myChoice;
MethodParams mp = new MethodParams();
do
{
myChoice = mp.getChoice();
mp.makeDecision(myChoice);
Console.Write("press Enter key to continue...");
Console.ReadLine();
Console.WriteLine();
} while (myChoice != "Q" && myChoice != "q");
}
string getChoice()
{
string myChoice;
Console.WriteLine("My Address Book\n");
Console.WriteLine("A - Add New Address");
Console.WriteLine("D - Delete Address");
Console.WriteLine("M - Modify Address");
Console.WriteLine("V - View Addresses");
Console.WriteLine("Q - Quit\n");
Console.WriteLine("Choice (A,D,M,V,or Q): ");
myChoice = Console.ReadLine();
return myChoice;
}
void makeDecision(string myChoice)
{
Address addr = new Address();
switch(myChoice)
{
case "A":
case "a":
addr.name = "Joe";
addr.address = "C# Station";
this.addAddress(ref addr);
break;
case "D":
case "d":
addr.name = "Robert";
this.deleteAddress(addr.name);
break;
case "M":
case "m":
addr.name = "Matt";
this.modifyAddress(out addr);
Console.WriteLine("Name is now {0}.", addr.name);
break;
case "V":
case "v":
this.viewAddresses("Cheryl", "Joe", "Matt", "Robert");
break;
case "Q":
case "q":
Console.WriteLine("Bye.");
break;
default:
Console.WriteLine("{0} is not a valid choice", myChoice);
break;
}
}
void addAddress(ref Address addr)
{
Console.WriteLine("Name: {0}, Address: {1} added.", addr.name, addr.address);
}
void deleteAddress(string name)
{
Console.WriteLine("You wish to delete {0}'s address.", name);
}
void modifyAddress(out Address addr)
{
addr = new Address();
addr.name = "Joe";
addr.address = "C# Station";
}
void viewAddresses(params string[] names)
{
foreach (string name in names)
{
Console.WriteLine("Name: {0}", name);
}
}
}
|
|
|
|
|
In the case above there is almost no difference; the tutorial is trying to illustrate a point that we can separate the logic from static code into methods of a class. This is the basis of inheritance whereby we can build on a simple or base class and keep adding functionality to create quite sophisticated objects. If you really have trouble understanding this I would suggest doing some study on OOP and C# classes.
[edit]Sorry, I meant C# not C++.[/edit]
It's time for a new signature.
|
|
|
|
|
Richard MacCutchan wrote: In the case above there is almost no difference; the tutorial is trying to illustrate a point that we can separate the logic from static code into methods of a class.
Yes, I understand that. I was hoping to get a general explanation of when one would want to declare the method as static versus instantiating the class. I would imagine that this has or could be the subject of a dissertation, but was looking for a brief answer as to what would be a reason for not declaring the method as static.
|
|
|
|
|
When a class represents a real-life object (e.g. a car), or something more abstract but not unique by its very nature (a binary tree), or something with some internal state, then it should be modeled by a non-static class.
It is only when there is going to be only one of them ever needed and it has no state, that you should consider a static class, and hence zero rather than one instances. A collection of global constants could be an example; a collection of simple methods could be too (see e.g. the trig functions in the Math class).
static classes are often abused; when in doubt, the class should not be static.
|
|
|
|
|
|
you're welcome.
|
|
|
|
|
mprice214 wrote: I was hoping to get a general explanation
That is what the tutorial is for. As I said in my previous answer if you have difficulty understanding this then you should study some more tutorials and books on the subject.
It's time for a new signature.
|
|
|
|
|
Hi!!
I would like to know, how to make statistical graphs in C #?
Please, help me!!!
|
|
|
|
|
Use the free charting library available for .Net (from Microsoft).
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Is Google on strike?
Free Microsoft chart controls:
http://code.msdn.microsoft.com/mschart
http://www.microsoft.com/downloads/details.aspx?FamilyId=130F7986-BF49-4FE5-9CA8-910AE6EA442C&displaylang=en
http://www.microsoft.com/downloads/details.aspx?FamilyId=1D69CE13-E1E5-4315-825C-F14D33A303E9&displaylang=en
commercial chart controls (all with trial versions):
Dundas Charts
Telerik Charts
Syncfusion Charts
Component One Charts
A flexible charting library for .NET[^]
|
|
|
|
|
I don't know which statistical graphs do you want??
if you want your graph more beautiful and your data isn't very mang ,you can use donnetcharting ,is very useful.
and zepgraph is a good control too!!!
|
|
|
|
|
Hi,
I think you replied to the wrong message!
you should press the reply widget of the original question if you want to provide an answer; that way the author of the question will get an e-mail notification (the one I got now as you replied to me).
|
|
|
|
|
Microsoft supplies one example libaray WinSamples,
there is many examples that how to use the chart.
|
|
|
|
|
Hey everyone,
I have a problem with XmlSerialization.
I have a property like this one:
[XmlElement(typeof(MyClass1),
ElementName = "myClass1"),
XmlElement(typeof(MyClass2),
ElementName = "myClass2"),
XmlElement(typeof(MyClass3),
ElementName = "myClass3"),
XmlElement(typeof(MyClass4),
ElementName = "myClass4")]
public ArrayList Items
{
get { return _items; }
set { _items = value; }
}
All MyClasses are derived from the same base-class.
Now, I like to check which XmlElements are "allowed" for my property "Items". It would be very nice if I have a function which returns me a list of the Class-Names (MyClass1, MyClass2, MyClass3, MyClass4). Is this possible? I found no way to access the XmlElement-Attributes of a property.
Thanks in advance
|
|
|
|
|
I would recommend using List<yourbaseclass> rather than an ArrayList. The more strong typing you can do, the better. That would automatically limit the allowed types to some degree. Are there other subclasses that you do not want to be allowed in the list?
|
|
|
|
|
The arraylist is fix because this code snippet is already in use and I can't change all dependencys. I can only add a function.
|
|
|
|
|
How to have Ruler for form in RunTime with diffrent units (centimeters OR pixels OR inch OR ...)!
|
|
|
|
|
I assume you want to use Windows Forms. Assuming that, you might want to look at this[^] article. Now, it's for inches but it would be fairly easy for you to convert it to support different units.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Hi guys,
I posted last week about an issue that I'm having but it was lost in a flood of new posts.
Conceptually - I need to be able to have my process write a file to an external share that is locked down by a production controlled ProcessID | Password combination. We have a contrived development example that is configured as a folder locked down by a local User Account ("Foo" | "Bar") on one of our developer's machines.
I need to:
1)reference this machine via UNC (\\server\share)
2)programmatically provide the above credentials from our configuration file
3)deposit a file.
Impersonation is a very last resort, as it would require jumping through tons of hoops with Security(...among other things and just generally being a very bad idea)
Does anyone have any experience at all completing this suite of tasks?
"I need build Skynet. Plz send code"
|
|
|
|
|
I'm afraid impersonation is the only solution, or run process as user, which is simpler to implement.
I'm not sure I understand what you mean by ProcessId|Password combination? Is this ProcessId a user account? or rather a Unix-like setuid assigned to the executable?
|
|
|
|
|
...a User account in our corporate directory that exists not for an employee but for a process that needs access to resources in the corporate domain having a password that is only known by our production management group
"I need build Skynet. Plz send code"
|
|
|
|
|
meh...Impersonation it is then, I guess. This is a major shortcoming of the .NET platform. There is no excuse for this not to be encapsulated by the managed runtime. At any rate, if anyone needs to Impersonate a user, here is the code for a class that I threw together...DuplicateToken isn't even invoked, but I wrote it, so I'll include it.
using System;
using System.Data;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Principal;
using System.Security.Permissions;
using System.Web.Security;
using System.Web;
using System.Runtime.CompilerServices;
[assembly:AllowPartiallyTrustedCallers]
namespace NationallyKnownPoliticallyContentiousProjectThatIAmNotAtLibertyToDisclose.data
{
public class ImpersonationManager
{
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool DuplicateToken(IntPtr hToken, int SECURITY_IMPERSONATION_LEVEL, ref IntPtr hDuplicateToken);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool RevertToSelf();
public static string Domain { get; set; }
public static string UserName { get; set; }
public static string Password { get; set; }
private static WindowsImpersonationContext _context;
[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
public static void BeginImpersonation()
{
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_INTERACTIVE = 2;
IntPtr tokenHandle = IntPtr.Zero;
try
{
bool successfulLogin = LogonUser(UserName, Domain, Password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle);
if (successfulLogin)
{
WindowsIdentity identity = new WindowsIdentity(tokenHandle);
_context = identity.Impersonate();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (tokenHandle != IntPtr.Zero)
CloseHandle(tokenHandle);
}
}
public static void EndImpersonation()
{
_context.Undo();
}
}
}
invocation looks something like this:
public static void DeliverOutbound(string document, MessageType messageInfo)
{
WindowsImpersonationContext context = null;
try
{
string filePath = data.StaticDataClassTEMP.getFileSavePath();
string archivePath = data.StaticDataClassTEMP.getArchiveSavePath();
XmlDocument xDocument = new XmlDocument();
xDocument.Load(HttpContext.Current.Server.MapPath("~/data/ApplicationSettings.xml"));
ImpersonationManager.UserName = xDocument.DocumentElement.SelectSingleNode("username").InnerText;
ImpersonationManager.Password = xDocument.DocumentElement.SelectSingleNode("password").InnerText;
ImpersonationManager.Domain = xDocument.DocumentElement.SelectSingleNode("domain").InnerText;
ImpersonationManager.BeginImpersonation();
WriteArchive(document, messageInfo, archivePath);
DeliverZip(document, messageInfo, filePath);
ImpersonationManager.EndImpersonation();
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
}
...and in ApplicationSettings (listing just to be completely thorough for those wanting an example)
<username>processID</username>
<password>processPW</password>
<domain>Corp</domain>
"I need build Skynet. Plz send code"
|
|
|
|
|
it is floyd marshall algorithm but it is run slowly
how can i increase speed?
i have quad pc and it use %25 cpu but i wait so much .
for (int k = 0; k < 2000; k++)
for (int i = 0; i < 2000; i++)
for (int j = 0; j < 2000; j++)
G[i, j] = Math.Min(G[i, j], G[i, k] + G[k, j]);
|
|
|
|