|
|
nope, both solutions have encoded text!
|
|
|
|
|
Hi,
Quick and simple question
Is there a simple way to sync a sql2005 db with a sqlCe database on a mobile device.
Is there a default path on a windows mobile device where i can connect to (c:\....) ?
I need a fixed path on the mobile device to push the updates to.
Thanks for the advice.
grtz
Wouter
|
|
|
|
|
Let's say you define this class (at the namespace level; i.e., outside
a form or whatever)
public class anyDamnList<T> : List<T>
{
public anyDamnList() { }
}
And you declare and use it ... in a form's code ... with no problems :
anyDamnList<int> aYIntList = new anyDamnList<int>();
aYIntList.Add(100);
aYIntList.Add(200);
Console.WriteLine(aYIntList[1]);
List<int> intList3 = new List<int> { 9, 19, 22, 44 };
aYIntList.Clear();
aYIntList.AddRange(intList3);
Console.WriteLine(aYIntList[3]);
Here's the question : is there any other way than using 'Clear() and then 'AddRange
to replace the "virtual inner list" <T> "embodied" via inheritance in the class ?
1. you cannot assign to an instance of the class : aYIntList = intList3;
You get this error : "Error Cannot implicitly convert type
'System.Collections.Generic.List<int>' to
'WindowsFormsApplication1.anyDamnList<int>'. An explicit conversion
exists (are you missing a cast?_
2. you can't cast like this : aYIntList = (anyDamnList<int>) intList3
: that will NOT give you a compile time error but at run-time you get
: ""Unable to cast object of type
'System.Collections.Generic.List`1[System.Int32]' to type
'WindowsFormsApplication1.anyDamnList`1[System.Int32]'."}
3. I played around with Linq's OfType<> and Cast<> operators
aYIntList = intList3.OfType<int>();
aYIntList = intList3.Cast<int>();
Both of those will throw compile time errors like so : "Cannot
implicitly convert type 'System.Collections.Generic.IEnumerable<int>'
to 'WindowsFormsApplication1.anyDamnList<int>'. An explicit conversion
exists (are you missing a cast?)"
"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844
|
|
|
|
|
You could write an implicit (or explicit) conversion operator.
Or write a ClearAndAddRange method.
Edit:
OK, a conversion operator won't work:
user-defined conversions to or from a base class are not allowed
and
user-defined conversions to or from an interface are not allowed
But a copy constructor will work:
public anyDamnList
(
System.Collections.Generic.IEnumerable<T> Source
)
{
this.AddRange ( Source ) ;
return ;
}
anyDamnList<int> ai = new anyDamnList<int> ( intList3 ) ;
modified on Sunday, December 13, 2009 10:54 AM
|
|
|
|
|
Hi PieBaldConsult,
Well, I thought I was writing 'implicit and/or 'explicit conversion operators in the attempts I documented in the question that result in compile-time and run-time errors.
And, while, yes, I could write a ClearAndAddRange method : the whole purpose of the question is part of my "self-education" in Linq which (for me) is best done by posing problems and then trying to solve them.
I have the feeling that somewhere there's a way to use some of the Linq commands like ConvertAll, Cast, OfType that will do the "right thing" easily. to get from one List<int> instance into a form that can be assigned to an instance of 'anyDamnList.
After all, if you build the example and type instanceOfAnyDamnList. ... you get the intellisense you expect including methods like 'Count.
This is the kind of question that Jon Skeet could answer in his sleep
Thanks for responding !
best, Bill
"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844
|
|
|
|
|
|
Please don't bother to respond further; wasting your time is one thing; wasting my time is another.
Bill
"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844
|
|
|
|
|
|
If you add a constructor that takes an IEnumerable and call the base constructor then you can just assign the List as a new instance. It doesn't exactly answer your question but gives you a workaround...
public class AnyDamnList<T> : List<T>
{
public AnyDamnList()
: base()
{ }
public AnyDamnList(int capacity)
: base(capacity)
{ }
public AnyDamnList(IEnumerable<T> collection)
: base(collection)
{ }
}
AnyDamnList<int> anyDamnList = new AnyDamnList<int>((new List<int>() { 1, 2, 3 }));
List<int> list = new List<int>() { 1, 2, 3 };
anyDamnList = new AnyDamnList<int>(list);
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Hi Dave,
Thanks for the responsible answer. I am using C#4, by the way.
I'm familiar with using automatic properties, and automtatic property initializers.
What I am curious about here is that :
1. once you've defined an instance of AnyDamnList<sometype> : you can type in the instance name followed by a dot and get all the intellisense you'd expect for an instance of List<sometype> including "non-generic" properties like 'Count. You've got access to methods like 'Add, 'AddRange, GetRange, indexing with [] : in other words your instance "embodies" a List<t>.
2. using your class definition above you still cannot assign another List<int> to an instance of AnyDamnList<int> directly :
anyDamnList2<int> aYIntList = new anyDamnList2<int>();
aYIntList.Add(100);
aYIntList.Add(200);
Console.WriteLine(aYIntList[1]);
List<int> intList3 = new List<int> { 9, 19, 22, 44 };
aYIntList = intList3;
Console.WriteLine(aYIntList[3]);
When you get the error at compile time : "Cannot implicitly convert type 'System.Collections.Generic.List<int>' to 'WindowsFormsApplication1.anyDamnList2<int>'. An explicit conversion exists (are you missing a cast?)"
The fact that the error output indicates "an explicit conversion exists."
Leads me to think that somewhere in the "galaxy" of Linq's many operators/extensions that do conversions ( OfType, Cast, ConvertAll, ToList, and so forth ), or just in the typical ways we now do casting, is a way to get this done without, as in your, much appreciated example :
anyDamnList = new AnyDamnList<int>(list);
Which is essentially discarding the current instance (of 'anyDamnList ) and replacing it with a new instance.
A friend of mine has suggested : "look at implementing the IEnumarable<T> interface instead of inheriting from List<T>."
So maybe that's the next step. And, of course, going back to study Skeet's book more
thanks, Bill
"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844
|
|
|
|
|
BillWoodruff wrote: I am using C#4
Then it should either work as is or you need an implicit conversion operator.
|
|
|
|
|
There's nothing new or LINQish about the explicit cast its talking about. That is just the standard cast to derived. It's not implicit for the same reason you could not assign a Mammal varaible into a Cat variable even if the Mammal variable is holding a Cat.
|
|
|
|
|
Gideon Engelberth wrote: There's nothing new or LINQish about the explicit cast its talking about. That is just the standard cast to derived. It's not implicit for the same reason you could not assign a Mammal varaible into a Cat variable even if the Mammal variable is holding a Cat.
Here we are talking about strongly typed generic class templates, not specific instances of specific classes.
best, Bill
"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844
|
|
|
|
|
BillWoodruff wrote: Here's the question : is there any other way than using 'Clear() and then 'AddRange
to replace the "virtual inner list" <t> "embodied" via inheritance in the class ?
No. anyDamnList "is a" List<int> -> the particular List is an inherent part of a particular instance of anyDamnList -> you cannot replace just the "List part" of an anyDamnList.
That's just the way inheritance works.
LINQ won't help either. LINQ mostly knows about interfaces: IEnumerable and IQueryable; and a little about List and Array. It knows nothing about anyDamnList.
If you want to be able to plug in a new List part into an anyDamnList, you would have to use composition:
public class anyDamnList
{
private List<int> _InnerList;
public void ReplaceInnerList( List<int> newList )
{
_InnerList = newList;
}
}
If you also want anyDamnList to behave like a List<int>, so you can do this:
int count = myAnyDamnList.Count;
Then you can implement the same interfaces as List<int>:
public class anyDamnList : IList<int>
Unfortunately, you have to implement the interfaces yourself:
int Count { get { return _InnerList.Count; } }
This is a pain, but is do-able. Since C# doesn't support multiple inheritance, which means you end up using composition a lot, it would be nice IMHO if there was an implementedBy keyword:
public class anyDamnList : IList<int>
{
IList<int> implementedBy _InnerList;
}
But I've never heard anyone else even suggest this.
Nick
----------------------------------
Be excellent to each other
|
|
|
|
|
Thanks, Nick, for your reply.
The entire point of my using a generic class that inherited from List<t> was to avoid having to implement List methods and properties.
The first example of 'anyDamnList<t> ... which inherits from List<t> ... I presented does "behave" exactly as you would expect a List<t> to behave : once you have created an instance of it :
A definition as simple as this :
public class anyDamnList<T> : List<T> {}
And a use case as simple as this :
anyDamnList<int> myIntList = new anyDamnList<int> { 12, 21, 32 };
Then just type in 'myIntList in VS code followed by the dot : IntelliSense will show you all the "regular" properties you would expect to see for any List<int> : .Count, etc. methods like .Clear(), .AddRange(), .GetRange() ... and of course all the Linq extension methods are there too ... over 50 of them ! ...
... in this case there's no need to use Ilist. Your use of IList is, of course, appropriate for other types of solutions.
imho the one "mising part of the picture" ... and my motivation for posting this post ... was to address the issue of an alternative way to assign the effective internal List<int> "embodied" in the instance rather than use the (to me tedious) call to Clear() followed by a call to 'AddRange().
You wrote : "LINQ won't help either. LINQ mostly knows about interfaces: IEnumerable and IQueryable; and a little about List and Array. It knows nothing about anyDamnList."
imho you are really short-changing the functionality of Linq with this statement. Many of the over 50 Linq extension methods exposed for List<int>, or similar, are effectively methods, methods that have side-effects, methods that transform from type to type, methods that test and return a bool result. Suggest you get Skeet's book "C# in Depth," and prepare to have your "mind blown" about what Linq can do.
best, Bill
"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844
|
|
|
|
|
Hi Bill,
I didn't mean to "short change" LINQ at all. I love it. IMHO, it should have been introduced earlier ( with generics in .NET 2.0 ) as it is just so useful.
You don't lose LINQ if you derive from IList because IList derives from IEnumerable and LINQ is implemented as extension methods on IEnumerable. You can use LINQ on List objects for the same reason: List derives from IEnumerable.
Your original question was about deriving from List and then replacing the "virtual" List object. I just suggested composition as a possible solution for you. If you're happy with using Clear and AddRange then that's good, but then you aren't changing the inner List, you're just changing it's contents.
I've thought up an analogy, so please bear with me
Imagine List is a safe. You can use physical instances of a safe and they would be quite useful.
You can put one in your house and use it yourself: this is composition - you're house "has a" safe. But if people come to your door, they can't use your internal safe.
You could open a window and allow people to deposit and withdraw money from your safe: this is implementing IList on your house class - it's a bit of work for you, but now people can use your safe.
And now you can quite easily replace your instance of safe: just throw the old one away and set the new one. I think this is your original requirement.
Now, deriving from safe would be like building a bank. A bank "is a" safe, but it does more. The internal safe is there, but you cannot replace it because it's a part of your bank. You can, however, change it's contents: this would be the Clear and AddRange solution.
I hope that's useful
Nick
----------------------------------
Be excellent to each other
|
|
|
|
|
Hi Nick,
Nick Butler wrote: I didn't mean to "short change" LINQ at all. I love it. IMHO, it should have been introduced earlier ( with generics in .NET 2.0 ) as it is just so useful.
Sorry, in the context of those terse remarks above, and your previous statement "LINQ won't help either. LINQ mostly knows about interfaces: IEnumerable and IQueryable; and a little about List and Array" : I drew what seemed like a logical conclusion to me that you were not fully aware of the wide gamut of functionality Linq exposes.
Nick Butler wrote: You don't lose LINQ if you derive from IList because IList derives from IEnumerable and LINQ is implemented as extension methods on IEnumerable. You can use LINQ on List objects for the same reason: List derives from IEnumerable.
Your original question was about deriving from List and then replacing the "virtual" List object. I just suggested composition as a possible solution for you. If you're happy with using Clear and AddRange then that's good, but then you aren't changing the inner List, you're just changing it's contents.
I've never suggested any idea that Linq was "loseable" in this context, and I was aware from the get-go that an instance of List<T> and List<int> all implemente/derive from IEnumerable. In fact it's the common derivation from IEnumerable and the range of Linq operators that convert type like Cast and OfType that suggest to me possibility of a transformation of a "vanilla" List<int> theList = new List<int> { 1, 2 }; into something suitable for direct assignment to an instance of anyDamnList<int> was possible.
Nick Butler wrote: Your original question was about deriving from List and then replacing the "virtual" List object. I just suggested composition as a possible solution for you. If you're happy with using Clear and AddRange then that's good, but then you aren't changing the inner List, you're just changing it's contents.
From my point of view the definition of a class which creates a strongly typed generic list that inherits from a <T> type generic list is nothing more than a template, an object factory, as it were, for manufacture of instances of strongly typed generic lists, and there's nothing "virtual" about the instances whatsoever. The instances, imho, are a List, and the fact you can directly "get at" all its properties and methods with simple dot notation indicates to me they are a "first class" List<whatever> object in every sense.
For me the notion of "composition," and the analogy of a "safe" just don't really work for me here. It is an adding an extra level of object wrapper that is unneeded, and in your example the class wrapper is untyped.
If we thought of an instance of a class ... that is generic and inherits from a generic List<T> ... as a "safe" : it would be safe that holds only on type of object; a "transparent safe" where you could look inside and see what was it it with no combination or key required, a safe without "walls" where you could re-arrange, and add things, and delete things with no problem, but, also, a "safe" that in the special case you wanted to replace everything inside it at once required a special and awkward trick to open a "hidden trick" door and replace all of the contents easily; a "safe" that was transparent, had no walls except in one strange case, would not, for me, be a "safe"
Thanks for your time. Like I said, above, I'm going back to studying Skeet's book.
Yes, certainly, your thoughts are "useful"
best, Bill
"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844
modified on Monday, December 14, 2009 11:07 AM
|
|
|
|
|
fyi : < note code only tested in VS Studio beta 2 against FrameWork 4.0 >
If you are defining a generic class template that inherits from a generic List whose type is specifically defined as a "real" type at compile time like this :
public class anyDamnList3<T> : List<int>
{
public List<int> innerList
{
set
{
this.Clear();
this.AddRange(value);
}
}
}
the above will work. Of course you have to use its public property 'innerList to assign its "inner hidden list" value :
anyDamnList3<int> myList5 = new anyDamnList3<int> { 12, 21, 32 };
anyDamnList3<int> myList6 = new anyDamnList3<int> { 42, 53, 66 };
myList6.innerList = new List<int> { 33, 44, 55 };
So : no getting around having to define at least property in the class, and use Clear and AddRange !
The much more interesting case is this : where you are defining a generic strongly typed class which inherits from a List whose content type is generic <t> at compile time :
public class anyDamnList<T> : List<T>
{
public List<int> innerList
{
set
{
this.Clear();
this.AddRange(value as List<T>);
}
}
}
Yes, of course, you could define a method to set the "hidden inner list" rather than a property. Define to your own taste
You may note that in both cases above in actual use the "indexer" [] is exposed automatically. In essence you have every facility that comes with a List<t> type available to you except the ability to automatically assign to the "internal list it embodies." And, of course, just using the instance name returns the contents of what I am calling the "internal list" : in effect you have an automatic 'getter.
Thanks for the responses. I am going back to studying Skeet's magnificent book for a while !
best, Bill
"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844
|
|
|
|
|
Hello, everyone! I'm in need of assistance. I have to write a program calculating a user's input in a switch case.. I'm having problem.. Within my switch case, I reference a method to execute if the case statement is true.
A Little about my program:
The user will be given a menu. User will then select the option they desire and the amount they would like... User can keep entering the same option over and over(which I initialize the counter).
Here's a snippet of my code.
This is a Method of the Switch:
static void SwitchIntialize(ref int userResponse)
{
switch (userResponse)
{
case 1:
GetY(out yQ, out yTotal);
break;
case 2:
{
GetZ(out zQ, out zTotal);
}
break;
default:
{
Console.WriteLine("Sorry, You've entered an Invalid Number");
Console.WriteLine("Please try Again!!");
}
break;
}
}
Now, This is my Method
static void GetY(out double yQ, out double yTotal)
{
const double yPrice = 60.5;
Console.WriteLine("How many pounds would you like of Y? ");
yQ = Convert.ToDouble(Console.ReadLine());
yQ += yQ;
yTotal = yQ * yPrice;
}
static void GetZ(out double zQ, out double zTotal)
{
const double zPrice = 20.39;
Console.WriteLine("How many pounds would you like of Z? ");
zQ = Convert.ToDouble(Console.ReadLine());
zQ += zQ;
zTotal = zQ * zPrice;
}
For my final output, I will have to display how many lbs of X,Y, and Z the user purchase and the price of it..
I've also, declare it in my main as,
SwitchIntialize(ref int userChoice)
The Errors, that I've encountered are:
The Name yQ does not exist in the current context.
The name yTotal does not exist in the current context.
The best overloaded method match 'Program.GetY(out double, out double); has some invalid arguments
Argument '1': cannot convert from 'out yQ' to 'out double'
Argument '2': cannot convert from 'out yTotal' to 'out double'
The Name zQ does not exist in the current context.
The name zTotal does not exist in the current context.
The best overloaded method match 'Program.GetZ(out double, out double); has some invalid arguments
Argument '1': cannot convert from 'out zQ' to 'out double'
Argument '2': cannot convert from 'out zTotal' to 'out double'
|
|
|
|
|
DevonDaDude wrote: GetY(out yQ, out yTotal);
If you have you declared your variable types yQ and yTotal somewhere outside switchinitialize then declare them as static.
static double yQ;<br />
static double yTotal;
The same goes for all the other variables.
There's nothing left in my right brain and nothing right in my left brain. |
|
|
|
|
|
Why would he want to declare them as static? There would appear to be no need for a single copy of them regardless of the number of instances.
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
Simple.
I did not see any variables in his method. So he's missed them. And if he wants to declare them in the class (as class variables) he would have to declare them as static (otherwise the project would not even build). Its entirely up to him.
There's nothing left in my right brain and nothing right in my left brain. |
|
|
|
|
|
Abhinav S wrote: And if he wants to declare them in the class (as class variables) he would have to declare them as static (otherwise the project would not even build)
Not true. You can declare as class variable without static method.
example:
public class myClass
{
public myClass()
{
str = "This has a string";
}
public void WhatIsInString()
{
MessageBox.Show(str);
}
public String str;
}
static is useful when you want variable to be accessible anywhere within project
|
|
|
|
|
Saksida Bojan wrote: public String str;
That's not a class variable, it's an instance variable. Class variables are static , i.e. there is only one copy regardless of how many instances of the class exist. This is different from a static variable defined at the module level which does not belong to a class.
|
|
|
|
|