|
To access the value there, you need to use the 'as' keyword, as in
Parent p = this as Parent;
If p != null, then the object was indeed of the parent type, and you can then use the p object to access public properties ( and, I assume, protected ones )
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
But wouldn't that violate OOP principles? (The parent knowing too much about the child.)
Wouldn't the "proper" way be for the parent to specify that the child must have a particular method?
class Parent
{
protected abstract string Value{get;} ;
public override string ToString()
{
return ( Value ) ;
}
}
class Child : Parent
{
string value="asdf";
protected override string Value
{
get { return ( this.value ) ; }
}
}
(I hope that's correct.)
|
|
|
|
|
yes, you're right. The 'proper' was is definately for the property to be defined in the base method. But, if that's not possible, and if there's no way around it, what I described is the way to achieve what was asked for.
Another way would be to define the property through an external interface, which is implimented by the derived class, and which is used in the 'as' call in the base class.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I don't even think that would work, Parent doesn't declare value. And the further problem would be if a child declares value as some unexpected type or not at all.
The base class must either include the declaration of value or specify an abstract method either directly or via an interface.
|
|
|
|
|
Have anyone used ngen.exe to generate native images of an assembly? I need to know if it has real utility...
Any sample will be welcome. Thanks in advance.
Visit my blog at http://dotnetforeveryone.blogspot.com/
|
|
|
|
|
Yes, I've done it before. It can speed up cold boots of your app significantly and warm boots somewhat. You can read more articles about it on MSDN. Here's one[^] for you.
|
|
|
|
|
How can I get the collection of field values from the below class.
So consider this instance of SentRecord
SentRecord sr=new SentRecord(1,new Subscriber(), DataTime.Now, false, "C:\img.jpg", 0);
Now how can I get all fields and values from sr. e.g.:
SentRecordID => 1
ContentCampaign => [ContentCampaign object]
Subscriber => [Subscriber object]
TimeSent => [DateTime object]
Confirmed => false
FileSent => "C:\img.jpg"
AdIDDelivered => 0
public class SentRecord : Model
{
public SentRecord(int contentID, Subscriber subscriber, DateTime timeSent, bool confirmed, string fileSent, int adIDDelivered)
{
this.ContentCampaign.ContentID = contentID;
this.Subscriber = subscriber;
this.TimeSent = timeSent;
this.Confirmed = confirmed;
this.FileSent = fileSent;
this.AdIDDelivered = adIDDelivered;
}
public int SentRecordID;
public ContentCampaign ContentCampaign;
public Subscriber Subscriber;
public DateTime TimeSent;
public bool Confirmed;
public string FileSent;
public int AdIDDelivered;
}
/\ |_ E X E GG
|
|
|
|
|
Look for these classes in MSDN.
1. System.Type
2. System.Reflection.FieldInfo.
Call GetField() method of your type and GetValue() method of FieldInfo of your members.
Farhan Noor Qureshi
|
|
|
|
|
I'm having trouble using GetValue()
Type t = this.MetaClass.modelObject.GetType();
this.MetaClass.TableName = t.FullName.Substring(t.FullName.LastIndexOf(".")+1);
FieldInfo []fields=t.GetFields();
this.MetaClass.fields =new string[fields.Length];
this.MetaClass.values = new string[fields.Length];
for (int i = 0; i < fields.Length; i++)
{
this.MetaClass.fields[i] = fields[i].Name;
}
I'm able to get all the field names, but I can't get the values of the fields. See the commented line.
Any ideas?
/\ |_ E X E GG
|
|
|
|
|
 Hi
Try the following example that iterate over all the fields of an object and print the field name and value
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace Test
{
public class CustomizeField
{
public CustomizeField(int x, int y)
{
m_x = x;
m_y = y;
}
public int m_x;
public int m_y;
public override string ToString()
{
return string.Format("X:{0}, Y:{1}", m_x, m_y);
}
};
public class SentRecord
{
public SentRecord(
DateTime timeSent,
bool confirmed,
string fileSent,
int adIDDelivered)
{
this.customizeField = new CustomizeField(10, 15);
this.TimeSent = timeSent;
this.Confirmed = confirmed;
this.FileSent = fileSent;
this.AdIDDelivered = adIDDelivered;
}
public CustomizeField customizeField;
public DateTime TimeSent;
public bool Confirmed;
public string FileSent;
public int AdIDDelivered;
}
class Program
{
static void Main(string[] args)
{
SentRecord sr = new SentRecord(DateTime.Now, true, @"c:\image.jpg", 10);
FieldInfo[] fields = sr.GetType().GetFields();
foreach (FieldInfo fi in fields)
{
Console.WriteLine(fi.Name + " " + fi.GetValue(sr).ToString());
}
}
}
}
|
|
|
|
|
Hi, how come GetFields doesn't pick up any public properties?
/\ |_ E X E GG
|
|
|
|
|
Hi All,
I am working on an application in which there is a toolstrip containing two ToolStripSplitButton. If I click on the button the drop-down comes down and when re-click duration is more than .5 seconds approx then re-click doesnot closes the drop-down. Also in another toolstrip when one button is checked taking mouse over to another button makes the MouseOver bordering and Tooltip fliker.
If anyone is having any idea please let me know. Thanks.
|
|
|
|
|
Have you looked at any of the C# graphing libraries out there? They generate 3d-like shapes using GDI/System.Drawing. Some of them are open source. I believe this site has some articles on graphing and charting as well.
p.s. next time use <pre> tags around your code. It preserves formatting and is easier to read.
|
|
|
|
|
I want a bidirectional way for cryptography in c#.
(not like MD5 unidirectional!)
|
|
|
|
|
MD5 is not an encryption method, it's a hashing method. Any encryption method is bidirectional.
You find the encryption methods in the System.Security.Cryptography namespace.
---
single minded; short sighted; long gone;
|
|
|
|
|
could you say the Methods?
|
|
|
|
|
DES, DSA, RC2, Rijndael, RSA, TripleDES
---
single minded; short sighted; long gone;
|
|
|
|
|
 MSDN has several good samples. Here is one.
using System;
using System.Security.Cryptography;
using System.Text;
class RSACSPSample
{
static void Main()
{
try
{
UnicodeEncoding ByteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");
byte[] encryptedData;
byte[] decryptedData;
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
encryptedData = RSAEncrypt(dataToEncrypt,RSA.ExportParameters(false), false);
decryptedData = RSADecrypt(encryptedData,RSA.ExportParameters(true), false);
Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));
}
catch(ArgumentNullException)
{
Console.WriteLine("Encryption failed.");
}
}
static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
{
try
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.ImportParameters(RSAKeyInfo);
return RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
}
catch(CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
}
static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo,bool DoOAEPPadding)
{
try
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.ImportParameters(RSAKeyInfo);
return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
}
catch(CryptographicException e)
{
Console.WriteLine(e.ToString());
return null;
}
}
}
Farhan Noor Qureshi
|
|
|
|
|
i have added control at runtime in .aspx page using c#. the dyamic controls are textbox and literal controls. as per the design issue i have added all thes controls in a html table in which i have added dynamic rows as per requirement. so table > dyamic row>panel>table with the controls.
so now i cant find the controls as the dynamic child level is three...
Please help me out...
Thanx..
Jayesh Talsaniya
|
|
|
|
|
Why would you have to find the controls? Just keep the references from when you create the controls.
---
single minded; short sighted; long gone;
|
|
|
|
|
When you create a control dynamically, assign it a unique name. Now when you need to refer to them you can call something like this:
FindControl("<your unique control name level 1>").FindControl("<your unique control name level 2>").FindControl("<your unique control name level 3>")
Farhan Noor Qureshi
|
|
|
|
|
Q #1:
I have two buttons to zoom in and zoom out the text size of a RichTextBox. Is it possible to increment/decrement the ZoomFactor by 0.1F each time?
Q #2:
Does changing the ZoomFactor also change the font size when you save the file as .rtf?
Thanks,
Mark
|
|
|
|
|
Hi,
Q1.
from reading the documentation it is not completely clear, so an experiment may be in order.
It says it accepts numbers from 1/64 to 64
That could mean all fractions 1/n and all integers n with 1 <= n <= 64
Q2.
I doubt that very much; normally "zoom" indicates a visualization property, not a
content property. Try it !
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Thanks for the tips. I will have to make a small test form and see what happens.
Mark
|
|
|
|
|
Yes it is Possible
No it does not any effect on the saved File
|
|
|
|