|
As well as the answer already provided, which will work no problem, you can also look at the CreateInstance method on the Activator class.
The documentation can be found on msdn.
Current blacklist
svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour
|
|
|
|
|
Hi,
As well as the class name, you need to know what assembly the class is contained in. There are different ways of doing this, but just lifting a bit of code from work:
Assembly assembly = Assembly.LoadFile(@"c:\myassembly.dll");
object dynamicClass = assembly.CreateInstance(classNameFromDb);
Not sure that helps as you end up with an untyped object. The way we do it here is to make the objects we create dynamically implement an interface, so the second line actually looks more like
IDynamicObject dynamicClass = assembly.CreateInstance(classNameFromDb) as IDynamicObject;
This way, we can then call the methods the interface specifies.
After all, creating an instance of a class is little use if you have no idea what the object does. This is why defining an interface is useful.
And don't forget the gotcha, the trap I always fall in with this, its not just the type name you need, but also the namespace. eg. "MyNamespace.MyClass" which you need to pass to CreateInstance.
Regards,
Rob Philpott.
|
|
|
|
|
Hey Everyone,
I'm writing an app to test whether a serial port on a computer will work correctly and I'm using the principle of serial loopback.
I'm using the SerialPort class from System.io.ports and using a serial port object to write to the port and then listen to see if there was any data recieved. However, I always get a timeout for the recieve portion, even when I put the recieve method into a Thread.
Any ideas what I should try? Thanks in advance
-Karthik
|
|
|
|
|
Karthik Gaekwad wrote: I'm using the principle of serial loopback
I don't think you can use a principle I think you have to actually have a loopback.
"Just about every question you've asked over the last 3-4 days has been "urgent". Perhaps a little planning would be helpful?" Colin Angus Mackay in the C# forum
led mike
|
|
|
|
|
led Mike,
You are correct. I am using loopback to solve the problem, but my recieve thread won't fire, even if I have the recieve wait for a message.
Hope that clears it up. Any suggestions?
-Karthik
|
|
|
|
|
And the solution is....
After you write to the port using a serial port object, use the readExisting method. DO NOT CLOSE THE PORT and then OPEN the port again to read. There is nothing stored in the buffer at that point.
I can be so dumb sometimes. That's what I get for trying to modularize my program.
|
|
|
|
|
Nice work, thanks for posting the solution.
|
|
|
|
|
Hello,
Directory.Exists(“\\localhost\c$”) and Directory.Exists(“\\localhost\SomeLocalShare”) and Directory.Exists(“\\127.0.0.1\c$”) and Directory.Exists(“\\127.0.0.1\SomeLocalShare”) all stop returning true once the network goes down. I know this seems obvious but how is it I can still resolve the local instance of SQL server? My question is how do I resolve a UNC name (assuming it is localhost) if the network is down, disabled, or non existent on Windows Xp pro and Better?
I was thinking of a virtual port maybe or a virtual Nic.
Any help will be very much appreciated
Ronald Hahn, CNT - Computer Engineering Technologist
New Technologies Analyst
HahnTech Affiliated With Code Constructors
Edmonton, Alberta, Canada
Email: rhahn82@telus.net
|
|
|
|
|
Okay, maybe this is really a silly question but why do you need to resolve a UNC to the localhost/local system? I'm trying to figure out what purpose this would serve and for the life of me I don't get it.
Here's why I'm a bit lost: The localhost/local system is a known quantity and therefore you can write your code to use local system drive paths for accessing any information.
Mike Poz
|
|
|
|
|
I’m writing a tablet app that has start up parameters in a Db. Some of the parameters are UNC paths to data libraries that may or may not be local. Some tables will have a constant connection to the LAN, others will be disconnected piroticly. I’ll know in advance the ones that are mobile and set the data stores to be local in the Db. I want to use the same logic in both connected and disconnected environments. (I’m able to assume that the app was started with either a connection to the Db or a copy of the data needed locally) Also how Universal is a naming convention if it Only works in a connected environment. In the real world 25 drives are not enough. I think the whole industry should do away with the idea of local letters and move completely to UNC \\Device\share\path.
One idea I had was to find where windows holds its share info and find the local resource that way. In the db I’d have \\computerName\share so I could parse it out and compare to this list. In computer management Win still know what it is sharing even with no NIC
Ronald Hahn, CNT - Computer Engineering Technologist
New Technologies Analyst
HahnTech Affiliated With Code Constructors
Edmonton, Alberta, Canada
Email: rhahn82@telus.net
|
|
|
|
|
HahnTech wrote: One idea I had was to find where windows holds its share info and find the local resource that way.
That's maintained in the registry under HKLM here:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\Shares
You'll find keys with each share name, and their data value contains permissions and local path to the share.
This assumes your use account has the permissions to at least read this information.
HahnTech wrote: I’ll know in advance the ones that are mobile and set the data stores to be local in the Db.
If you will know in advance what is local and what is remote, then it sounds to me like you need to have some error checking with some "if - else" blocks to deal with when your "directory.exists()" fails to return true.
If(Directory.Exists(\\localhost\c$)
{
Connect(\\localhost\c$\datbase);
}
else
{
//try a possible local path using C:\
or
//pop an error stating that the database is unavailable
}
Either way this is basically an error handling issue where you need to fail gracefully when your "directory.exists" returns false.
Mike Poz
|
|
|
|
|
Thanks for the Reg Keys. I'll look into that solution.
This is not an error handling problem. This is a windows problem with not Fully utilizing UNC. There is no way to convert the parts of a UNC name into a local path. A properly normalized db will have separate columns for device, share, and path (UNC: \\DeviceName\ShareName\Path) so its not as simply as If UNCpath exists Else just use local. That would require restructuring the Db. And as you probably know that’s not always an option.
Ronald Hahn, CNT - Computer Engineering Technologist
New Technologies Analyst
HahnTech Affiliated With Code Constructors
Edmonton, Alberta, Canada
Email: rhahn82@telus.net
|
|
|
|
|
Hi,
Is it posible to implement a class inheritet from eg. int string .. types
thx
|
|
|
|
|
int and string are both sealed. You cannot inherit from a sealed class.
You could try implementing a class to wrap an int or a string though that might meet you needs.
|
|
|
|
|
|
string-> NO
public sealed class String
int (Int32) is a struct. Does not support inheritance
Sincerely,
Elina
Life is great!!!
Enjoy every moment of it!
|
|
|
|
|
Why on earth would you want to?
Try code model generation tools at BoneSoft.com.
|
|
|
|
|
how can i have ellips form
MD_NADA
|
|
|
|
|
|
Hi everyone!
I've got a problem, coding in C#!
I have to create some PictureBox controls at runtime and the number of these will be specified at runtime too. Next, I should be able to target them by the name.
I mean I have to be able to generate the names as I need, at runtime, something like this:
<br />
for(int x = 0; x < xCount; x++){<br />
for(int y = 0; y <yCount; y++){<br />
public PictureBox "PictureBoxX"+x.ToString()+Y+Y.ToString() = new PictureBox();<br />
}<br />
}<br />
and then I should be able to access the control by its name and also be able to assign its properties!
There is a function in Javascript and Actionscript that make the object accesible at runtime by generating its name:
<br />
piece3 = "dangerous";<br />
x = 3; <br />
y = eval("piece" add x);<br />
trace(y);
or
<br />
name1 = "mike";<br />
name2 = "debbie";<br />
name3 = "logan";<br />
for(i = 1; i <= 3; i++) {<br />
trace (eval("name" add i));
but I couldn't find such a thing in C#!
Please help me with this! ((
Sojaner!
|
|
|
|
|
Would something like this help:
PictureBox temp = new PictureBox();
temp.Name = "PictureBoxX" + x.ToString();
MyForm.Controls.Add(temp);
Then to use your PictureBox:
PictureBox temp = (PictureBox) MyForm.Controls["PictureBoxX" + x.ToString()];
This will give you a mechanism for retrieving your PictureBoxes using a string, but it's not quite the same as eval in JavaScript, but it's probably safer.
-- modified at 17:28 Tuesday 27th June, 2006
|
|
|
|
|
Thanx a million, taking time to answer!
I've not tested it yet, but I'm almost sure it would work.
Sojaner!
|
|
|
|
|
Why do you think that you have to access the references to the picture boxes by name?
C# is not a script language, so there are no dynamic creation of variables. Use an array, a list, or a dictionary.
---
b { font-weight: normal; }
|
|
|
|
|
Thanx a million, taking time to answer!
Do you mean using a 2 dimention array of PictureBox controls?
And you mean, when coding in C#, from the beginning, you have to plane every object and other things and there is no way to decide what to do at runtime, like scripting languages?
|
|
|
|
|
Sojaner wrote: Do you mean using a 2 dimention array of PictureBox controls?
How you store the references depends on how you want to access them. If they are arranged in rows and columns in the form doesn't mean that you have to use a two dimensional array to keep track of them.
And you mean, when coding in C#, from the beginning, you have to plane every object and other things and there is no way to decide what to do at runtime, like scripting languages?
Yes. I have never really seen that as a limitation, though.
There are a lot of neat data structures like arrays, lists, linked lists, sorted lists, dictionaries, sorted dictionaries, queues and stacks that you can use to store data. A dictionary actually resembles what a script language uses to handle all variables.
I've built a lot of rather advanced web sites in VBScript too, and never needed to create variables dynamically there either. I believe that the Eval command is pretty much like the Goto command; it's never really needed, and your code gets better if you avoid it.
---
b { font-weight: normal; }
-- modified at 15:41 Wednesday 28th June, 2006
|
|
|
|