Use of unassigned local variable 'myDB"

R

r h

using System;
using System.Collections.Generic;
using System.Text;
using Autodesk;
using Autodesk.AutoCAD;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;

namespace AcadProj2
{
public class Commands
{

[CommandMethod("newCmd1")]
static public void newCmd1()
{
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Place your code here.");
}

[CommandMethod("DIS")]
static public void DrawInModelSpace()
{
TransactionManager myTransMan;
Transaction myTrans;
Database myDB;

BlockTable myBT;
BlockTableRecord myBTR;

Point3d cenPT = new Point3d(1,2,3);
Vector3d cVect = new Vector3d(0,0,1);
Circle myCircle = new Circle(cenPT,cVect,1);

//open the database for Read
myBT = (BlockTable) myDB.BlockTableId.GetObject(OpenMode.ForRead);

//Get the block table record and add a new circle to the block.
myBTR = (BlockTableRecord)myBT[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite);
myBTR.AppendEntity(myCircle);
myTrans.AddNewlyCreatedDBObject(myCircle,true);

//commit the Transaction
myTrans.Commit();

//Dispose of the Transaction objects
myTrans.Dispose();
myTransMan.Dispose();
}
}
}

EggHeadCafe - Software Developer Portal of Choice
C# and GDI : draw Round rectangles, fonts, ellipse, fonts
http://www.eggheadcafe.com/tutorial...f96-f7b6c502a6e2/c-and-gdi--draw-round-r.aspx
 
P

Peter Duniho

r said:
[...]
Database myDB;

BlockTable myBT;
BlockTableRecord myBTR;

Point3d cenPT = new Point3d(1,2,3);
Vector3d cVect = new Vector3d(0,0,1);
Circle myCircle = new Circle(cenPT,cVect,1);

//open the database for Read
myBT = (BlockTable) myDB.BlockTableId.GetObject(OpenMode.ForRead);

Yes. And?
 
R

r h

So My Question is why am i getting this error. please help thank you.



r h wrote:

Use of unassigned local variable 'myDB"
16-Oct-09

using System;
using System.Collections.Generic;
using System.Text;
using Autodesk;
using Autodesk.AutoCAD;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;

namespace AcadProj2
{
public class Commands
{

[CommandMethod("newCmd1")]
static public void newCmd1()
{
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Place your code here.");
}

[CommandMethod("DIS")]
static public void DrawInModelSpace()
{
TransactionManager myTransMan;
Transaction myTrans;
Database myDB;

BlockTable myBT;
BlockTableRecord myBTR;

Point3d cenPT = new Point3d(1,2,3);
Vector3d cVect = new Vector3d(0,0,1);
Circle myCircle = new Circle(cenPT,cVect,1);

//open the database for Read
myBT = (BlockTable) myDB.BlockTableId.GetObject(OpenMode.ForRead);

//Get the block table record and add a new circle to the block.
myBTR = (BlockTableRecord)myBT[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite);
myBTR.AppendEntity(myCircle);
myTrans.AddNewlyCreatedDBObject(myCircle,true);

//commit the Transaction
myTrans.Commit();

//Dispose of the Transaction objects
myTrans.Dispose();
myTransMan.Dispose();
}
}
}

Previous Posts In This Thread:

EggHeadCafe - Software Developer Portal of Choice
Distributed .NET Programming in C# [aPress]
http://www.eggheadcafe.com/tutorial...80-0c490b10aa2f/distributed-net-programm.aspx
 
M

Mike Lovell

So My Question is why am i getting this error. please help thank you.

Use of unassigned local variable 'myDB"

[CommandMethod("DIS")]
static public void DrawInModelSpace()
{
TransactionManager myTransMan;
Transaction myTrans;
Database myDB;

BlockTable myBT;
BlockTableRecord myBTR;

Point3d cenPT = new Point3d(1,2,3);
Vector3d cVect = new Vector3d(0,0,1);
Circle myCircle = new Circle(cenPT,cVect,1);

//open the database for Read
myBT = (BlockTable) myDB.BlockTableId.GetObject(OpenMode.ForRead);

You've used a class without initializing it first, in this case you've
declared 'myDB' with no value:

Database myDB;

then you've tried to use it:

myDB.BlockTableId.GetObject(OpenMode.ForRead);

You should create a new instance of the class, best time to do with is when
you declare it:

Database myDB = new Database();

(The class might when some arguments in the constructor that are requires,
intellisense will show you this when you type the first "(")


~ Mike
 
R

rharris_jr

r said:
[...]
                   Database myDB;
                   BlockTable myBT;
                   BlockTableRecord myBTR;
                   Point3d cenPT = new Point3d(1,2,3);
                   Vector3d cVect = new Vector3d(0,0,1);
                   Circle myCircle = new Circle(cenPT,cVect,1);
                   //open the database for Read
                   myBT = (BlockTable) myDB.BlockTableId.GetObject(OpenMode.ForRead);

Yes.  And?

do you happen to know why this error is happening?
 
P

Peter Duniho

r said:
[...]
Database myDB;
BlockTable myBT;
BlockTableRecord myBTR;
Point3d cenPT = new Point3d(1,2,3);
Vector3d cVect = new Vector3d(0,0,1);
Circle myCircle = new Circle(cenPT,cVect,1);
//open the database for Read
myBT = (BlockTable) myDB.BlockTableId.GetObject(OpenMode.ForRead);
Yes. And?

do you happen to know why this error is happening?

Yes. But the error message explains it perfectly, at least as well as I
could.

Mike's reply offers useful advice with respect to a possible fix, but
you might have to do more work than just what he shows, depending on
what exactly is the proper way to use a "Database" object in the context
of your program.

As for what that work specifically might be, you either need to know
already what it is, or you need to consult a forum where questions about
the Autodesk/AutoCAD .NET API are on-topic. You appear to be using a
"Database" class that is not part of .NET, so you should not expect that
anyone here would be able to tell you specifically how to use it.

All we can really do is tell you that when the compiler says to you that
there's a "Use of unassigned local variable 'myDB'", that you have in
fact tried to use the local variable 'myDB' before it's been assigned.

Pete
 
R

rharris_jr

r h wrote:
[...]
                   Database myDB;
                   BlockTable myBT;
                   BlockTableRecord myBTR;
                   Point3d cenPT = new Point3d(1,2,3);
                   Vector3d cVect = new Vector3d(0,0,1);
                   Circle myCircle = new Circle(cenPT,cVect,1);
                   //open the database for Read
                   myBT = (BlockTable) myDB.BlockTableId.GetObject(OpenMode.ForRead);
Yes.  And?
do you happen to know why this error is happening?

Yes.  But the error message explains it perfectly, at least as well as I
could.

Mike's reply offers useful advice with respect to a possible fix, but
you might have to do more work than just what he shows, depending on
what exactly is the proper way to use a "Database" object in the context
of your program.

As for what that work specifically might be, you either need to know
already what it is, or you need to consult a forum where questions about
the Autodesk/AutoCAD .NET API are on-topic.  You appear to be using a
"Database" class that is not part of .NET, so you should not expect that
anyone here would be able to tell you specifically how to use it.

All we can really do is tell you that when the compiler says to you that
there's a "Use of unassigned local variable 'myDB'", that you have in
fact tried to use the local variable 'myDB' before it's been assigned.

Pete- Hide quoted text -

- Show quoted text -

thank you all for you r help and patience. thank you very much.
 
Top