By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,467 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,467 IT Pros & Developers. It's quick & easy.

Class ... Why is this not working?

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
16 Replies


P: n/a
On Jun 25, 11:14 am, shapper <mdmo...@gmail.comwrote:
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
try instantiating a Tie class then try to access;

Tie tie = new Tie();
tie.Count
Jun 27 '08 #2

P: n/a
On Jun 25, 4:14*pm, shapper <mdmo...@gmail.comwrote:
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 ?
Count is an instance property - so you should be able to do:

Tie someTie = new Tie();
Count count = someTie.Count;

But you can't do:

Count count = Tie.Count;

Jon
Jun 27 '08 #3

P: n/a

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" <md*****@gmail.comwrote in message
news:e6**********************************@m36g2000 hse.googlegroups.com...
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

Jun 27 '08 #4

P: n/a
On Jun 25, 4:23*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jun 25, 4:14*pm, shapper <mdmo...@gmail.comwrote:
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 ?

Count is an instance property - so you should be able to do:

Tie someTie = new Tie();
Count count = someTie.Count;

But you can't do:

Count count = Tie.Count;

Jon
I am trying to do the following:

data.TagPapers = (from t in
database.Tags
select new TagPaper {
Tag = t,
Tie.Count.File =
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
Jun 27 '08 #5

P: n/a
On Jun 25, 4:23*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jun 25, 4:14*pm, shapper <mdmo...@gmail.comwrote:
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 ?

Count is an instance property - so you should be able to do:

Tie someTie = new Tie();
Count count = someTie.Count;

But you can't do:

Count count = Tie.Count;

Jon
I am trying to do the following:

data.TagPapers = (from t in
database.Tags
select new TagPaper {
Tag = t,
Tie.Count.File =
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
Jun 27 '08 #6

P: n/a
On Jun 25, 4:23*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jun 25, 4:14*pm, shapper <mdmo...@gmail.comwrote:
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 ?

Count is an instance property - so you should be able to do:

Tie someTie = new Tie();
Count count = someTie.Count;

But you can't do:

Count count = Tie.Count;

Jon
I am trying to do the following:

data.TagPapers = (from t in
database.Tags
select new TagPaper {
Tag = t,
Tie.Count.File =
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
Jun 27 '08 #7

P: n/a
On Jun 25, 4:44*pm, shapper <mdmo...@gmail.comwrote:
On Jun 25, 4:23*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jun 25, 4:14*pm, shapper <mdmo...@gmail.comwrote:
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 ?
Count is an instance property - so you should be able to do:
Tie someTie = new Tie();
Count count = someTie.Count;
But you can't do:
Count count = Tie.Count;
Jon

I am trying to do the following:

* * * data.TagPapers = (from t in
database.Tags
* * * * * * * * * * * * * * * * * select new TagPaper {
* * * * * * * * * * * * * * * * * * Tag = t,
* * * * * * * * * * * * * * * * * * Tie.Count.File =
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
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/archi...tializers.aspx

That was what I was doing.

Thanks,
Miguel
Jun 27 '08 #8

P: n/a
On Jun 25, 4:44*pm, shapper <mdmo...@gmail.comwrote:
On Jun 25, 4:23*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jun 25, 4:14*pm, shapper <mdmo...@gmail.comwrote:
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 ?
Count is an instance property - so you should be able to do:
Tie someTie = new Tie();
Count count = someTie.Count;
But you can't do:
Count count = Tie.Count;
Jon

I am trying to do the following:

* * * data.TagPapers = (from t in
database.Tags
* * * * * * * * * * * * * * * * * select new TagPaper {
* * * * * * * * * * * * * * * * * * Tag = t,
* * * * * * * * * * * * * * * * * * Tie.Count.File =
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
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/archi...tializers.aspx

That was what I was doing.

Thanks,
Miguel
Jun 27 '08 #9

P: n/a
On Jun 25, 4:56 pm, shapper <mdmo...@gmail.comwrote:

<snip>
But since, I think, NET 3.0 there are automatic properties so you
don't need all those variables:http://weblogs.asp.net/scottgu/archi...-orcas-languag...
Careful of terminology - automatic properties are part of C# 3.0,
not .NET 3.0.
See http://csharpindepth.com/Articles/Ch.../Versions.aspx

Jon
Jun 27 '08 #10

P: n/a
On Jun 25, 4:44 pm, shapper <mdmo...@gmail.comwrote:
I am trying to do the following:

data.TagPapers = (from t in
database.Tags
select new TagPaper {
Tag = t,
Tie.Count.File =
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?
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
Jun 27 '08 #11

P: n/a
On Jun 25, 5:07*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jun 25, 4:56 pm, shapper <mdmo...@gmail.comwrote:

<snip>
But since, I think, NET 3.0 there are automatic properties so you
don't need all those variables:http://weblogs.asp.net/scottgu/archi...-orcas-languag...

Careful of terminology - automatic properties are part of C# 3.0,
not .NET 3.0.
Seehttp://csharpindepth.com/Articles/Chapter1/Versions.aspx

Jon
I think I got it ... I changed my LINQ procedure to:

TagPaper paper = (from t in database.Tags
where t.TagID == id
select new TagPaper {
Tag = t,
new Tie {
new Count {
File = t.FileTag.Count
}
}
}).SingleOrDefault();

Now it does not give me any error ...

Jon, I will read what you sent me ... Ok, C# 3.0.
Jun 27 '08 #12

P: n/a
On Jun 25, 5:15*pm, shapper <mdmo...@gmail.comwrote:
On Jun 25, 5:07*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jun 25, 4:56 pm, shapper <mdmo...@gmail.comwrote:
<snip>
But since, I think, NET 3.0 there are automatic properties so you
don't need all those variables:http://weblogs.asp.net/scottgu/archi...-orcas-languag...
Careful of terminology - automatic properties are part of C# 3.0,
not .NET 3.0.
Seehttp://csharpindepth.com/Articles/Chapter1/Versions.aspx
Jon

I think I got it ... I changed my LINQ procedure to:

* * * TagPaper paper = (from t in database.Tags
* * * * * * * * * * * * where t.TagID == id
* * * * * * * * * * * * select new TagPaper {
* * * * * * * * * * * * * Tag = t,
* * * * * * * * * * * * * new Tie {
* * * * * * * * * * * * * * new Count {
* * * * * * * * * * * * * * * File = t.FileTag.Count
* * * * * * * * * * * * * * }
* * * * * * * * * * * * * }
* * * * * * * * * * * * }).SingleOrDefault();

Now it does not give me any error ...

Jon, I will read what you sent me ... Ok, C# 3.0.
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
Jun 27 '08 #13

P: n/a
On Jun 25, 5:15 pm, shapper <mdmo...@gmail.comwrote:
I think I got it ... I changed my LINQ procedure to:

TagPaper paper = (from t in database.Tags
where t.TagID == id
select new TagPaper {
Tag = t,
new Tie {
new Count {
File = t.FileTag.Count
}
}
}).SingleOrDefault();

Now it does not give me any error ...
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
Jun 27 '08 #14

P: n/a
Ok, I didn't realize you were there yet.

And I'm a little behind in the 3.5 (framework).


"shapper" <md*****@gmail.comwrote in message
news:2b**********************************@b1g2000h sg.googlegroups.com...
On Jun 25, 4:44 pm, shapper <mdmo...@gmail.comwrote:
On Jun 25, 4:23 pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jun 25, 4:14 pm, shapper <mdmo...@gmail.comwrote:
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 ?
Count is an instance property - so you should be able to do:
Tie someTie = new Tie();
Count count = someTie.Count;
But you can't do:
Count count = Tie.Count;
Jon

I am trying to do the following:

data.TagPapers = (from t in
database.Tags
select new TagPaper {
Tag = t,
Tie.Count.File =
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
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/archi...tializers.aspx

That was what I was doing.

Thanks,
Miguel
Jun 27 '08 #15

P: n/a
On Jun 25, 5:23*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jun 25, 5:15 pm, shapper <mdmo...@gmail.comwrote:
I think I got it ... I changed my LINQ procedure to:
* * * TagPaper paper = (from t in database.Tags
* * * * * * * * * * * * where t.TagID == id
* * * * * * * * * * * * select new TagPaper {
* * * * * * * * * * * * * Tag = t,
* * * * * * * * * * * * * new Tie {
* * * * * * * * * * * * * * new Count {
* * * * * * * * * * * * * * * File = t.FileTag.Count
* * * * * * * * * * * * * * }
* * * * * * * * * * * * * }
* * * * * * * * * * * * }).SingleOrDefault();
Now it does not give me any error ...

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
In fact it must be:
TagPaper paper = (from t in database.Tags
where t.TagID == id
select new TagPaper {
Tag = t,
Tie = new Tie { Count = new Count{ File =
t.FileTag.Count } }
}).SingleOrDefault();

If I don't use new Tie:

TagPaper paper = (from t in database.Tags
where t.TagID == id
select new TagPaper {
Tag = t,
Tie = { Count = { File = 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
Jun 27 '08 #16

P: n/a
shapper <md*****@gmail.comwrote:
In fact it must be:
TagPaper paper = (from t in database.Tags
where t.TagID == id
select new TagPaper {
Tag = t,
Tie = new Tie { Count = new Count{ File =
t.FileTag.Count } }
}).SingleOrDefault();
Well, that depends on the context.
If I don't use new Tie:

TagPaper paper = (from t in database.Tags
where t.TagID == id
select new TagPaper {
Tag = t,
Tie = { Count = { File = 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.
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 - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #17

This discussion thread is closed

Replies have been disabled for this discussion.