.NET Framework - Class ... Why is this not working?
Asked By shapper
25-Jun-08 09:13 PM
Hello,
I have the following classes:
namespace MyApp.Models {
public class Tie {
public Count Count { get; set; }
public Tie() {
this.Count = new Count();
}
}
}
namespace MyApp.Models
{
public class Count
{
public int File { get; set; }
public int Professor { get; set; }
}
}
However, when I access the Tie class in my code the only think Visual
Studio recognizes is:
Tie.Tie.Tie ...
Why can't I access Tie.Count ?
The Tie method inside the Tie class is only a constructor to
initialize the Count property.
Am I doing something wrong?
I have been using VB.NET for a long time and C# just recently so I am
still getting familiar with it.
Thanks,
Miguel
Visual Studio
(1)
InvalidOperationException
(1)
Console.WriteLine
(1)
SingleOrDefault
(1)
FileTag.Count
(1)
MemberBinding
(1)
MyApp.Models
(1)
VB.NET
(1)
parez replied...
try instantiating a Tie class then try to access;
Tie tie = new Tie();
tie.Count
Jon Skeet [C# MVP] replied...
Count is an instance property - so you should be able to do:
Tie someTie =3D new Tie();
Count count =3D someTie.Count;
But you cannot do:
Count count =3D Tie.Count;
Jon
sloan replied...
Since this is newer to you, I'm gonna try to post what I think you're after.
I"m not sure though.
I renamed one thing to "TheCount" so we can avoid ambiguity in the
discussion.
Tie t1 = new Tie();
Console.WriteLine(t1.TheCount.File);
Console.WriteLine(t1.TheCount.Professor);
public class Tie
{
private Count _count1 = null;
public Count TheCount
{
get
{
return _count1;
}
set
{
_count1 = value;
}
}
public Tie()
{
this.TheCount = new Count();
}
}
public class Count
{
private int _file = 0;
private int _prof = 0;
public int File
{
get
{
return _file;
}
set
{
_file = value;
}
}
public int Professor
{
get
{
return _prof;
}
set
{
_prof = value;
}
}
}
shapper replied...
I am trying to do the following:
data.TagPapers =3D (from t in
database.Tags
select new TagPaper {
Tag =3D t,
Tie.Count.File =3D
t.FileTag.Count
}).ToList
Where TagPapers is a List of TagPaper which is a class with 2
properties: Tag and Tie.
Shouldn't this be working?
It is not working on Tie.Count.File
I have no idea what is wrong. It seems right to me.
Any idea?
Thanks,
Miguel
shapper replied...
I am trying to do the following:
data.TagPapers =3D (from t in
database.Tags
select new TagPaper {
Tag =3D t,
Tie.Count.File =3D
t.FileTag.Count
}).ToList
Where TagPapers is a List of TagPaper which is a class with 2
properties: Tag and Tie.
Shouldn't this be working?
It is not working on Tie.Count.File
I have no idea what is wrong. It seems right to me.
Any idea?
Thanks,
Miguel
shapper replied...
I am trying to do the following:
data.TagPapers =3D (from t in
database.Tags
select new TagPaper {
Tag =3D t,
Tie.Count.File =3D
t.FileTag.Count
}).ToList
Where TagPapers is a List of TagPaper which is a class with 2
properties: Tag and Tie.
Shouldn't this be working?
It is not working on Tie.Count.File
I have no idea what is wrong. It seems right to me.
Any idea?
Thanks,
Miguel
shapper replied...
t new TagPaper {
ag =3D t,
ie.Count.File =3D
ist
Sloan,
But since, I think, NET 3.0 there are automatic properties so you
don't need all those variables:
http://weblogs.asp.net/scottgu/archive/2007/03/08/new-c-orcas-language-feat=
ures-automatic-properties-object-initializers-and-collection-initializers.a=
spx
That was what I was doing.
Thanks,
Miguel
shapper replied...
t new TagPaper {
ag =3D t,
ie.Count.File =3D
ist
Sloan,
But since, I think, NET 3.0 there are automatic properties so you
don't need all those variables:
http://weblogs.asp.net/scottgu/archive/2007/03/08/new-c-orcas-language-feat=
ures-automatic-properties-object-initializers-and-collection-initializers.a=
spx
That was what I was doing.
Thanks,
Miguel
Jon Skeet [C# MVP] replied...
Careful of terminology - automatic properties are part of C# 3.0,
not .NET 3.0.
See http://csharpindepth.com/Articles/Chapter1/Versions.aspx
Jon
Jon Skeet [C# MVP] replied...
No, it shouldn't. (It would have been helpful to explain that you were
trying to use an object initializer in your first post, btw - this is
where short but complete examples come in very handy.)
You can do:
select new TagPaper { Tag = t, Tie = { Count = { File =
t.FileTag.Count } } }
That's the proper syntax for setting properties on embedded objects.
Jon
shapper replied...
007/03/08/new-c-orcas-languag...
I think I got it ... I changed my LINQ procedure to:
TagPaper paper =3D (from t in database.Tags
where t.TagID =3D=3D id
select new TagPaper {
Tag =3D t,
new Tie {
new Count {
File =3D t.FileTag.Count
}
}
}).SingleOrDefault();
Now it does not give me any error ...
Jon, I will read what you sent me ... Ok, C# 3.0.
shapper replied...
/2007/03/08/new-c-orcas-languag...
leTag.Count
Thanks Jon.
Yes I know but I though the problem was with my classes ... I am just
moving from VB.NET to C# since I started to use MVC ...
Thank you for the help!
Miguel
Jon Skeet [C# MVP] replied...
It should. It certainly does in my test program.
It should be "Tie = {" rather than "new Tie {" and the same for Count.
(You could equally do "Tie = new Tie {" which is subtly different,
setting the Tie property rather than merely setting properties within
the existing value.)
Jon
sloan replied...
Ok, I didn't realize you were there yet.
And I'm a little behind in the 3.5 (framework).
Sloan,
But since, I think, NET 3.0 there are automatic properties so you
don't need all those variables:
http://weblogs.asp.net/scottgu/archive/2007/03/08/new-c-orcas-language-features-automatic-properties-object-initializers-and-collection-initializers.aspx
That was what I was doing.
Thanks,
Miguel
shapper replied...
FileTag.Count
In fact it must be:
TagPaper paper =3D (from t in database.Tags
where t.TagID =3D=3D id
select new TagPaper {
Tag =3D t,
Tie =3D new Tie { Count =3D new Count{ File =3D
t.FileTag.Count } }
}).SingleOrDefault();
If I don't use new Tie:
TagPaper paper =3D (from t in database.Tags
where t.TagID =3D=3D id
select new TagPaper {
Tag =3D t,
Tie =3D { Count =3D { File =3D t.FileTag.Count } =
}
}).SingleOrDefault();
I get the error: System.InvalidOperationException: Unhandled binding
type: MemberBinding
So I think there is no way around it ... must use new.
Thanks,
Miguel
Jon Skeet [C# MVP] replied...
Well, that depends on the context.
If you're using LINQ to SQL, it looks like it. That would be fine for
LINQ to Objects - it's valid C#, just not a pattern LINQ to SQL
recognises.
--
Jon Skeet - <skeet@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
that is not what I need. I have tried re-installing Service Pack 1 for Visual Studio 2008. I am really hoping that I will not have to to a un-install of Visual Studio. I am running Windows XP Version 5.1.2600 Service Pack 3 Build 2600 Just in-case it is helpful, I am pasting the info from my Visual Studio about window below * ** ** **ABOUT INFO* ** ** * Microsoft Visual Studio 2008 Version 9.0.30729.1 SP Microsoft .NET Framework Version 3.5 SP1 Installed
hash, out salt); String resultHash = 3D Convert.ToBase64String(hash); String resultSalt = 3D Convert.ToBase64String(salt); Console.WriteLine("Hash:"); Console.WriteLine(resultHash); Console.WriteLine("Salt:"); Console.WriteLine(resultSalt); Byte[] hash2; Hash(password, out hash2, out salt); Console.WriteLine("Compares hashes:"); Console.WriteLine
it in dot.net? How can I do this is vb.net? VB.NET Discussions Visual Studio 2010 (1) Visual Studio (1) Silverlight (1) Windows 7 (1) Office (1) Vista (1) Linux (1) Error (1) HI FileRecord hFile = FreeFile() FileOpen(hFile, "testing.rnd", OpenMode.Random, OpenAccess.Read) FileGet(hFile, rec, 2) Console.WriteLine("*{0}*", rec.aString) End Sub End Module Personally, I would abandon this method. Like I record in console ReadRecord(2) InitialiseCols() 'initialise columns used for printing PrintDataFile() 'print data file Console.WriteLine() Console.Write("Press any key to continue. . .") Console.ReadKey() End Sub Private Sub CreateDataFile() ' Create
OO-Sicht. MfG, Alex class Program { static void Main(string[] args) { Test test = new Test(); Console.WriteLine(test.Value); test.Value = 24; / / compiliert nicht, weil setter private ist Console.ReadLine(); } } internal class namespace OuterSpace { using InnerSpace; class Program { static void Main(string[] args) { UserClass1 AnyClass = new UserClass1(); Console.WriteLine(AnyClass.Argument1.Value); / / hier bitte compilerwarnung AnyClass.Argument1.Value = 70.0; Console.WriteLine(AnyClass.Argument1.Value); CollectionClass AnySystem = new CollectionClass(); Console.WriteLine(AnySystem.MySystem[0].Argument1.Value); / / hier bitte compilerwarnung AnySystem.MySystem[0].Argument1.Value = 90.0
void Main() { try { ManagementObjectSearcher searcher = new ManagementObjectSearcher("root \ CIMV2", foreach (ManagementObject queryObj in searcher.Get()) { Console.WriteLine("- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --"); Console.WriteLine("Win32_Product instance"); Console.WriteLine("- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --"); Console.WriteLine("AssignmentType: {0}", queryObj["AssignmentType"]); Console.WriteLine("Caption: {0}", queryObj["Caption"]); Console.WriteLine("Description: {0}", queryObj