|
Try google. Copy and paste your first question into it's text box, and press search. It finds your answer, and even offers to correct your spelling at the same time!
Repeat.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
"Rumour has it that if you play Microsoft CDs backwards you will hear Satanic messages.Worse still, is that if you play them forwards they will install Windows"
|
|
|
|
|
I have an object named 'dv'
'dv' has a string property named 'refNumber'
There is also a string 'altString' = "refNumber"
Is there a way to use 'altString' to get that property from 'dv'?
string newString = dv.??altString??
-- should set newString the same as --
string newString = dv.refNumber
Thanks for your suggestions.
|
|
|
|
|
Reflection can do this. Use System.Reflection name space. You'd do something like (not tested)
PropertyInfo property = (PropertyInfo) dv.GetMembers().SingleOrDefault(info =>
info.MemberType == MemberTypes.Property &&
info.Name == altString);
string newString = string.Empty;
if (property != null)
newString = (string) property.GetValue(dv, null);
Best wishes,
Navaneeth
|
|
|
|
|
Thank you. Please forgive my ignorance, but where does 'refNumber' occur in your reply?
|
|
|
|
|
BDJones wrote: but where does 'refNumber' occur in your reply?
Quote Selected Text
You said it will be in altString variable. I am using that variable to compare against name. You can replace it with "refNumber" if you wish to.
Best wishes,
Navaneeth
|
|
|
|
|
|
|
|
Hi all,
how we can use the enum any simple Example of using the enum?
enum is data type or object?
Hope hear from you
Best Regards
|
|
|
|
|
Rameez Raja wrote: any simple Example
public enum Response
{
No
,
Yes
}
return Response.Yes ;
Rameez Raja wrote: enum is data type or object?
Read up on it[^] and then ask any specific questions you have.
|
|
|
|
|
Hi,
i can able to insert random letters in TableLayoutPanel with this code
private void form_Load(object sender, EventArgs e)
{
for (int a = 0; a < tableLayoutPanel1.ColumnCount; a++)
{
for (int b = 0; b < tableLayoutPanel1.RowCount; b++)
Random r = new random();
lable nl = new lable();
int x = r.nest(65,90);
char c = (char)x;
nl.text = c.tostring();
tableLayoutPanel1.Controls.Add(nl, a, b)
}
}
}
i couldn't able to insert chosen words into TableLayoutPanel .pls help me.
how to change the color of letters when they click answer.
thanks in avance.
|
|
|
|
|
Hi,
please use PRE tags to show code.
and show real code, something that might be acceptable to a compiler.
your code is filled with minor mistakes.
also don't create new Random() instances all the time, use one Random() over and over.
Add a Click handler to each Label, so you can change its color; you can reuse the handler for all Labels, and use its sender to identify which Label got clicked.
From what you show and ask, I expect you are pretty new to C#, and would benefit from studying a C# book to properly learn the basics.
|
|
|
|
|
Why does it call two ReferenceEquals methods when only one is needed? I have tried removing the one with null as a parameter and it works exactly the same. I don't understand its purpose.
return !ReferenceEquals(null, obj) &&
(ReferenceEquals(this, obj) || obj.GetType() == typeof(Foo) &&
Equals((Foo)obj));
|
|
|
|
|
When you override Equals() , you are saying that "your class objects has value equality rather than reference equality". The suspected code here is doing just a null check before doing the value equality. Calling members on a null reference will lead to exception. It can't just use if(obj == null) because if operator == is overloaded on the type and it calls Equals to do the equality, you will end up in infinite loop. Usually it is a recommended practice to overload operator == if you have overridden Equals and call Equals from operator overload.
Having said that, resharper is stupid in generating Equals and GetHashCode . You should never rely on the code what it generated. Resharper includes mutable objects in the hashcode calculation which will lead into severe issues.
Best wishes,
Navaneeth
|
|
|
|
|
I'm trying to:
1st Create an array containing ref's to labels.
2nd Initiate them with a for loop and add some text to the label.
3d Add them to a panel with controls.add() either in the same loop or afterwards
The problem:
My code below sort of works but it will just add 1 control instead of 4 in my dummy example... what I'm I missing here??
I am aware I'm not setting any location Point prop's in the code below but im sure that isnt the problem...
private void btnExampleClick(object sender, EventArgs e)
{
Label[] lbl = new Label[4];
for (int i = 0; i < 4; i++)
{
lbl[i] = new Label();
lbl[i].Text = i.ToString();
}
foreach (Label x in lbl)
{
Controls.Add(x);
}
}
|
|
|
|
|
Hi,
welcome to CodeProject.
please use PRE tags when showing code, or select the code and use the "code block" widget.
why are you having a separate loop to add the labels to the Controls, you could do that in the first loop.
You did add 4 labels, however you need to set the label's Location, they are all on top of each other.
|
|
|
|
|
I am having a hard time figuring this out. My applications is using WMI to get information about a remote computers disk drives, installed applications, and services (all of this works fine). I can seem to get all this information but I cannot get the computer to stop a remote service.
I keep getting "RPC server is unavailable". How is this possible? I am able to retrieve information from these computers so it can't be unavailable.
At first I kept getting 'Access Denied' errors. I was trying to make it use the windows username and passwords somehow instead of having to enter a username and password.
So now I'm doing this:
ConnectionOptions conn = new ConnectionOptions();
conn.Impersonation = ImpersonationLevel.Impersonate;
conn.Username = "DOMAIN\username";
conn.Password = "password";
conn.EnablePrivileges = true;
Like I said.. I can connect and get all the information in the world I want, but cannot seem to stop a service. I read this other article about settings permissions and I have already tried that by going through computer management on the server.
Oh and by the way I am using the top domain admin user to do this.. so there is not another user in this domain that has more rights.
If you are interested in the whole code it is here: (Thanks to (Windows Management Instrumentation (WMI) Implementation[^]) for most of it)
ServiceInfo service = (ServiceInfo)serviceInfo;
ManagementOperationObserver observer = new ManagementOperationObserver();
MyHandler completionHandlerObj = new MyHandler();
observer.ObjectReady += new ObjectReadyEventHandler(completionHandlerObj.Done);
try
{
ConnectionOptions conn = new ConnectionOptions();
conn.Impersonation = ImpersonationLevel.Impersonate;
conn.Username = "DOMAIN\username";
conn.Password = "password";
conn.EnablePrivileges = true;
ManagementScope scope = new ManagementScope("\\\\" + service.DNSHost + "\\root\\CIMV2", conn);
scope.Connect();
ManagementObjectSearcher mos = new ManagementObjectSearcher(scope, new ObjectQuery("SELECT * FROM Win32_Service WHERE DisplayName='" + service.item.Text + "'"));
foreach (ManagementObject mo in mos.Get())
{
bool Started = Convert.ToBoolean(service.item.SubItems[1].Text);
if (Started)
mo.InvokeMethod(observer, "StopService", null);
else if (!Started)
mo.InvokeMethod(observer, "StartService", null);
int intCount = 0;
while
(!completionHandlerObj.IsComplete)
{
if (intCount > 10)
{
MessageBox.Show("Terminate process timed out.", "Terminate Process Status");
break;
}
System.Threading.Thread.Sleep(500);
intCount++;
}
if (completionHandlerObj.ReturnObject.Properties["returnValue"].Value.ToString() == "0")
{
if (!Started)
{
service.item.SubItems[1].Text = "True";
service.item.SubItems[1].BackColor = Color.LimeGreen;
service.item.SubItems[1].ForeColor = Color.Black;
}
else
{
service.item.SubItems[1].Text = "False";
service.item.SubItems[1].BackColor = Color.Red;
service.item.SubItems[1].ForeColor = Color.White;
}
}
else
{
MessageBox.Show("Failed.", "Start/Stop Service Failure");
}
}
}
catch (Exception ex) { MessageBox.Show("Error starting/stoppping service.\n\n" + ex.Message); }
finally
{
Invoke((Action)(() => { picProgressServices.Visible = false; }));
}
}
|
|
|
|
|
I guess I could always use a ServiceController. I was able to get taht to work great. Would still like to understand more why the wmi is being so difficult
|
|
|
|
|
How can I use the mouse event to move object
|
|
|
|
|
search CodeProject articles for movable controls
|
|
|
|
|
Whenever I try to add new functions or method via Class Diagram,
VS always insert this code:
throw new System.NotImplementedException();
1. What kind of exception is this and what is it's use?
2. How can I configure VS to not generate this code?
Thanks a lot.
|
|
|
|
|
The exception does exactly what it says on the tin. It is used where a method that should be implemented isn't, presumably so that you don't forget to implement the method. I don't know if you can change a setting to make it not show up, in my opinion, doing so could make it very easy to forget to implement a function, leading to potentially hard to diagnose bugs.
|
|
|
|
|
So I will just have to delete it if I'm going to right code inside the function?
|
|
|
|
|
Unless you want your code to throw a NotImplementedException, yes.
|
|
|
|
|
Sorry, I'm kinda new to the term. What do you mean by "implementing the function", using it in a program or adding functionality in it?
|
|
|
|