I'm using VS.NET 2003. If I tried to create an example program that didn't
compile, I wouldn't be able to. I honestly believe that this is a problem
with the C# compiler. These problems crop up in code that HAS worked for
months. Usually silly things like changing a foreach to a for will fix it.
For example, I fixed one a couple weeks ago. Only one member of the struct
was causing compile errors, so I changed the code around inside the struct.
Here's how I did it.
struct TestStruct {
public int a; // <-- still works fine
//public int b; // <-- broken
private int m_b;
public int b {
get {return m_b;}
set {m_b = value;}
}
}
that's right, simply changing a public variable to a property fixed the
problem.
I've researched the error (Use of possibly unassigned field) thoroughly.
Google only came up with documentation, not anyone else with problems.
Something I haven't tried is to compile it on another computer when it does
not compile on mine.
I do appreciate your interest in this, but I'm afraid that in the end we
won't be any closer to figuring it out.
Chris LaJoie
"Jon Skeet" <skeet@pobox.com> wrote in message
news:MPG.19d2190bcd2c97d79896c5@news.microsoft.com ...[color=blue]
> Chris LaJoie <chris@etriptrader.com> wrote:[color=green]
> > as I said earlier, my problem has been solved, but just to clear some[/color][/color]
things[color=blue][color=green]
> > up I'll post some sample code. I have my struct:
> >
> > struct TestStruct {
> > public int a;
> > public int b;
> > }
> >
> > now I have a function that only assigns the values of the struct. In my
> > case, this function did not get any compile errors:
> >
> > public TestStruct[] Parse() {
> > TestStruct[] ret = new TestStruct[10];
> > for(int x = 0; x < ret.Length; x++) {
> > ret[x].a = 1;
> > ret[x].b = 2;
> > }
> > return ret;
> > }[/color]
>
> Right - just assigning will never have any problem.
>[color=green]
> > as you can see, that function doesn't do any reading of the values, only
> > assigns them. I have another function, which reads the data and inserts[/color][/color]
it[color=blue][color=green]
> > in our database.
> >
> > public bool InsertInDB(TestStruct[] things) {
> > StringBuilder sql = new StringBuilder("INSERT....");
> > foreach(TestStruct t in things) {
> > sql.Append(sqlify(t.a) + ", "); // <-- compile error here
> > sql.Append(sqlify(t.b) + ")"); // <-- compile error here
> >
> > [...insert in db...etc...]
> > }
> > }[/color]
>
> That *is* strange. I don't get a compile-time error with this, for
> instance:
>
> using System;
>
> struct Foo
> {
> public int x;
> }
>
> class Test
> {
> static void Main(string[] args)
> {
> Foo[] f = new Foo[5];
> f[0].x=5;
> Bar (f);
> }
>
> static void Bar (Foo[] x)
> {
> foreach (Foo y in x)
> {
> Console.WriteLine (y.x);
> }
> }
> }
>
> <snip>
>[color=green]
> > I appologize for using such silly examples, but I'm not allowed to post
> > company code.[/color]
>
> That's not a problem at all - but I'm still finding it hard to see why
> you're getting an error in the first place. Could you try compiling my
> code above? It should compile without warning. If it does, could you
> try constructing a similar complete test case which *doesn't* compile?
>
> Which version of Visual Studio (or csc) are you using?
>
> --
> Jon Skeet - <skeet@pobox.com>
>
http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too[/color]