|
I tried. Its not possible from the constructor I create for a value type (declared without new keyword) struct.
Regards,
Lenus.
|
|
|
|
|
Limitation is that structs cannot contain explicit parameterless constructors. But you certainly can use Console.WriteLine in sturct's constructor. See this example:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Test t = new Test(7);
Console.ReadLine();
}
}
public struct Test
{
public Test(int x)
{
Console.WriteLine("I'm Test's constructor and this is my x parameter: "+ x.ToString());
}
}
}
|
|
|
|
|
Yes it is possible only if the struct is instantiated (as if it was an object). Not in the other case when we declare it without the new operator(as if it was a value type variable).
Any suggestions?
Regards,
Lenus.
|
|
|
|
|
It seems as if the constructors don't perform any other work than to assign fields withe values when created as a value type(without new operator).
Regards,
Lenus.
|
|
|
|
|
SPanicker* wrote: Not in the other case when we declare it without the new operator(as if it was a value type variable).
Now I know what you were asking, it was not clear from your initial post. In that case, default parameterless constructor is called and as I said before you cannot declare or override paremeterless constructor.
|
|
|
|
|
Even in this case, the constructor(which i provide) is getting read and the values are assigned.
Only exception is that,I cannot call any other functions from inside that( But values can be assigned to variables ).
I still need two points to be cleared:
As you said if the parameterless constructor cannot be overridden then how is this happening?
If the parameterless constructor cannot be overriden, then why is C# designed to accept such constructors which are of no use?
Thanks.
Regards,
Lenus.
|
|
|
|
|
You declare(override) parameterless constructor? I'm lost here, can you past piece of code with your struct?
|
|
|
|
|
Hi Michal,
Sorry what i meant was, I have a constructor for a (value type - declared without new keyword) struct.
This constructor cannot invoke any inbuilt functions like Console.WriteLine or user defined ones. But at the same time it can do assign values for fields. This seems quite strange.
But, the same things works well(from inside the constructor)if it is declared with the new keyword.
Pls. chk this sample code and tell me if somethngs wrong:
using System;<br />
<br />
struct newStruct<br />
{<br />
public newStruct(int a)<br />
{<br />
Console.WriteLine("In newStruct");<br />
Console.WriteLine("{0}",a);<br />
}<br />
}<br />
struct anotherStruct<br />
{<br />
public int x;<br />
public anotherStruct(int a)<br />
{<br />
Console.WriteLine("In newStruct");<br />
x = a;<br />
Console.WriteLine("{0},{1}",a,x);<br />
}<br />
public void method1()<br />
{<br />
Console.WriteLine(x);<br />
}<br />
}<br />
<br />
class AppEntry <br />
{<br />
<br />
<br />
public static void Main()<br />
{<br />
newStruct abc = new newStruct(10);<br />
anotherStruct def;<br />
def.x= 250; <br />
def.method1();<br />
}<br />
}
Regards,
Lenus.
|
|
|
|
|
Hi Lenus,
there is nothing wrong with your code. In struct newStruct you defined constructor with parameter that is called when you create instance of newStruct: newStruct abc = new newStruct(10);
In struct anotherStruct you also defined constructor with parameter, but this constructor is never called.
anotherStruct def; //here you create object def of type anotherStruct. no costructor is called
def.x = 250; // here you just assign value 250 to variable x, no constructor is called
def.method1(); //this writes 250 to output which is totally correct since you set x to 250 before
Compiler won't compile code if you do not set def.x.
I was probably wrong when I said there is default parameterless constructor. There is no default parameterless constructor at all and you can not declare your own.
|
|
|
|
|
Ok. Thanks for the clarification. But I think this proves there is nothing wrong declaring a value type stuct with a constructor - which obviously of no use and does nothing.
Regards,
Lenus.
|
|
|
|
|
Hi, I'm using the C# webbrowser in order to automate the navigating to a web site, the website is using widgets whichs means a terrible javascript code behind..
For example, a TR that opens a new Tab, looks like this:
"<tr role="wairole:presentation" style="display: table-row;" onmouseup="MU_function(event,this);" onmouseover="MO_function(event,this);" class="folderRow" title="Click here"></tr>"
And I want to say to C# to click this TR in order to open the tab, which means invoking the MU_function(event,this) or maybe MO_function(event,this)then MU_function(event,this).
There's a way of communication between C# and Javascript: by doing something like :
<code>MyWebBrowser.Document.InvokeScript("alert", new string[] { "Hello World" });</code>
by using System.Diagnostics namespace and doing :
[ComVisible(true)]
public partial class MainForm : Form
....
This InvokeScript can also execute a javascript function in the page, so there's a possibility to invoke the MU_function(event,this).. (event: mouseup, this: the TR tag).
Now the problem is: (event,this) have to be JavaScript objects!! How can we do this??
I was thinking for a while, I got an idea: what if we serialize the "event" which is a MouseEvent object and the "this" which is a HTMLElement, then deserializing them to 2 assembly types or whatever, in this case the code should looks like :
MyWebBrowser.Document.InvokeScript("MU_function", new object[] { ObjEvent , Objthis });
Do you know how can we serialize to javascript object ???
I tried the XML one but I don't know how to make them in the function pamaretres!!!
|
|
|
|
|
Hey Guys
i have a lil problem with the following line of code...
txtIp.Text = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].ToString();
on winXP it returns the correct IP Address but on winVista it returns garbage.
if i change the AddressList[] index to 2(3rd position) it returns the correct IP address
if i change the AddressList[] index in winXP to anything other than 0 i get an indexOutOfBoundsException
what is up with that?
why is there more indexes in vista and only one in XP?
any ideas why and how to fix it because the same app will run on xp and vista
ps. both the computers i tested this on is exactly the same from a hardware point of view, only the OS differs, windows XP Pro Service Pack 2 and windows vista business.
thanx
Harvey Saayman - South Africa
Junior Developer
.Net, C#, SQL
think BIG and kick ASS
you.suck = (you.passion != Programming)
|
|
|
|
|
Have you compared the exact result you get in the AddressList array? Just a hunch: Windows Vista has IP v6 support enabled by default, meaning you get separate entries for both the IP v4 stack and the IP v6 stack. You can filter out the version you want by looking at the AddressFamily property for each IPAddress returned by GetHostEntry .
Standards are great! Everybody should have one!
|
|
|
|
|
yes that makes sense!
i fixed the issue like this
private void getIP()
{
System.OperatingSystem osInfo = System.Environment.OSVersion;
switch (osInfo.Version.Major)
{
case 3:
txtIp.Text = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].ToString();
break;
case 4:
txtIp.Text = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].ToString();
break;
case 5:
if (osInfo.Version.Minor == 0)
{
txtIp.Text = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].ToString();
}
else
{
txtIp.Text = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].ToString();
}
break;
case 6:
txtIp.Text = Dns.GetHostEntry(Dns.GetHostName()).AddressList[2].ToString();
break;
default:
txtIp.Text = "Unknown";
break;
}
Harvey Saayman - South Africa
Junior Developer
.Net, C#, SQL
think BIG and kick ASS
you.suck = (you.passion != Programming)
|
|
|
|
|
txtIp.Text = Dns.GetHostEntry(Dns.GetHostName()).AddressList[2].ToString();
You really shouldn't use this, or even AddressList[0] , without checking there actually are enough entries. Also, there's no guarantee the value you want is always in the same position of the array: This depends on the installed hardware, in some cases there might not be even network hardware installed, giving you an uncaught IndexOutOfBounds exception, and on some Windows XP machines IP v6 is actually enabled (you can use it in XP, it's just not enabled by default).
Here's a better way of doing it:
IPAddress[] list = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
foreach (IPAddress a in list)
if (a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
Console.WriteLine(a.ToString());
Standards are great! Everybody should have one!
|
|
|
|
|
thanx, ill try and let you know how it went
Harvey Saayman - South Africa
Junior Developer
.Net, C#, SQL
think BIG and kick ASS
you.suck = (you.passion != Programming)
|
|
|
|
|
tried, tested and APPROVED!
thanx alot dude, that silly case statement irritated me! thats exactly why i posted our solution to the problem, in the hopes of someone giving me a better way
thanx again
Harvey Saayman - South Africa
Junior Developer
.Net, C#, SQL
think BIG and kick ASS
you.suck = (you.passion != Programming)
|
|
|
|
|
You're welcome.
Standards are great! Everybody should have one!
|
|
|
|
|
I've got a snippet of code that was working until recently but now appears to just write the text to disk without encrypting it.
public static void EncryptToFile(string ConfigData, string FilePath)<br />
{<br />
FileStream fStream = File.Open(FilePath, FileMode.OpenOrCreate);<br />
Rijndael myRijndael = Rijndael.Create();<br />
ICryptoTransform encryptor = myRijndael.CreateEncryptor(_key, _IV);<br />
CryptoStream csEncrypt = new CryptoStream(fStream, encryptor, CryptoStreamMode.Write);<br />
StreamWriter sw = new StreamWriter(csEncrypt);<br />
sw.WriteLine(ConfigData);<br />
sw.Close();<br />
csEncrypt.Close();<br />
fStream.Close();<br />
}
the code was almost copied line for line from MSDN and I've seen plenty of examples where people suggest doing something similar.
Does anyone know what i'm doing wrong?
cheers
Russell
|
|
|
|
|
Usually the file path you are looking for is incorrect. Ie the file you are checking is not the file that is being written.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
i'd checked that.
I deleted the file then ran the code from start to finish of the method and can see the file being created in explorer.
Cheers,
Russell
|
|
|
|
|
I suddenly thought that there might be some caching going on as the file being saved was xml and opening in IE.
when i deleted the file on disc and refreshed the file I can still load the page of xml in IE unencrypted.
Notepad tells a different story though.
Cheers for your help
RUssell
|
|
|
|
|
I want to create 5 threads doing five different jobs..
I also want a mechanism to stop any of these threads at any point of time..
What methods do I use?
Do I have to use delegates?
Som
|
|
|
|
|
You can use a thread pool for this matter. You can use thread.Suspend() at any time to kill any of this thread.'
Thanks
Laddie
Kindly rate if the answer was helpful
|
|
|
|
|
Is there a limit to the thread pool??
What if I have say 5000 threads?
Som
|
|
|
|