Class ... Why is this not working?

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

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
 
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
 
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
 
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;

}

}





}
 
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
 
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
 
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
 
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/arch...initializers-and-collection-initializers.aspx

That was what I was doing.

Thanks,
Miguel
 
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/arch...initializers-and-collection-initializers.aspx

That was what I was doing.

Thanks,
Miguel
 
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
 
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.
 
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
 
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
 
Ok, I didn't realize you were there yet.

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




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/arch...initializers-and-collection-initializers.aspx

That was what I was doing.

Thanks,
Miguel
 
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
 
shapper said:
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.
 
Back
Top