|
When I have the following definitions:
public class CMsg
{
protected int m_Id;
}
public class CMsgTx : CMsg
{
public string m_Dst;
}
To be used by:
public abstract class CNet
{
abstract public int SendMsg(ref CMsg oMsg);
}
public class CNetSend : CNet
{
override public int SendMsg(ref CMsg oMsg)
{
CMsgTx oTxMsg = oMsg as CMsgTx;
return 1;
}
}
I getting compiler error when trying to call SendMsg:
{...
CMsgTx oMsg = new CMsgTx();
CNetSend oSender = new CNetSend()
oSender.SendMsg(ref oMsg);
...}
My question is how to convert CMsgTx.oMsg to CMsg when calling SendMsg,
I thought that the compiler would do it...
|
|
|
|
|
You can't do that with ref parameters.
Why not? Simple: you could replace the instance you pass in with a completely different type also derived from the same base type - which could mean that the outside world ends up with a CMsgTx variable referencing a CMsgBananaSplit instance - which doesn't support half the properties of CMsgTx, but does support the IceCream property and the GetNuts method - and so forth.
You can only pass a reference through if it is of the exact same type as the methdo declaration, not a derived type at all.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
What about out parameters,
in case of:
public abstract class CNet
{
abstract public int SendMsg(out CMsg oMsg);
}
public class CNetRx : CNet
{
override public int SendMsg(out CMsg oMsg)
{
CMsgTx oTxMsg = new CMsgTx();
oMsg = (CMsg)oTxMsg ;
return 1;
}
}
and the program:
CMsg oM;
CNetRx oNetRx = new CNetRx();
oNetRx.SendMsg(out oM);
CMsgTx oMsgTx = oM as CMsgTx;
is there also loss of information?
Do you suggest other 'design' for CNet and CMsg classes?
|
|
|
|
|
No - because what you pass through is a valid CMsg - it's derived from it, and the variable you pass into is a CMsg, not a derived class. So to use it as an instance of CMsgTx you have to cast it - as your code does - and a derived class instance that doesn't include CMsgTx will not cast: the as will return null.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Why does it need to be a ref parameter?
namespace ConsoleApplication379
{
class ReferenceTest
{
public string Bla { get; set; }
}
class Program
{
static void Main(string[] args)
{
var a = new ReferenceTest() { Bla = "Test" };
change(a);
Console.WriteLine(a.Bla);
Console.ReadLine();
}
static void change(ReferenceTest whichOne)
{
whichOne.Bla = "Hello World";
}
}
} I'm asking because it makes more sense to use on a value-type, not on a pointer.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Why MVC best from Asp.Net while less work in asp.net as compare to MVC.
|
|
|
|
|
Did you read your question before posting? It's a good practice...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
Sorry , I got answer of my question.
|
|
|
|
|
It depends what you are doing - to hack together a simple ASP.NET winforms style web app, then asp.net is probably going to be easier. Where MVC wins is where you actually want to create websites, or sites with more modern features (for example AJAX calls, exposing a REST API), then MVC3 wins IMO.
The other thing you have to take into account is the learning curve: if you've been an asp.net "forms" developer for 2 years and you switch to MVC, then you're already 2-years behind more or less, and you have to stop using a lot of techniques you've become used to.
So the answer is, if you've gained a similar amount of experience/expertise in both technologies and one has taken more effort than the other one, you've chosen the wrong tech.
|
|
|
|
|
Thanks a lot ,
|
|
|
|
|
What is Extension Method in C# and when i can use it ?
|
|
|
|
|
|
An extension method is a piece of code that allows you to extend a type with custom functionality. This feature was introduced alongside LINQ. Suppose you wanted to add the ability to check whether or not an integer was within a certain range, you would declare a static method inside a static class (this is required for extension methods), that passed in the parameter in a special way. Here's this example
public static class IntegerExtensions
{
public static bool Between(this int value, int startRange, int endRange)
{
return value >= startRange && value <= endRange;
}
} To call this you would do something like this:
int checker = 10;
if (checker.Between(0, 50)
{
Console.WriteLine("I'm in the range");
} Now, if you want to use the extension method, you have to make sure that the namespace for the static class is included in the file that you are calling the extension from. Also, there's nothing to stop you writing this code as well
if (IntegerExtensions.Between(checker, 0, 50)) Ultimately they resolve to the same thing.
|
|
|
|
|
What sort of pond life would downvote such an excellent answer to a question? Compensation provided.
|
|
|
|
|
Thanks. I have to admit, I was surprised by the vote as I thought I'd given a clear enough start to get someone on the way with extension methods. Oh well
|
|
|
|
|
Hi
I have about 1000 2d points that I draw a ClosedCurve on bitmap then on picturbox, but I want saving a few points from this 1000 points that are necessery to drawing previous ClosedCurve or shape.
Thanks
|
|
|
|
|
What have you tried, where exactly are you stuck?
What do you mean when you say saving? When will you draw that previous ClosedCurve? When the application restarts? When the user hits the undo button? On the whim of a butterfly flapping its wings ?
Give us more info, so we better understand the question.
|
|
|
|
|
thanks, suppose we have an image that contain a shape like "s", this "s" formed from thousands points or pixels. well, we want drawing a shape like "s" whit .DrawCurve method in C# but we don't want .DrawCurve accept that thousnds parameter(or points). we want detect a few number of point that are critical(in mathemathics) or necessary points then draw same shape("s"). of course if we detect this points we can use DrawLine or DrawBezier and etc methods to draw same shape("s").
|
|
|
|
|
how to read .Zim file in C#
|
|
|
|
|
Well, you're probably going to need to write something that implements the .ZIM file format[^]. There is no default implementation in the BCL.
|
|
|
|
|
I am able to create a XML file with:
="1.0"="ISO-8859-1"
with the following codes:
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.Encoding = Encoding.GetEncoding("ISO-8859-1");
XmlWriter writer = XmlWriter.Create("C:\\XML files\\XML_File.xml", settings);
How can I add standalone="no" after encoding="ISO-8859-1" as
="1.0"="ISO-8859-1"="no"
Thanks for any help
|
|
|
|
|
|
When I use the link and set:
writer.WriteStartDocument(true) ; then I have
="1.0"="ISO-8859-1"="true"
However when I set:
writer.WriteStartDocument(false) ; then I have
="1.0"="ISO-8859-1"
*) What I need as mentionned before:
="1.0"="ISO-8859-1"="no"
Thanks for help, we are close!
|
|
|
|
|
|
Thanks for the link, it is OK if I viewing my xml file with Notepad!
|
|
|
|